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