Merge branch 'bc/hash-algo'
[git] / t / t6120-describe.sh
1 #!/bin/sh
2
3 test_description='test describe
4
5                        B
6         .--------------o----o----o----x
7        /                   /    /
8  o----o----o----o----o----.    /
9        \        A    c        /
10         .------------o---o---o
11                    D,R   e
12 '
13 . ./test-lib.sh
14
15 check_describe () {
16         expect="$1"
17         shift
18         R=$(git describe "$@" 2>err.actual)
19         S=$?
20         cat err.actual >&3
21         test_expect_success "describe $*" '
22         test $S = 0 &&
23         case "$R" in
24         $expect)        echo happy ;;
25         *)      echo "Oops - $R is not $expect";
26                 false ;;
27         esac
28         '
29 }
30
31 test_expect_success setup '
32
33         test_tick &&
34         echo one >file && git add file && git commit -m initial &&
35         one=$(git rev-parse HEAD) &&
36
37         git describe --always HEAD &&
38
39         test_tick &&
40         echo two >file && git add file && git commit -m second &&
41         two=$(git rev-parse HEAD) &&
42
43         test_tick &&
44         echo three >file && git add file && git commit -m third &&
45
46         test_tick &&
47         echo A >file && git add file && git commit -m A &&
48         test_tick &&
49         git tag -a -m A A &&
50
51         test_tick &&
52         echo c >file && git add file && git commit -m c &&
53         test_tick &&
54         git tag c &&
55
56         git reset --hard $two &&
57         test_tick &&
58         echo B >side && git add side && git commit -m B &&
59         test_tick &&
60         git tag -a -m B B &&
61
62         test_tick &&
63         git merge -m Merged c &&
64         merged=$(git rev-parse HEAD) &&
65
66         git reset --hard $two &&
67         test_tick &&
68         echo D >another && git add another && git commit -m D &&
69         test_tick &&
70         git tag -a -m D D &&
71         test_tick &&
72         git tag -a -m R R &&
73
74         test_tick &&
75         echo DD >another && git commit -a -m another &&
76
77         test_tick &&
78         git tag e &&
79
80         test_tick &&
81         echo DDD >another && git commit -a -m "yet another" &&
82
83         test_tick &&
84         git merge -m Merged $merged &&
85
86         test_tick &&
87         echo X >file && echo X >side && git add file side &&
88         git commit -m x
89
90 '
91
92 check_describe A-* HEAD
93 check_describe A-* HEAD^
94 check_describe R-* HEAD^^
95 check_describe A-* HEAD^^2
96 check_describe B HEAD^^2^
97 check_describe R-* HEAD^^^
98
99 check_describe c-* --tags HEAD
100 check_describe c-* --tags HEAD^
101 check_describe e-* --tags HEAD^^
102 check_describe c-* --tags HEAD^^2
103 check_describe B --tags HEAD^^2^
104 check_describe e --tags HEAD^^^
105
106 check_describe heads/master --all HEAD
107 check_describe tags/c-* --all HEAD^
108 check_describe tags/e --all HEAD^^^
109
110 check_describe B-0-* --long HEAD^^2^
111 check_describe A-3-* --long HEAD^^2
112
113 check_describe c-7-* --tags
114 check_describe e-3-* --first-parent --tags
115
116 test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
117         echo "A^0" >expect &&
118         git checkout A &&
119         test_when_finished "git checkout -" &&
120         git describe --contains >actual &&
121         test_cmp expect actual
122 '
123
124 : >err.expect
125 check_describe tags/A --all A^0
126 test_expect_success 'no warning was displayed for A' '
127         test_cmp err.expect err.actual
128 '
129
130 test_expect_success 'rename tag A to Q locally' '
131         mv .git/refs/tags/A .git/refs/tags/Q
132 '
133 cat - >err.expect <<EOF
134 warning: tag 'A' is really 'Q' here
135 EOF
136 check_describe A-* HEAD
137 test_expect_success 'warning was displayed for Q' '
138         test_i18ncmp err.expect err.actual
139 '
140 test_expect_success 'rename tag Q back to A' '
141         mv .git/refs/tags/Q .git/refs/tags/A
142 '
143
144 test_expect_success 'pack tag refs' 'git pack-refs'
145 check_describe A-* HEAD
146
147 check_describe "A-*[0-9a-f]" --dirty
148
149 test_expect_success 'set-up dirty work tree' '
150         echo >>file
151 '
152
153 check_describe "A-*[0-9a-f]-dirty" --dirty
154
155 check_describe "A-*[0-9a-f].mod" --dirty=.mod
156
157 test_expect_success 'describe --dirty HEAD' '
158         test_must_fail git describe --dirty HEAD
159 '
160
161 test_expect_success 'set-up matching pattern tests' '
162         git tag -a -m test-annotated test-annotated &&
163         echo >>file &&
164         test_tick &&
165         git commit -a -m "one more" &&
166         git tag test1-lightweight &&
167         echo >>file &&
168         test_tick &&
169         git commit -a -m "yet another" &&
170         git tag test2-lightweight &&
171         echo >>file &&
172         test_tick &&
173         git commit -a -m "even more"
174
175 '
176
177 check_describe "test-annotated-*" --match="test-*"
178
179 check_describe "test1-lightweight-*" --tags --match="test1-*"
180
181 check_describe "test2-lightweight-*" --tags --match="test2-*"
182
183 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
184
185 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
186
187 check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
188
189 check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
190
191 check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
192
193 test_expect_success 'set-up branches' '
194         git branch branch_A A &&
195         git branch branch_C c &&
196         git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
197         git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
198         git update-ref refs/original/original_branch_A test-annotated~2
199 '
200
201 check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
202
203 check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
204
205 check_describe "original/original_branch_A*" --all test-annotated~1
206
207 test_expect_success '--match does not work for other types' '
208         test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
209 '
210
211 test_expect_success '--exclude does not work for other types' '
212         R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
213         case "$R" in
214         *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
215                 false;;
216         *) echo ok: Found some known type;;
217         esac
218 '
219
220 test_expect_success 'name-rev with exact tags' '
221         echo A >expect &&
222         tag_object=$(git rev-parse refs/tags/A) &&
223         git name-rev --tags --name-only $tag_object >actual &&
224         test_cmp expect actual &&
225
226         echo "A^0" >expect &&
227         tagged_commit=$(git rev-parse "refs/tags/A^0") &&
228         git name-rev --tags --name-only $tagged_commit >actual &&
229         test_cmp expect actual
230 '
231
232 test_expect_success 'name-rev --all' '
233         >expect.unsorted &&
234         for rev in $(git rev-list --all)
235         do
236                 git name-rev $rev >>expect.unsorted
237         done &&
238         sort <expect.unsorted >expect &&
239         git name-rev --all >actual.unsorted &&
240         sort <actual.unsorted >actual &&
241         test_cmp expect actual
242 '
243
244 test_expect_success 'name-rev --stdin' '
245         >expect.unsorted &&
246         for rev in $(git rev-list --all)
247         do
248                 name=$(git name-rev --name-only $rev) &&
249                 echo "$rev ($name)" >>expect.unsorted
250         done &&
251         sort <expect.unsorted >expect &&
252         git rev-list --all | git name-rev --stdin >actual.unsorted &&
253         sort <actual.unsorted >actual &&
254         test_cmp expect actual
255 '
256
257 test_expect_success 'describe --contains with the exact tags' '
258         echo "A^0" >expect &&
259         tag_object=$(git rev-parse refs/tags/A) &&
260         git describe --contains $tag_object >actual &&
261         test_cmp expect actual &&
262
263         echo "A^0" >expect &&
264         tagged_commit=$(git rev-parse "refs/tags/A^0") &&
265         git describe --contains $tagged_commit >actual &&
266         test_cmp expect actual
267 '
268
269 test_expect_success 'describe --contains and --match' '
270         echo "A^0" >expect &&
271         tagged_commit=$(git rev-parse "refs/tags/A^0") &&
272         test_must_fail git describe --contains --match="B" $tagged_commit &&
273         git describe --contains --match="B" --match="A" $tagged_commit >actual &&
274         test_cmp expect actual
275 '
276
277 test_expect_success 'describe --exclude' '
278         echo "c~1" >expect &&
279         tagged_commit=$(git rev-parse "refs/tags/A^0") &&
280         test_must_fail git describe --contains --match="B" $tagged_commit &&
281         git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
282         test_cmp expect actual
283 '
284
285 test_expect_success 'describe --contains and --no-match' '
286         echo "A^0" >expect &&
287         tagged_commit=$(git rev-parse "refs/tags/A^0") &&
288         git describe --contains --match="B" --no-match $tagged_commit >actual &&
289         test_cmp expect actual
290 '
291
292 test_expect_success 'setup and absorb a submodule' '
293         test_create_repo sub1 &&
294         test_commit -C sub1 initial &&
295         git submodule add ./sub1 &&
296         git submodule absorbgitdirs &&
297         git commit -a -m "add submodule" &&
298         git describe --dirty >expect &&
299         git describe --broken >out &&
300         test_cmp expect out
301 '
302
303 test_expect_success 'describe chokes on severely broken submodules' '
304         mv .git/modules/sub1/ .git/modules/sub_moved &&
305         test_must_fail git describe --dirty
306 '
307 test_expect_success 'describe ignoring a broken submodule' '
308         git describe --broken >out &&
309         test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
310         grep broken out
311 '
312
313 test_expect_success 'describe a blob at a directly tagged commit' '
314         echo "make it a unique blob" >file &&
315         git add file && git commit -m "content in file" &&
316         git tag -a -m "latest annotated tag" unique-file &&
317         git describe HEAD:file >actual &&
318         echo "unique-file:file" >expect &&
319         test_cmp expect actual
320 '
321
322 test_expect_success 'describe a blob with its first introduction' '
323         git commit --allow-empty -m "empty commit" &&
324         git rm file &&
325         git commit -m "delete blob" &&
326         git revert HEAD &&
327         git commit --allow-empty -m "empty commit" &&
328         git describe HEAD:file >actual &&
329         echo "unique-file:file" >expect &&
330         test_cmp expect actual
331 '
332
333 test_expect_success 'describe directly tagged blob' '
334         git tag test-blob unique-file:file &&
335         git describe test-blob >actual &&
336         echo "unique-file:file" >expect &&
337         # suboptimal: we rather want to see "test-blob"
338         test_cmp expect actual
339 '
340
341 test_expect_success 'describe tag object' '
342         git tag test-blob-1 -a -m msg unique-file:file &&
343         test_must_fail git describe test-blob-1 2>actual &&
344         test_i18ngrep "fatal: test-blob-1 is neither a commit nor blob" actual
345 '
346
347 test_expect_failure ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
348         i=1 &&
349         while test $i -lt 8000
350         do
351                 echo "commit refs/heads/master
352 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
353 data <<EOF
354 commit #$i
355 EOF"
356                 test $i = 1 && echo "from refs/heads/master^0"
357                 i=$(($i + 1))
358         done | git fast-import &&
359         git checkout master &&
360         git tag far-far-away HEAD^ &&
361         echo "HEAD~4000 tags/far-far-away~3999" >expect &&
362         git name-rev HEAD~4000 >actual &&
363         test_cmp expect actual &&
364         run_with_limited_stack git name-rev HEAD~4000 >actual &&
365         test_cmp expect actual
366 '
367
368 test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
369         git tag -f far-far-away HEAD~7999 &&
370         echo "far-far-away" >expect &&
371         git describe --tags --abbrev=0 HEAD~4000 >actual &&
372         test_cmp expect actual &&
373         run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
374         test_cmp expect actual
375 '
376
377 check_describe tags/A --all A
378 check_describe tags/c --all c
379 check_describe heads/branch_A --all --match='branch_*' branch_A
380
381 test_done