| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2008 Christian Couder |
| 4 | # |
| 5 | test_description='test git rev-parse --verify' |
| 6 | |
| 7 | exec </dev/null |
| 8 | |
| 9 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 10 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 11 | |
| 12 | . ./test-lib.sh |
| 13 | |
| 14 | add_line_into_file() |
| 15 | { |
| 16 | _line=$1 |
| 17 | _file=$2 |
| 18 | |
| 19 | if [ -f "$_file" ]; then |
| 20 | echo "$_line" >> $_file || return $? |
| 21 | MSG="Add <$_line> into <$_file>." |
| 22 | else |
| 23 | echo "$_line" > $_file || return $? |
| 24 | git add $_file || return $? |
| 25 | MSG="Create file <$_file> with <$_line> inside." |
| 26 | fi |
| 27 | |
| 28 | test_tick |
| 29 | git commit --quiet -m "$MSG" $_file |
| 30 | } |
| 31 | |
| 32 | HASH1= |
| 33 | HASH2= |
| 34 | HASH3= |
| 35 | HASH4= |
| 36 | |
| 37 | test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' ' |
| 38 | add_line_into_file "1: Hello World" hello && |
| 39 | HASH1=$(git rev-parse --verify HEAD) && |
| 40 | add_line_into_file "2: A new day for git" hello && |
| 41 | HASH2=$(git rev-parse --verify HEAD) && |
| 42 | add_line_into_file "3: Another new day for git" hello && |
| 43 | HASH3=$(git rev-parse --verify HEAD) && |
| 44 | add_line_into_file "4: Ciao for now" hello && |
| 45 | HASH4=$(git rev-parse --verify HEAD) |
| 46 | ' |
| 47 | |
| 48 | test_expect_success 'works with one good rev' ' |
| 49 | rev_hash1=$(git rev-parse --verify $HASH1) && |
| 50 | test "$rev_hash1" = "$HASH1" && |
| 51 | rev_hash2=$(git rev-parse --verify $HASH2) && |
| 52 | test "$rev_hash2" = "$HASH2" && |
| 53 | rev_hash3=$(git rev-parse --verify $HASH3) && |
| 54 | test "$rev_hash3" = "$HASH3" && |
| 55 | rev_hash4=$(git rev-parse --verify $HASH4) && |
| 56 | test "$rev_hash4" = "$HASH4" && |
| 57 | rev_main=$(git rev-parse --verify main) && |
| 58 | test "$rev_main" = "$HASH4" && |
| 59 | rev_head=$(git rev-parse --verify HEAD) && |
| 60 | test "$rev_head" = "$HASH4" |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'fails with any bad rev or many good revs' ' |
| 64 | test_must_fail git rev-parse --verify 2>error && |
| 65 | test_grep "single revision" error && |
| 66 | test_must_fail git rev-parse --verify foo 2>error && |
| 67 | test_grep "single revision" error && |
| 68 | test_must_fail git rev-parse --verify HEAD bar 2>error && |
| 69 | test_grep "single revision" error && |
| 70 | test_must_fail git rev-parse --verify baz HEAD 2>error && |
| 71 | test_grep "single revision" error && |
| 72 | test_must_fail git rev-parse --verify $HASH2 HEAD 2>error && |
| 73 | test_grep "single revision" error |
| 74 | ' |
| 75 | |
| 76 | test_expect_success 'fails silently when using -q' ' |
| 77 | test_must_fail git rev-parse --verify --quiet 2>error && |
| 78 | test_must_be_empty error && |
| 79 | test_must_fail git rev-parse -q --verify foo 2>error && |
| 80 | test_must_be_empty error && |
| 81 | test_must_fail git rev-parse --verify -q HEAD bar 2>error && |
| 82 | test_must_be_empty error && |
| 83 | test_must_fail git rev-parse --quiet --verify baz HEAD 2>error && |
| 84 | test_must_be_empty error && |
| 85 | test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error && |
| 86 | test_must_be_empty error |
| 87 | ' |
| 88 | |
| 89 | test_expect_success 'fails silently when using -q with deleted reflogs' ' |
| 90 | ref=$(git rev-parse HEAD) && |
| 91 | git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" && |
| 92 | git reflog delete --updateref --rewrite refs/test@{1} && |
| 93 | test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 && |
| 94 | test_must_be_empty error |
| 95 | ' |
| 96 | |
| 97 | test_expect_success 'fails silently when using -q with not enough reflogs' ' |
| 98 | ref=$(git rev-parse HEAD) && |
| 99 | git update-ref --create-reflog -m "message for refs/test2" refs/test2 "$ref" && |
| 100 | test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 && |
| 101 | test_must_be_empty error |
| 102 | ' |
| 103 | |
| 104 | test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' ' |
| 105 | ref=$(git rev-parse HEAD) && |
| 106 | git update-ref --create-reflog -m "message for refs/test3" refs/test3 "$ref" && |
| 107 | git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error && |
| 108 | test_must_be_empty error && |
| 109 | echo "$ref" >expect && |
| 110 | test_cmp expect actual |
| 111 | ' |
| 112 | |
| 113 | test_expect_success 'no stdout output on error' ' |
| 114 | test -z "$(git rev-parse --verify)" && |
| 115 | test -z "$(git rev-parse --verify foo)" && |
| 116 | test -z "$(git rev-parse --verify baz HEAD)" && |
| 117 | test -z "$(git rev-parse --verify HEAD bar)" && |
| 118 | test -z "$(git rev-parse --verify $HASH2 HEAD)" |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'use --default' ' |
| 122 | git rev-parse --verify --default main && |
| 123 | git rev-parse --verify --default main HEAD && |
| 124 | git rev-parse --default main --verify && |
| 125 | git rev-parse --default main --verify HEAD && |
| 126 | git rev-parse --verify HEAD --default main && |
| 127 | test_must_fail git rev-parse --verify foo --default main && |
| 128 | test_must_fail git rev-parse --default HEAD --verify bar && |
| 129 | test_must_fail git rev-parse --verify --default HEAD baz && |
| 130 | test_must_fail git rev-parse --default foo --verify && |
| 131 | test_must_fail git rev-parse --verify --default bar |
| 132 | ' |
| 133 | |
| 134 | test_expect_success 'main@{n} for various n' ' |
| 135 | git reflog >out && |
| 136 | N=$(wc -l <out) && |
| 137 | Nm1=$(($N-1)) && |
| 138 | Np1=$(($N+1)) && |
| 139 | git rev-parse --verify main@{0} && |
| 140 | git rev-parse --verify main@{1} && |
| 141 | git rev-parse --verify main@{$Nm1} && |
| 142 | test_must_fail git rev-parse --verify main@{$N} && |
| 143 | test_must_fail git rev-parse --verify main@{$Np1} |
| 144 | ' |
| 145 | |
| 146 | test_expect_success 'options can appear after --verify' ' |
| 147 | git rev-parse --verify HEAD >expect && |
| 148 | git rev-parse --verify -q HEAD >actual && |
| 149 | test_cmp expect actual |
| 150 | ' |
| 151 | |
| 152 | test_expect_success 'verify respects --end-of-options' ' |
| 153 | git update-ref refs/heads/-tricky HEAD && |
| 154 | git rev-parse --verify HEAD >expect && |
| 155 | git rev-parse --verify --end-of-options -tricky >actual && |
| 156 | test_cmp expect actual |
| 157 | ' |
| 158 | |
| 159 | test_done |