Merge branch 'bc/hash-independent-tests'
authorJunio C Hamano <gitster@pobox.com>
Wed, 30 May 2018 12:51:28 +0000 (21:51 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 May 2018 12:51:28 +0000 (21:51 +0900)
Many tests hardcode the raw object names, which would change once
we migrate away from SHA-1. While some of them must test against
exact object names, most of them do not have to use hardcoded
constants in the test. The latter kind of tests have been updated
to test the moral equivalent of the original without hardcoding the
actual object names.

* bc/hash-independent-tests: (28 commits)
t5300: abstract away SHA-1-specific constants
t4208: abstract away SHA-1-specific constants
t4045: abstract away SHA-1-specific constants
t4042: abstract away SHA-1-specific constants
t4205: sort log output in a hash-independent way
t/lib-diff-alternative: abstract away SHA-1-specific constants
t4030: abstract away SHA-1-specific constants
t4029: abstract away SHA-1-specific constants
t4029: fix test indentation
t4022: abstract away SHA-1-specific constants
t4020: abstract away SHA-1-specific constants
t4014: abstract away SHA-1-specific constants
t4008: abstract away SHA-1-specific constants
t4007: abstract away SHA-1-specific constants
t3905: abstract away SHA-1-specific constants
t3702: abstract away SHA-1-specific constants
t3103: abstract away SHA-1-specific constants
t2203: abstract away SHA-1-specific constants
t: skip pack tests if not using SHA-1
t4044: skip test if not using SHA-1
...

1  2 
t/t1400-update-ref.sh
t/t1512-rev-parse-disambiguation.sh
t/t2025-worktree-add.sh
t/t4014-format-patch.sh
t/t5300-pack-object.sh
t/t5516-fetch-push.sh
t/test-lib.sh
diff --combined t/t1400-update-ref.sh
index faf0dfe993d375c885762b2d15706cd8ebe6d577,f6dc05654ea5a2f06b41d8c300f8916b2e773972..e1fd0f0ca8b5d731d79ccca1d6e6140683a3665f
@@@ -6,7 -6,7 +6,7 @@@
  test_description='Test git update-ref and basic ref logging'
  . ./test-lib.sh
  
- Z=$_z40
+ Z=$ZERO_OID
  
  m=refs/heads/master
  n_dir=refs/heads/gu
@@@ -457,66 -457,6 +457,66 @@@ test_expect_success 'git cat-file blob 
        test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")
  '
  
 +# Test adding and deleting pseudorefs
 +
 +test_expect_success 'given old value for missing pseudoref, do not create' '
 +      test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
 +      test_path_is_missing .git/PSEUDOREF &&
 +      grep "could not read ref" err
 +'
 +
 +test_expect_success 'create pseudoref' '
 +      git update-ref PSEUDOREF $A &&
 +      test $A = $(cat .git/PSEUDOREF)
 +'
 +
 +test_expect_success 'overwrite pseudoref with no old value given' '
 +      git update-ref PSEUDOREF $B &&
 +      test $B = $(cat .git/PSEUDOREF)
 +'
 +
 +test_expect_success 'overwrite pseudoref with correct old value' '
 +      git update-ref PSEUDOREF $C $B &&
 +      test $C = $(cat .git/PSEUDOREF)
 +'
 +
 +test_expect_success 'do not overwrite pseudoref with wrong old value' '
 +      test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
 +      test $C = $(cat .git/PSEUDOREF) &&
 +      grep "unexpected object ID" err
 +'
 +
 +test_expect_success 'delete pseudoref' '
 +      git update-ref -d PSEUDOREF &&
 +      test_path_is_missing .git/PSEUDOREF
 +'
 +
 +test_expect_success 'do not delete pseudoref with wrong old value' '
 +      git update-ref PSEUDOREF $A &&
 +      test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
 +      test $A = $(cat .git/PSEUDOREF) &&
 +      grep "unexpected object ID" err
 +'
 +
 +test_expect_success 'delete pseudoref with correct old value' '
 +      git update-ref -d PSEUDOREF $A &&
 +      test_path_is_missing .git/PSEUDOREF
 +'
 +
 +test_expect_success 'create pseudoref with old OID zero' '
 +      git update-ref PSEUDOREF $A $Z &&
 +      test $A = $(cat .git/PSEUDOREF)
 +'
 +
 +test_expect_success 'do not overwrite pseudoref with old OID zero' '
 +      test_when_finished git update-ref -d PSEUDOREF &&
 +      test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
 +      test $A = $(cat .git/PSEUDOREF) &&
 +      grep "already exists" err
 +'
 +
 +# Test --stdin
 +
  a=refs/heads/a
  b=refs/heads/b
  c=refs/heads/c
index 270146204140bf32a12a9664817d32615cc11752,6537f30c9e7ed0ef8c05fbdea6b48c25f51ed33d..96fe3754c8c96fd65286d4eea44cb65b0edde343
@@@ -22,6 -22,12 +22,12 @@@ one tagged as v1.0.0.  They all have on
  
  . ./test-lib.sh
  
+ if ! test_have_prereq SHA1
+ then
+       skip_all='not using SHA-1 for objects'
+       test_done
+ fi
  test_expect_success 'blob and tree' '
        test_tick &&
        (
@@@ -361,25 -367,4 +367,25 @@@ test_expect_success 'core.disambiguate 
                git -c core.disambiguate=committish rev-parse $sha1^{tree}
  '
  
 +test_expect_success C_LOCALE_OUTPUT 'ambiguous commits are printed by type first, then hash order' '
 +      test_must_fail git rev-parse 0000 2>stderr &&
 +      grep ^hint: stderr >hints &&
 +      grep 0000 hints >objects &&
 +      cat >expected <<-\EOF &&
 +      tag
 +      commit
 +      tree
 +      blob
 +      EOF
 +      awk "{print \$3}" <objects >objects.types &&
 +      uniq <objects.types >objects.types.uniq &&
 +      test_cmp expected objects.types.uniq &&
 +      for type in tag commit tree blob
 +      do
 +              grep $type objects >$type.objects &&
 +              sort $type.objects >$type.objects.sorted &&
 +              test_cmp $type.objects.sorted $type.objects
 +      done
 +'
 +
  test_done
diff --combined t/t2025-worktree-add.sh
index 224049892423075dd7d03bb15229f4a11302d66f,beaed5e7a19fd2bce7d68f14c5fdac892c51e0fc..d2e49f763263333e8e269051686d3b8b2cae44ba
@@@ -198,25 -198,13 +198,25 @@@ test_expect_success '"add" with <branch
        test_cmp_rev HEAD bat
  '
  
 -test_expect_success '"add" auto-vivify does not clobber existing branch' '
 -      test_commit c1 &&
 -      test_commit c2 &&
 -      git branch precious HEAD~1 &&
 -      test_must_fail git worktree add precious &&
 -      test_cmp_rev HEAD~1 precious &&
 -      test_path_is_missing precious
 +test_expect_success '"add" checks out existing branch of dwimd name' '
 +      git branch dwim HEAD~1 &&
 +      git worktree add dwim &&
 +      test_cmp_rev HEAD~1 dwim &&
 +      (
 +              cd dwim &&
 +              test_cmp_rev HEAD dwim
 +      )
 +'
 +
 +test_expect_success '"add <path>" dwim fails with checked out branch' '
 +      git checkout -b test-branch &&
 +      test_must_fail git worktree add test-branch &&
 +      test_path_is_missing test-branch
 +'
 +
 +test_expect_success '"add --force" with existing dwimd name doesnt die' '
 +      git checkout test-branch &&
 +      git worktree add --force test-branch
  '
  
  test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
@@@ -477,7 -465,7 +477,7 @@@ post_checkout_hook () 
  test_expect_success '"add" invokes post-checkout hook (branch)' '
        post_checkout_hook &&
        {
-               echo $_z40 $(git rev-parse HEAD) 1 &&
+               echo $ZERO_OID $(git rev-parse HEAD) 1 &&
                echo $(pwd)/.git/worktrees/gumby &&
                echo $(pwd)/gumby
        } >hook.expect &&
  test_expect_success '"add" invokes post-checkout hook (detached)' '
        post_checkout_hook &&
        {
-               echo $_z40 $(git rev-parse HEAD) 1 &&
+               echo $ZERO_OID $(git rev-parse HEAD) 1 &&
                echo $(pwd)/.git/worktrees/grumpy &&
                echo $(pwd)/grumpy
        } >hook.expect &&
@@@ -506,7 -494,7 +506,7 @@@ test_expect_success '"add --no-checkout
  test_expect_success '"add" in other worktree invokes post-checkout hook' '
        post_checkout_hook &&
        {
-               echo $_z40 $(git rev-parse HEAD) 1 &&
+               echo $ZERO_OID $(git rev-parse HEAD) 1 &&
                echo $(pwd)/.git/worktrees/guppy &&
                echo $(pwd)/guppy
        } >hook.expect &&
@@@ -518,7 -506,7 +518,7 @@@ test_expect_success '"add" in bare rep
        rm -rf bare &&
        git clone --bare . bare &&
        {
-               echo $_z40 $(git --git-dir=bare rev-parse HEAD) 1 &&
+               echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
                echo $(pwd)/bare/worktrees/goozy &&
                echo $(pwd)/goozy
        } >hook.expect &&
diff --combined t/t4014-format-patch.sh
index 6ea08fd5e9c21bc1efe9726c8745dd1397c3ddab,349029f43b0f02e549ff3212c42eac9043ee8573..028d5507a6b684208ba82292379ec74bc6628574
@@@ -578,7 -578,11 +578,11 @@@ test_expect_success 'excessive subject
  
        rm -rf patches/ &&
        git checkout side &&
+       before=$(git hash-object file) &&
+       before=$(git rev-parse --short $before) &&
        for i in 5 6 1 2 3 A 4 B C 7 8 9 10 D E F; do echo "$i"; done >>file &&
+       after=$(git hash-object file) &&
+       after=$(git rev-parse --short $after) &&
        git update-index file &&
        git commit -m "This is an excessively long subject line for a message due to the habit some projects have of not having a short, one-line subject at the start of the commit message, but rather sticking a whole paragraph right at the start as the only thing in the commit message. It had better not become the filename for the patch." &&
        git format-patch -o patches/ master..side &&
  '
  
  test_expect_success 'cover-letter inherits diff options' '
        git mv file foo &&
        git commit -m foo &&
        git format-patch --no-renames --cover-letter -1 &&
@@@ -616,7 -619,7 +619,7 @@@ test_expect_success 'shortlog of cover-
  '
  
  cat > expect << EOF
- index 40f36c6..2dc5c23 100644
+ index $before..$after 100644
  --- a/file
  +++ b/file
  @@ -13,4 +13,20 @@ C
@@@ -640,7 -643,7 +643,7 @@@ test_expect_success 'format-patch respe
  cat > expect << EOF
  
  diff --git a/file b/file
- index 40f36c6..2dc5c23 100644
+ index $before..$after 100644
  --- a/file
  +++ b/file
  @@ -14,3 +14,19 @@ C
@@@ -1523,14 -1526,14 +1526,14 @@@ test_expect_success 'cover letter auto 
  test_expect_success 'format-patch --zero-commit' '
        git format-patch --zero-commit --stdout v2..v1 >patch2 &&
        grep "^From " patch2 | sort | uniq >actual &&
-       echo "From $_z40 Mon Sep 17 00:00:00 2001" >expect &&
+       echo "From $ZERO_OID Mon Sep 17 00:00:00 2001" >expect &&
        test_cmp expect actual
  '
  
  test_expect_success 'From line has expected format' '
        git format-patch --stdout v2..v1 >patch2 &&
        grep "^From " patch2 >from &&
-       grep "^From $_x40 Mon Sep 17 00:00:00 2001$" patch2 >filtered &&
+       grep "^From $OID_REGEX Mon Sep 17 00:00:00 2001$" patch2 >filtered &&
        test_cmp from filtered
  '
  
@@@ -1661,15 -1664,6 +1664,15 @@@ test_expect_success 'format-patch --bas
        test_write_lines 1 2 >expect &&
        test_cmp expect actual
  '
 +test_expect_success 'format-patch --attach cover-letter only is non-multipart' '
 +      test_when_finished "rm -fr patches" &&
 +      git format-patch -o patches --cover-letter --attach=mimemime --base=HEAD~ -1 &&
 +      ! egrep "^--+mimemime" patches/0000*.patch &&
 +      egrep "^--+mimemime$" patches/0001*.patch >output &&
 +      test_line_count = 2 output &&
 +      egrep "^--+mimemime--$" patches/0001*.patch >output &&
 +      test_line_count = 1 output
 +'
  
  test_expect_success 'format-patch --pretty=mboxrd' '
        sp=" " &&
diff --combined t/t5300-pack-object.sh
index 54eff03851dfcb6ba0d4a41a4d0a221edde43729,9e66637a19e08821124abec704aee85a423e58a3..87a590c4a952d743c2d9f8b0daac76d448f51e8e
@@@ -457,11 -457,6 +457,11 @@@ test_expect_success !PTHREADS,C_LOCALE_
        grep -F "no threads support, ignoring pack.threads" err
  '
  
 +test_expect_success 'pack-objects in too-many-packs mode' '
 +      GIT_TEST_FULL_IN_PACK_ARRAY=1 git repack -ad &&
 +      git fsck
 +'
 +
  #
  # WARNING!
  #
  
  test_expect_success \
      'fake a SHA1 hash collision' \
-     'test -f  .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67 &&
-      cp -f    .git/objects/9d/235ed07cd19811a6ceb342de82f190e49c9f68 \
-               .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67'
+     'long_a=$(git hash-object a | sed -e "s!^..!&/!") &&
+      long_b=$(git hash-object b | sed -e "s!^..!&/!") &&
+      test -f  .git/objects/$long_b &&
+      cp -f    .git/objects/$long_a \
+               .git/objects/$long_b'
  
  test_expect_success \
      'make sure index-pack detects the SHA1 collision' \
diff --combined t/t5516-fetch-push.sh
index 3e8940eee5d5793c4b49b02586ab460b355a0f56,d9c90f2ba57d67fda1db86c8cc56dfecb806ee92..f4d28288f070c131ed4bbe1d38c12eb61dc57973
@@@ -94,9 -94,6 +94,9 @@@ mk_child() 
  }
  
  check_push_result () {
 +      test $# -ge 3 ||
 +      error "bug in the test script: check_push_result requires at least 3 parameters"
 +
        repo_name="$1"
        shift
  
@@@ -556,7 -553,10 +556,7 @@@ test_expect_success 'branch.*.pushremot
  test_expect_success 'push with dry-run' '
  
        mk_test testrepo heads/master &&
 -      (
 -              cd testrepo &&
 -              old_commit=$(git show-ref -s --verify refs/heads/master)
 -      ) &&
 +      old_commit=$(git -C testrepo show-ref -s --verify refs/heads/master) &&
        git push --dry-run testrepo : &&
        check_push_result testrepo $old_commit heads/master
  '
@@@ -612,7 -612,7 +612,7 @@@ test_expect_success 'push does not upda
        chmod +x testrepo/.git/hooks/pre-receive &&
        (
                cd child &&
 -              git pull .. master
 +              git pull .. master &&
                test_must_fail git push &&
                test $(git rev-parse master) != \
                        $(git rev-parse remotes/origin/master)
@@@ -634,7 -634,7 +634,7 @@@ test_expect_success 'pushing valid ref
        orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
        newmaster=$(git show-ref -s --verify refs/heads/master) &&
        orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
-       newnext=$_z40 &&
+       newnext=$ZERO_OID &&
        git push testrepo refs/heads/master:refs/heads/master :refs/heads/next &&
        (
                cd testrepo/.git &&
@@@ -672,15 -672,15 +672,15 @@@ test_expect_success 'deleting dangling 
        (
                cd testrepo/.git &&
                cat >pre-receive.expect <<-EOF &&
-               $_z40 $_z40 refs/heads/master
+               $ZERO_OID $ZERO_OID refs/heads/master
                EOF
  
                cat >update.expect <<-EOF &&
-               refs/heads/master $_z40 $_z40
+               refs/heads/master $ZERO_OID $ZERO_OID
                EOF
  
                cat >post-receive.expect <<-EOF &&
-               $_z40 $_z40 refs/heads/master
+               $ZERO_OID $ZERO_OID refs/heads/master
                EOF
  
                cat >post-update.expect <<-EOF &&
@@@ -703,12 -703,12 +703,12 @@@ test_expect_success 'deletion of a non-
                cd testrepo/.git &&
                cat >pre-receive.expect <<-EOF &&
                $orgmaster $newmaster refs/heads/master
-               $_z40 $_z40 refs/heads/nonexistent
+               $ZERO_OID $ZERO_OID refs/heads/nonexistent
                EOF
  
                cat >update.expect <<-EOF &&
                refs/heads/master $orgmaster $newmaster
-               refs/heads/nonexistent $_z40 $_z40
+               refs/heads/nonexistent $ZERO_OID $ZERO_OID
                EOF
  
                cat >post-receive.expect <<-EOF &&
@@@ -732,11 -732,11 +732,11 @@@ test_expect_success 'deletion of a non-
        (
                cd testrepo/.git &&
                cat >pre-receive.expect <<-EOF &&
-               $_z40 $_z40 refs/heads/nonexistent
+               $ZERO_OID $ZERO_OID refs/heads/nonexistent
                EOF
  
                cat >update.expect <<-EOF &&
-               refs/heads/nonexistent $_z40 $_z40
+               refs/heads/nonexistent $ZERO_OID $ZERO_OID
                EOF
  
                test_cmp pre-receive.expect pre-receive.actual &&
@@@ -751,7 -751,7 +751,7 @@@ test_expect_success 'mixed ref updates
        orgmaster=$(cd testrepo && git show-ref -s --verify refs/heads/master) &&
        newmaster=$(git show-ref -s --verify refs/heads/master) &&
        orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
-       newnext=$_z40 &&
+       newnext=$ZERO_OID &&
        orgpu=$(cd testrepo && git show-ref -s --verify refs/heads/pu) &&
        newpu=$(git show-ref -s --verify refs/heads/master) &&
        git push testrepo refs/heads/master:refs/heads/master \
                $orgmaster $newmaster refs/heads/master
                $orgnext $newnext refs/heads/next
                $orgpu $newpu refs/heads/pu
-               $_z40 $_z40 refs/heads/nonexistent
+               $ZERO_OID $ZERO_OID refs/heads/nonexistent
                EOF
  
                cat >update.expect <<-EOF &&
                refs/heads/master $orgmaster $newmaster
                refs/heads/next $orgnext $newnext
                refs/heads/pu $orgpu $newpu
-               refs/heads/nonexistent $_z40 $_z40
+               refs/heads/nonexistent $ZERO_OID $ZERO_OID
                EOF
  
                cat >post-receive.expect <<-EOF &&
diff --combined t/test-lib.sh
index f3771ab195bad599e908b2ceb1d956fed5213e9b,fed21c3dfc44a00977a9d673ea7cb07415da76ce..28315706be709d28c85785e389b381a8617337e3
@@@ -184,6 -184,8 +184,8 @@@ _x40="$_x35$_x05
  # Zero SHA-1
  _z40=0000000000000000000000000000000000000000
  
+ OID_REGEX="$_x40"
+ ZERO_OID=$_z40
  EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
  
@@@ -195,7 -197,7 +197,7 @@@ LF=
  # when case-folding filenames
  u200c=$(printf '\342\200\214')
  
- export _x05 _x35 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB
+ export _x05 _x35 _x40 _z40 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
  
  # Each test should start with something like this, after copyright notices:
  #
@@@ -1106,7 -1108,12 +1108,7 @@@ test_lazy_prereq UTF8_NFD_TO_NFC 
        auml=$(printf "\303\244")
        aumlcdiar=$(printf "\141\314\210")
        >"$auml" &&
 -      case "$(echo *)" in
 -      "$aumlcdiar")
 -              true ;;
 -      *)
 -              false ;;
 -      esac
 +      test -f "$aumlcdiar"
  '
  
  test_lazy_prereq AUTOIDENT '
@@@ -1207,3 -1214,10 +1209,10 @@@ test_lazy_prereq TIME_T_IS_64BIT 'test-
  test_lazy_prereq CURL '
        curl --version
  '
+ # SHA1 is a test if the hash algorithm in use is SHA-1.  This is both for tests
+ # which will not work with other hash algorithms and tests that work but don't
+ # test anything meaningful (e.g. special values which cause short collisions).
+ test_lazy_prereq SHA1 '
+       test $(git hash-object /dev/null) = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
+ '