3 # Copyright (c) 2008 Christian Couder
5 test_description='test git rev-parse --verify'
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19 if [ -f "$_file" ]; then
20 echo "$_line" >> $_file || return $?
21 MSG="Add <$_line> into <$_file>."
23 echo "$_line" > $_file || return $?
24 git add $_file || return $?
25 MSG="Create file <$_file> with <$_line> inside."
29 git commit --quiet -m "$MSG" $_file
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)
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"
63 test_expect_success 'fails with any bad rev or many good revs' '
64 test_must_fail git rev-parse --verify 2>error &&
65 grep "single revision" error &&
66 test_must_fail git rev-parse --verify foo 2>error &&
67 grep "single revision" error &&
68 test_must_fail git rev-parse --verify HEAD bar 2>error &&
69 grep "single revision" error &&
70 test_must_fail git rev-parse --verify baz HEAD 2>error &&
71 grep "single revision" error &&
72 test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
73 grep "single revision" error
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
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
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
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
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)"
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
134 test_expect_success 'main@{n} for various n' '
135 N=$(git reflog | wc -l) &&
138 git rev-parse --verify main@{0} &&
139 git rev-parse --verify main@{1} &&
140 git rev-parse --verify main@{$Nm1} &&
141 test_must_fail git rev-parse --verify main@{$N} &&
142 test_must_fail git rev-parse --verify main@{$Np1}
145 test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
146 ln -s does-not-exist .git/refs/heads/broken &&
147 test_must_fail git rev-parse --verify broken
150 test_expect_success 'options can appear after --verify' '
151 git rev-parse --verify HEAD >expect &&
152 git rev-parse --verify -q HEAD >actual &&
153 test_cmp expect actual
156 test_expect_success 'verify respects --end-of-options' '
157 git update-ref refs/heads/-tricky HEAD &&
158 git rev-parse --verify HEAD >expect &&
159 git rev-parse --verify --end-of-options -tricky >actual &&
160 test_cmp expect actual