t4013: add tests for --diff-merges=first-parent
[git] / t / t4013-diff-various.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='Various diff formatting options'
7
8 . ./test-lib.sh
9
10 test_expect_success setup '
11
12         GIT_AUTHOR_DATE="2006-06-26 00:00:00 +0000" &&
13         GIT_COMMITTER_DATE="2006-06-26 00:00:00 +0000" &&
14         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
15
16         mkdir dir &&
17         mkdir dir2 &&
18         for i in 1 2 3; do echo $i; done >file0 &&
19         for i in A B; do echo $i; done >dir/sub &&
20         cat file0 >file2 &&
21         git add file0 file2 dir/sub &&
22         git commit -m Initial &&
23
24         git branch initial &&
25         git branch side &&
26
27         GIT_AUTHOR_DATE="2006-06-26 00:01:00 +0000" &&
28         GIT_COMMITTER_DATE="2006-06-26 00:01:00 +0000" &&
29         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
30
31         for i in 4 5 6; do echo $i; done >>file0 &&
32         for i in C D; do echo $i; done >>dir/sub &&
33         rm -f file2 &&
34         git update-index --remove file0 file2 dir/sub &&
35         git commit -m "Second${LF}${LF}This is the second commit." &&
36
37         GIT_AUTHOR_DATE="2006-06-26 00:02:00 +0000" &&
38         GIT_COMMITTER_DATE="2006-06-26 00:02:00 +0000" &&
39         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
40
41         for i in A B C; do echo $i; done >file1 &&
42         git add file1 &&
43         for i in E F; do echo $i; done >>dir/sub &&
44         git update-index dir/sub &&
45         git commit -m Third &&
46
47         GIT_AUTHOR_DATE="2006-06-26 00:03:00 +0000" &&
48         GIT_COMMITTER_DATE="2006-06-26 00:03:00 +0000" &&
49         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
50
51         git checkout side &&
52         for i in A B C; do echo $i; done >>file0 &&
53         for i in 1 2; do echo $i; done >>dir/sub &&
54         cat dir/sub >file3 &&
55         git add file3 &&
56         git update-index file0 dir/sub &&
57         git commit -m Side &&
58
59         GIT_AUTHOR_DATE="2006-06-26 00:04:00 +0000" &&
60         GIT_COMMITTER_DATE="2006-06-26 00:04:00 +0000" &&
61         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
62
63         git checkout master &&
64         git pull -s ours . side &&
65
66         GIT_AUTHOR_DATE="2006-06-26 00:05:00 +0000" &&
67         GIT_COMMITTER_DATE="2006-06-26 00:05:00 +0000" &&
68         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
69
70         for i in A B C; do echo $i; done >>file0 &&
71         for i in 1 2; do echo $i; done >>dir/sub &&
72         git update-index file0 dir/sub &&
73
74         mkdir dir3 &&
75         cp dir/sub dir3/sub &&
76         test-tool chmtime +1 dir3/sub &&
77
78         git config log.showroot false &&
79         git commit --amend &&
80
81         GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
82         GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
83         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
84         git checkout -b rearrange initial &&
85         for i in B A; do echo $i; done >dir/sub &&
86         git add dir/sub &&
87         git commit -m "Rearranged lines in dir/sub" &&
88         git checkout master &&
89
90         GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
91         GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
92         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
93         git checkout -b mode initial &&
94         git update-index --chmod=+x file0 &&
95         git commit -m "update mode" &&
96         git checkout -f master &&
97
98         GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
99         GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
100         export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
101         git checkout -b note initial &&
102         git update-index --chmod=+x file2 &&
103         git commit -m "update mode (file2)" &&
104         git notes add -m "note" &&
105         git checkout -f master &&
106
107         # Same merge as master, but with parents reversed. Hide it in a
108         # pseudo-ref to avoid impacting tests with --all.
109         commit=$(echo reverse |
110                  git commit-tree -p master^2 -p master^1 master^{tree}) &&
111         git update-ref REVERSE $commit &&
112
113         git config diff.renames false &&
114
115         git show-branch
116 '
117
118 : <<\EOF
119 ! [initial] Initial
120  * [master] Merge branch 'side'
121   ! [rearrange] Rearranged lines in dir/sub
122    ! [side] Side
123 ----
124   +  [rearrange] Rearranged lines in dir/sub
125  -   [master] Merge branch 'side'
126  * + [side] Side
127  *   [master^] Third
128  *   [master~2] Second
129 +*++ [initial] Initial
130 EOF
131
132 process_diffs () {
133         perl -e '
134                 my $oid_length = length($ARGV[0]);
135                 my $x40 = "[0-9a-f]{40}";
136                 my $xab = "[0-9a-f]{4,16}";
137                 my $orx = "[0-9a-f]" x $oid_length;
138
139                 sub munge_oid {
140                         my ($oid) = @_;
141                         my $x;
142
143                         return "" unless length $oid;
144
145                         if ($oid =~ /^(100644|100755|120000)$/) {
146                                 return $oid;
147                         }
148
149                         if ($oid =~ /^0*$/) {
150                                 $x = "0";
151                         } else {
152                                 $x = "f";
153                         }
154
155                         if (length($oid) == 40) {
156                                 return $x x $oid_length;
157                         } else {
158                                 return $x x length($oid);
159                         }
160                 }
161
162                 while (<STDIN>) {
163                         s/($orx)/munge_oid($1)/ge;
164                         s/From ($x40)( |\))/"From " . munge_oid($1) . $2/ge;
165                         s/commit ($x40)($| \(from )($x40?)/"commit " .  munge_oid($1) . $2 . munge_oid($3)/ge;
166                         s/\b($x40)( |\.\.|$)/munge_oid($1) . $2/ge;
167                         s/^($x40)($| )/munge_oid($1) . $2/e;
168                         s/($xab)(\.\.|,| |\.\.\.|$)/munge_oid($1) . $2/ge;
169                         print;
170                 }
171         ' "$ZERO_OID" <"$1"
172 }
173
174 V=$(git version | sed -e 's/^git version //' -e 's/\./\\./g')
175 while read magic cmd
176 do
177         status=success
178         case "$magic" in
179         '' | '#'*)
180                 continue ;;
181         :*)
182                 magic=${magic#:}
183                 label="$magic-$cmd"
184                 case "$magic" in
185                 noellipses) ;;
186                 failure)
187                         status=failure
188                         magic=
189                         label="$cmd" ;;
190                 *)
191                         BUG "unknown magic $magic" ;;
192                 esac ;;
193         *)
194                 cmd="$magic $cmd" magic=
195                 label="$cmd" ;;
196         esac
197         test=$(echo "$label" | sed -e 's|[/ ][/ ]*|_|g')
198         pfx=$(printf "%04d" $test_count)
199         expect="$TEST_DIRECTORY/t4013/diff.$test"
200         actual="$pfx-diff.$test"
201
202         test_expect_$status "git $cmd # magic is ${magic:-(not used)}" '
203                 {
204                         echo "$ git $cmd"
205                         case "$magic" in
206                         "")
207                                 GIT_PRINT_SHA1_ELLIPSIS=yes git $cmd ;;
208                         noellipses)
209                                 git $cmd ;;
210                         esac |
211                         sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
212                             -e "s/^\\(.*mixed; boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
213                         echo "\$"
214                 } >"$actual" &&
215                 if test -f "$expect"
216                 then
217                         process_diffs "$actual" >actual &&
218                         process_diffs "$expect" >expect &&
219                         case $cmd in
220                         *format-patch* | *-stat*)
221                                 test_i18ncmp expect actual;;
222                         *)
223                                 test_cmp expect actual;;
224                         esac &&
225                         rm -f "$actual" actual expect
226                 else
227                         # this is to help developing new tests.
228                         cp "$actual" "$expect"
229                         false
230                 fi
231         '
232 done <<\EOF
233 diff-tree initial
234 diff-tree -r initial
235 diff-tree -r --abbrev initial
236 diff-tree -r --abbrev=4 initial
237 diff-tree --root initial
238 diff-tree --root --abbrev initial
239 :noellipses diff-tree --root --abbrev initial
240 diff-tree --root -r initial
241 diff-tree --root -r --abbrev initial
242 :noellipses diff-tree --root -r --abbrev initial
243 diff-tree --root -r --abbrev=4 initial
244 :noellipses diff-tree --root -r --abbrev=4 initial
245 diff-tree -p initial
246 diff-tree --root -p initial
247 diff-tree --root -p --abbrev=10 initial
248 diff-tree --root -p --full-index initial
249 diff-tree --root -p --full-index --abbrev=10 initial
250 diff-tree --patch-with-stat initial
251 diff-tree --root --patch-with-stat initial
252 diff-tree --patch-with-raw initial
253 diff-tree --root --patch-with-raw initial
254
255 diff-tree --pretty initial
256 diff-tree --pretty --root initial
257 diff-tree --pretty -p initial
258 diff-tree --pretty --stat initial
259 diff-tree --pretty --summary initial
260 diff-tree --pretty --stat --summary initial
261 diff-tree --pretty --root -p initial
262 diff-tree --pretty --root --stat initial
263 # improved by Timo's patch
264 diff-tree --pretty --root --summary initial
265 # improved by Timo's patch
266 diff-tree --pretty --root --summary -r initial
267 diff-tree --pretty --root --stat --summary initial
268 diff-tree --pretty --patch-with-stat initial
269 diff-tree --pretty --root --patch-with-stat initial
270 diff-tree --pretty --patch-with-raw initial
271 diff-tree --pretty --root --patch-with-raw initial
272
273 diff-tree --pretty=oneline initial
274 diff-tree --pretty=oneline --root initial
275 diff-tree --pretty=oneline -p initial
276 diff-tree --pretty=oneline --root -p initial
277 diff-tree --pretty=oneline --patch-with-stat initial
278 # improved by Timo's patch
279 diff-tree --pretty=oneline --root --patch-with-stat initial
280 diff-tree --pretty=oneline --patch-with-raw initial
281 diff-tree --pretty=oneline --root --patch-with-raw initial
282
283 diff-tree --pretty side
284 diff-tree --pretty -p side
285 diff-tree --pretty --patch-with-stat side
286
287 diff-tree initial mode
288 diff-tree --stat initial mode
289 diff-tree --summary initial mode
290
291 diff-tree master
292 diff-tree -p master
293 diff-tree -p -m master
294 diff-tree -c master
295 diff-tree -c --abbrev master
296 :noellipses diff-tree -c --abbrev master
297 diff-tree --cc master
298 # stat only should show the diffstat with the first parent
299 diff-tree -c --stat master
300 diff-tree --cc --stat master
301 diff-tree -c --stat --summary master
302 diff-tree --cc --stat --summary master
303 # stat summary should show the diffstat and summary with the first parent
304 diff-tree -c --stat --summary side
305 diff-tree --cc --stat --summary side
306 diff-tree --cc --shortstat master
307 diff-tree --cc --summary REVERSE
308 # improved by Timo's patch
309 diff-tree --cc --patch-with-stat master
310 # improved by Timo's patch
311 diff-tree --cc --patch-with-stat --summary master
312 # this is correct
313 diff-tree --cc --patch-with-stat --summary side
314
315 log master
316 log -p master
317 log --root master
318 log --root -p master
319 log --patch-with-stat master
320 log --root --patch-with-stat master
321 log --root --patch-with-stat --summary master
322 # improved by Timo's patch
323 log --root -c --patch-with-stat --summary master
324 # improved by Timo's patch
325 log --root --cc --patch-with-stat --summary master
326 log --no-diff-merges -p --first-parent master
327 log --diff-merges=off -p --first-parent master
328 log --first-parent --diff-merges=off -p master
329 log -p --first-parent master
330 log -p --diff-merges=first-parent master
331 log --diff-merges=first-parent master
332 log -m -p --first-parent master
333 log -m -p master
334 log --cc -m -p master
335 log -c -m -p master
336 log -SF master
337 log -S F master
338 log -SF -p master
339 log -SF master --max-count=0
340 log -SF master --max-count=1
341 log -SF master --max-count=2
342 log -GF master
343 log -GF -p master
344 log -GF -p --pickaxe-all master
345 log --decorate --all
346 log --decorate=full --all
347
348 rev-list --parents HEAD
349 rev-list --children HEAD
350
351 whatchanged master
352 :noellipses whatchanged master
353 whatchanged -p master
354 whatchanged --root master
355 :noellipses whatchanged --root master
356 whatchanged --root -p master
357 whatchanged --patch-with-stat master
358 whatchanged --root --patch-with-stat master
359 whatchanged --root --patch-with-stat --summary master
360 # improved by Timo's patch
361 whatchanged --root -c --patch-with-stat --summary master
362 # improved by Timo's patch
363 whatchanged --root --cc --patch-with-stat --summary master
364 whatchanged -SF master
365 :noellipses whatchanged -SF master
366 whatchanged -SF -p master
367
368 log --patch-with-stat master -- dir/
369 whatchanged --patch-with-stat master -- dir/
370 log --patch-with-stat --summary master -- dir/
371 whatchanged --patch-with-stat --summary master -- dir/
372
373 show initial
374 show --root initial
375 show side
376 show master
377 show -c master
378 show -m master
379 show --first-parent master
380 show --stat side
381 show --stat --summary side
382 show --patch-with-stat side
383 show --patch-with-raw side
384 :noellipses show --patch-with-raw side
385 show --patch-with-stat --summary side
386
387 format-patch --stdout initial..side
388 format-patch --stdout initial..master^
389 format-patch --stdout initial..master
390 format-patch --stdout --no-numbered initial..master
391 format-patch --stdout --numbered initial..master
392 format-patch --attach --stdout initial..side
393 format-patch --attach --stdout --suffix=.diff initial..side
394 format-patch --attach --stdout initial..master^
395 format-patch --attach --stdout initial..master
396 format-patch --inline --stdout initial..side
397 format-patch --inline --stdout initial..master^
398 format-patch --inline --stdout --numbered-files initial..master
399 format-patch --inline --stdout initial..master
400 format-patch --inline --stdout --subject-prefix=TESTCASE initial..master
401 config format.subjectprefix DIFFERENT_PREFIX
402 format-patch --inline --stdout initial..master^^
403 format-patch --stdout --cover-letter -n initial..master^
404
405 diff --abbrev initial..side
406 diff -U initial..side
407 diff -U1 initial..side
408 diff -r initial..side
409 diff --stat initial..side
410 diff -r --stat initial..side
411 diff initial..side
412 diff --patch-with-stat initial..side
413 diff --patch-with-raw initial..side
414 :noellipses diff --patch-with-raw initial..side
415 diff --patch-with-stat -r initial..side
416 diff --patch-with-raw -r initial..side
417 :noellipses diff --patch-with-raw -r initial..side
418 diff --name-status dir2 dir
419 diff --no-index --name-status dir2 dir
420 diff --no-index --name-status -- dir2 dir
421 diff --no-index dir dir3
422 diff master master^ side
423 # Can't use spaces...
424 diff --line-prefix=abc master master^ side
425 diff --dirstat master~1 master~2
426 diff --dirstat initial rearrange
427 diff --dirstat-by-file initial rearrange
428 diff --dirstat --cc master~1 master
429 # No-index --abbrev and --no-abbrev
430 diff --raw initial
431 :noellipses diff --raw initial
432 diff --raw --abbrev=4 initial
433 :noellipses diff --raw --abbrev=4 initial
434 diff --raw --no-abbrev initial
435 diff --no-index --raw dir2 dir
436 :noellipses diff --no-index --raw dir2 dir
437 diff --no-index --raw --abbrev=4 dir2 dir
438 :noellipses diff --no-index --raw --abbrev=4 dir2 dir
439 diff --no-index --raw --no-abbrev dir2 dir
440
441 diff-tree --pretty --root --stat --compact-summary initial
442 diff-tree --pretty -R --root --stat --compact-summary initial
443 diff-tree --pretty note
444 diff-tree --pretty --notes note
445 diff-tree --format=%N note
446 diff-tree --stat --compact-summary initial mode
447 diff-tree -R --stat --compact-summary initial mode
448 EOF
449
450 test_expect_success 'log -S requires an argument' '
451         test_must_fail git log -S
452 '
453
454 test_expect_success 'diff --cached on unborn branch' '
455         echo ref: refs/heads/unborn >.git/HEAD &&
456         git diff --cached >result &&
457         process_diffs result >actual &&
458         process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached" >expected &&
459         test_cmp expected actual
460 '
461
462 test_expect_success 'diff --cached -- file on unborn branch' '
463         git diff --cached -- file0 >result &&
464         process_diffs result >actual &&
465         process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" >expected &&
466         test_cmp expected actual
467 '
468 test_expect_success 'diff --line-prefix with spaces' '
469         git diff --line-prefix="| | | " --cached -- file0 >result &&
470         process_diffs result >actual &&
471         process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" >expected &&
472         test_cmp expected actual
473 '
474
475 test_expect_success 'diff-tree --stdin with log formatting' '
476         cat >expect <<-\EOF &&
477         Side
478         Third
479         Second
480         EOF
481         git rev-list master | git diff-tree --stdin --format=%s -s >actual &&
482         test_cmp expect actual
483 '
484
485 test_done