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