github.com/git-lfs/git-lfs@v2.5.2+incompatible/t/t-update.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  . "$(dirname "$0")/testlib.sh"
     4  
     5  begin_test "update"
     6  (
     7    set -e
     8  
     9    pre_push_hook="#!/bin/sh
    10  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.\\n\"; exit 2; }
    11  git lfs pre-push \"\$@\""
    12  
    13    post_checkout_hook="#!/bin/sh
    14  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout.\\n\"; exit 2; }
    15  git lfs post-checkout \"\$@\""
    16  
    17    post_commit_hook="#!/bin/sh
    18  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-commit.\\n\"; exit 2; }
    19  git lfs post-commit \"\$@\""
    20  
    21    post_merge_hook="#!/bin/sh
    22  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-merge.\\n\"; exit 2; }
    23  git lfs post-merge \"\$@\""
    24  
    25    mkdir without-pre-push
    26    cd without-pre-push
    27    git init
    28  
    29    [ "Updated git hooks." = "$(git lfs update)" ]
    30    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    31    [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
    32    [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
    33    [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
    34  
    35    # run it again
    36    [ "Updated git hooks." = "$(git lfs update)" ]
    37    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    38    [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
    39    [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
    40    [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
    41  
    42    # replace old hook 1
    43    echo "#!/bin/sh
    44  git lfs push --stdin \$*" > .git/hooks/pre-push
    45    [ "Updated git hooks." = "$(git lfs update)" ]
    46    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    47  
    48    # replace old hook 2
    49    echo "#!/bin/sh
    50  git lfs push --stdin \"\$@\"" > .git/hooks/pre-push
    51    [ "Updated git hooks." = "$(git lfs update)" ]
    52    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    53  
    54    # replace old hook 3
    55    echo "#!/bin/sh
    56  git lfs pre-push \"\$@\"" > .git/hooks/pre-push
    57    [ "Updated git hooks." = "$(git lfs update)" ]
    58    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    59  
    60    # replace blank hook
    61    rm .git/hooks/pre-push
    62    touch .git/hooks/pre-push
    63    touch .git/hooks/post-checkout
    64    touch .git/hooks/post-merge
    65    [ "Updated git hooks." = "$(git lfs update)" ]
    66    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    67    [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
    68    [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
    69    [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
    70  
    71    # replace old hook 4
    72    echo "#!/bin/sh
    73  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 0; }
    74  git lfs pre-push \"$@\""
    75    [ "Updated git hooks." = "$(git lfs update)" ]
    76    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    77  
    78    # replace old hook 5
    79    echo "#!/bin/sh
    80  command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 2; }
    81  git lfs pre-push \"$@\""
    82    [ "Updated git hooks." = "$(git lfs update)" ]
    83    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
    84  
    85    # don't replace unexpected hook
    86    echo "test" > .git/hooks/pre-push
    87    echo "test" > .git/hooks/post-checkout
    88    echo "test" > .git/hooks/post-commit
    89    echo "test" > .git/hooks/post-merge
    90    expected="Hook already exists: pre-push
    91  
    92  	test
    93  
    94  To resolve this, either:
    95    1: run \`git lfs update --manual\` for instructions on how to merge hooks.
    96    2: run \`git lfs update --force\` to overwrite your hook."
    97  
    98    [ "$expected" = "$(git lfs update 2>&1)" ]
    99    [ "test" = "$(cat .git/hooks/pre-push)" ]
   100    [ "test" = "$(cat .git/hooks/post-checkout)" ]
   101    [ "test" = "$(cat .git/hooks/post-commit)" ]
   102    [ "test" = "$(cat .git/hooks/post-merge)" ]
   103  
   104    # Make sure returns non-zero
   105    set +e
   106    git lfs update
   107    if [ $? -eq 0 ]
   108    then
   109      exit 1
   110    fi
   111    set -e
   112  
   113    # test manual steps
   114    expected="Add the following to .git/hooks/pre-push:
   115  
   116  	#!/bin/sh
   117  	command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.\n\"; exit 2; }
   118  	git lfs pre-push \"\$@\"
   119  
   120  Add the following to .git/hooks/post-checkout:
   121  
   122  	#!/bin/sh
   123  	command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout.\n\"; exit 2; }
   124  	git lfs post-checkout \"\$@\"
   125  
   126  Add the following to .git/hooks/post-commit:
   127  
   128  	#!/bin/sh
   129  	command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-commit.\n\"; exit 2; }
   130  	git lfs post-commit \"\$@\"
   131  
   132  Add the following to .git/hooks/post-merge:
   133  
   134  	#!/bin/sh
   135  	command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-merge.\n\"; exit 2; }
   136  	git lfs post-merge \"\$@\""
   137  
   138    [ "$expected" = "$(git lfs update --manual 2>&1)" ]
   139    [ "test" = "$(cat .git/hooks/pre-push)" ]
   140    [ "test" = "$(cat .git/hooks/post-checkout)" ]
   141    [ "test" = "$(cat .git/hooks/post-commit)" ]
   142    [ "test" = "$(cat .git/hooks/post-merge)" ]
   143  
   144    # force replace unexpected hook
   145    [ "Updated git hooks." = "$(git lfs update --force)" ]
   146    [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
   147    [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
   148    [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
   149    [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
   150  
   151    has_test_dir || exit 0
   152  
   153    echo "test with bare repository"
   154    cd ..
   155    git clone --mirror without-pre-push bare
   156    cd bare
   157    git lfs env
   158    git lfs update
   159    ls -al hooks
   160    [ "$pre_push_hook" = "$(cat hooks/pre-push)" ]
   161  )
   162  end_test
   163  
   164  begin_test "update with leading spaces"
   165  (
   166    set -e
   167  
   168    reponame="update-leading-spaces"
   169    git init "$reponame"
   170    cd "$reponame"
   171  
   172    [ "Updated git hooks." = "$(git lfs update)" ]
   173  
   174    # $pre_push_hook contains leading TAB '\t' characters
   175    pre_push_hook="#!/bin/sh
   176  	command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.\\n\"; exit 2; }
   177  	git lfs pre-push \"\$@\""
   178  
   179    echo -n "$pre_push_hook" > .git/hooks/pre-push
   180  
   181    [ "Updated git hooks." = "$(git lfs update)" ]
   182  )
   183  end_test
   184  
   185  begin_test "update lfs.{url}.access"
   186  (
   187    set -e
   188  
   189    mkdir update-access
   190    cd update-access
   191    git init
   192    git config lfs.http://example.com.access private
   193    git config lfs.https://example.com.access private
   194    git config lfs.https://example2.com.access basic
   195    git config lfs.https://example3.com.access other
   196  
   197    [ "private" = "$(git config lfs.http://example.com.access)" ]
   198    [ "private" = "$(git config lfs.https://example.com.access)" ]
   199    [ "basic" = "$(git config lfs.https://example2.com.access)" ]
   200    [ "other" = "$(git config lfs.https://example3.com.access)" ]
   201  
   202    expected="Updated git hooks.
   203  Updated http://example.com access from private to basic.
   204  Updated https://example.com access from private to basic.
   205  Removed invalid https://example3.com access of other."
   206  )
   207  end_test
   208  
   209  begin_test "update: outside git repository"
   210  (
   211    if [ -d "hooks" ]; then
   212      ls -al
   213      echo "hooks dir exists"
   214      exit 1
   215    fi
   216  
   217    set +e
   218    git lfs update 2>&1 > check.log
   219    res=$?
   220    set -e
   221  
   222    if [ "$res" = "0" ]; then
   223      if [ -z "$GIT_LFS_TEST_DIR" ]; then
   224        echo "Passes because $GIT_LFS_TEST_DIR is unset."
   225        exit 0
   226      fi
   227    fi
   228  
   229    [ "$res" = "128" ]
   230  
   231    if [ -d "hooks" ]; then
   232      ls -al
   233      echo "hooks dir exists"
   234      exit 1
   235    fi
   236  
   237    cat check.log
   238    grep "Not in a git repository" check.log
   239  )
   240  end_test