3 test_description='range-diff tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 # Note that because of the range-diff's heuristics, test_commit does more
11 # harm than good. We need some real history.
13 test_expect_success 'setup' '
14 git fast-import <"$TEST_DIRECTORY"/t3206/history.export &&
15 test_oid_cache <<-\EOF
126 # Empty delimiter (included so lines match neatly)
132 test_expect_success 'simple A..B A..C (unmodified)' '
133 git range-diff --no-color main..topic main..unmodified \
135 cat >expect <<-EOF &&
136 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
137 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
138 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
139 4: $(test_oid t4) = 4: $(test_oid u4) s/12/B/
141 test_cmp expect actual
144 test_expect_success 'simple B...C (unmodified)' '
145 git range-diff --no-color topic...unmodified >actual &&
146 # same "expect" as above
147 test_cmp expect actual
150 test_expect_success 'simple A B C (unmodified)' '
151 git range-diff --no-color main topic unmodified >actual &&
152 # same "expect" as above
153 test_cmp expect actual
156 test_expect_success 'A^! and A^-<n> (unmodified)' '
157 git range-diff --no-color topic^! unmodified^-1 >actual &&
158 cat >expect <<-EOF &&
159 1: $(test_oid t4) = 1: $(test_oid u4) s/12/B/
161 test_cmp expect actual
164 test_expect_success 'A^{/..} is not mistaken for a range' '
165 test_must_fail git range-diff topic^.. topic^{/..} 2>error &&
166 test_i18ngrep "not a commit range" error
169 test_expect_success 'trivial reordering' '
170 git range-diff --no-color main topic reordered >actual &&
171 cat >expect <<-EOF &&
172 1: $(test_oid t1) = 1: $(test_oid r1) s/5/A/
173 3: $(test_oid t3) = 2: $(test_oid r2) s/11/B/
174 4: $(test_oid t4) = 3: $(test_oid r3) s/12/B/
175 2: $(test_oid t2) = 4: $(test_oid r4) s/4/A/
177 test_cmp expect actual
180 test_expect_success 'removed a commit' '
181 git range-diff --no-color main topic removed >actual &&
182 cat >expect <<-EOF &&
183 1: $(test_oid t1) = 1: $(test_oid d1) s/5/A/
184 2: $(test_oid t2) < -: $(test_oid __) s/4/A/
185 3: $(test_oid t3) = 2: $(test_oid d2) s/11/B/
186 4: $(test_oid t4) = 3: $(test_oid d3) s/12/B/
188 test_cmp expect actual
191 test_expect_success 'added a commit' '
192 git range-diff --no-color main topic added >actual &&
193 cat >expect <<-EOF &&
194 1: $(test_oid t1) = 1: $(test_oid a1) s/5/A/
195 2: $(test_oid t2) = 2: $(test_oid a2) s/4/A/
196 -: $(test_oid __) > 3: $(test_oid a3) s/6/A/
197 3: $(test_oid t3) = 4: $(test_oid a4) s/11/B/
198 4: $(test_oid t4) = 5: $(test_oid a5) s/12/B/
200 test_cmp expect actual
203 test_expect_success 'new base, A B C' '
204 git range-diff --no-color main topic rebased >actual &&
205 cat >expect <<-EOF &&
206 1: $(test_oid t1) = 1: $(test_oid b1) s/5/A/
207 2: $(test_oid t2) = 2: $(test_oid b2) s/4/A/
208 3: $(test_oid t3) = 3: $(test_oid b3) s/11/B/
209 4: $(test_oid t4) = 4: $(test_oid b4) s/12/B/
211 test_cmp expect actual
214 test_expect_success 'new base, B...C' '
215 # this syntax includes the commits from main!
216 git range-diff --no-color topic...rebased >actual &&
217 cat >expect <<-EOF &&
218 -: $(test_oid __) > 1: $(test_oid b5) unrelated
219 1: $(test_oid t1) = 2: $(test_oid b1) s/5/A/
220 2: $(test_oid t2) = 3: $(test_oid b2) s/4/A/
221 3: $(test_oid t3) = 4: $(test_oid b3) s/11/B/
222 4: $(test_oid t4) = 5: $(test_oid b4) s/12/B/
224 test_cmp expect actual
227 test_expect_success 'changed commit' '
228 git range-diff --no-color topic...changed >actual &&
229 cat >expect <<-EOF &&
230 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
231 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
232 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
242 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
253 test_cmp expect actual
256 test_expect_success 'changed commit with --no-patch diff option' '
257 git range-diff --no-color --no-patch topic...changed >actual &&
258 cat >expect <<-EOF &&
259 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
260 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
261 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
262 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
264 test_cmp expect actual
267 test_expect_success 'changed commit with --stat diff option' '
268 git range-diff --no-color --stat topic...changed >actual &&
269 cat >expect <<-EOF &&
270 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
271 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
272 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
274 1 file changed, 1 insertion(+), 1 deletion(-)
275 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
277 1 file changed, 1 insertion(+), 1 deletion(-)
279 test_cmp expect actual
282 test_expect_success 'changed commit with sm config' '
283 git range-diff --no-color --submodule=log topic...changed >actual &&
284 cat >expect <<-EOF &&
285 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
286 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
287 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
297 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
308 test_cmp expect actual
311 test_expect_success 'renamed file' '
312 git range-diff --no-color --submodule=log topic...renamed-file >actual &&
313 sed s/Z/\ /g >expect <<-EOF &&
314 1: $(test_oid t1) = 1: $(test_oid n1) s/5/A/
315 2: $(test_oid t2) ! 2: $(test_oid n2) s/4/A/
317 ZAuthor: Thomas Rast <trast@inf.ethz.ch>
319 Z ## Commit message ##
321 + s/4/A/ + rename file
324 + ## file => renamed-file ##
328 3: $(test_oid t3) ! 3: $(test_oid n3) s/11/B/
330 Z ## Commit message ##
340 4: $(test_oid t4) ! 4: $(test_oid n4) s/12/B/
342 Z ## Commit message ##
353 test_cmp expect actual
356 test_expect_success 'file with mode only change' '
357 git range-diff --no-color --submodule=log topic...mode-only-change >actual &&
358 sed s/Z/\ /g >expect <<-EOF &&
359 1: $(test_oid t2) ! 1: $(test_oid o1) s/4/A/
361 ZAuthor: Thomas Rast <trast@inf.ethz.ch>
363 Z ## Commit message ##
365 + s/4/A/ + add other-file
374 + ## other-file (new) ##
375 2: $(test_oid t3) ! 2: $(test_oid o2) s/11/B/
377 ZAuthor: Thomas Rast <trast@inf.ethz.ch>
379 Z ## Commit message ##
381 + s/11/B/ + mode change other-file
390 + ## other-file (mode change 100644 => 100755) ##
391 3: $(test_oid t4) = 3: $(test_oid o3) s/12/B/
393 test_cmp expect actual
396 test_expect_success 'file added and later removed' '
397 git range-diff --no-color --submodule=log topic...added-removed >actual &&
398 sed s/Z/\ /g >expect <<-EOF &&
399 1: $(test_oid t1) = 1: $(test_oid s1) s/5/A/
400 2: $(test_oid t2) ! 2: $(test_oid s2) s/4/A/
402 ZAuthor: Thomas Rast <trast@inf.ethz.ch>
404 Z ## Commit message ##
415 + ## new-file (new) ##
416 3: $(test_oid t3) ! 3: $(test_oid s3) s/11/B/
418 ZAuthor: Thomas Rast <trast@inf.ethz.ch>
420 Z ## Commit message ##
422 + s/11/B/ + remove file
431 + ## new-file (deleted) ##
432 4: $(test_oid t4) = 4: $(test_oid s4) s/12/B/
434 test_cmp expect actual
437 test_expect_success 'no commits on one side' '
438 git commit --amend -m "new message" &&
439 git range-diff main HEAD@{1} HEAD
442 test_expect_success 'changed message' '
443 git range-diff --no-color topic...changed-message >actual &&
444 sed s/Z/\ /g >expect <<-EOF &&
445 1: $(test_oid t1) = 1: $(test_oid m1) s/5/A/
446 2: $(test_oid t2) ! 2: $(test_oid m2) s/4/A/
448 Z ## Commit message ##
451 + Also a silly comment here!
456 3: $(test_oid t3) = 3: $(test_oid m3) s/11/B/
457 4: $(test_oid t4) = 4: $(test_oid m4) s/12/B/
459 test_cmp expect actual
462 test_expect_success 'dual-coloring' '
463 sed -e "s|^:||" >expect <<-EOF &&
464 :<YELLOW>1: $(test_oid c1) = 1: $(test_oid m1) s/5/A/<RESET>
465 :<RED>2: $(test_oid c2) <RESET><YELLOW>!<RESET><GREEN> 2: $(test_oid m2)<RESET><YELLOW> s/4/A/<RESET>
466 : <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
467 : ## Commit message ##<RESET>
470 : <REVERSE><GREEN>+<RESET><BOLD> Also a silly comment here!<RESET>
471 : <REVERSE><GREEN>+<RESET>
475 :<RED>3: $(test_oid c3) <RESET><YELLOW>!<RESET><GREEN> 3: $(test_oid m3)<RESET><YELLOW> s/11/B/<RESET>
476 : <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
480 : <REVERSE><RED>-<RESET><FAINT;GREEN>+BB<RESET>
481 : <REVERSE><GREEN>+<RESET><BOLD;GREEN>+B<RESET>
485 :<RED>4: $(test_oid c4) <RESET><YELLOW>!<RESET><GREEN> 4: $(test_oid m4)<RESET><YELLOW> s/12/B/<RESET>
486 : <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
487 : <CYAN> @@ file: A<RESET>
490 : <REVERSE><RED>-<RESET><FAINT> BB<RESET>
491 : <REVERSE><GREEN>+<RESET><BOLD> B<RESET>
496 git range-diff changed...changed-message --color --dual-color >actual.raw &&
497 test_decode_color >actual <actual.raw &&
498 test_cmp expect actual
501 for prev in topic main..topic
503 test_expect_success "format-patch --range-diff=$prev" '
504 git format-patch --cover-letter --range-diff=$prev \
505 main..unmodified >actual &&
506 test_when_finished "rm 000?-*" &&
507 test_line_count = 5 actual &&
508 test_i18ngrep "^Range-diff:$" 0000-* &&
509 grep "= 1: .* s/5/A" 0000-* &&
510 grep "= 2: .* s/4/A" 0000-* &&
511 grep "= 3: .* s/11/B" 0000-* &&
512 grep "= 4: .* s/12/B" 0000-*
516 test_expect_success 'format-patch --range-diff as commentary' '
517 git format-patch --range-diff=HEAD~1 HEAD~1 >actual &&
518 test_when_finished "rm 0001-*" &&
519 test_line_count = 1 actual &&
520 test_i18ngrep "^Range-diff:$" 0001-* &&
521 grep "> 1: .* new message" 0001-*
524 test_expect_success 'range-diff overrides diff.noprefix internally' '
525 git -c diff.noprefix=true range-diff HEAD^...
528 test_expect_success 'basic with modified format.pretty with suffix' '
529 git -c format.pretty="format:commit %H%d%n" range-diff \
530 main..topic main..unmodified
533 test_expect_success 'basic with modified format.pretty without "commit "' '
534 git -c format.pretty="format:%H%n" range-diff \
535 main..topic main..unmodified
538 test_expect_success 'range-diff compares notes by default' '
539 git notes add -m "topic note" topic &&
540 git notes add -m "unmodified note" unmodified &&
541 test_when_finished git notes remove topic unmodified &&
542 git range-diff --no-color main..topic main..unmodified \
544 sed s/Z/\ /g >expect <<-EOF &&
545 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
546 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
547 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
548 4: $(test_oid t4) ! 4: $(test_oid u4) s/12/B/
559 test_cmp expect actual
562 test_expect_success 'range-diff with --no-notes' '
563 git notes add -m "topic note" topic &&
564 git notes add -m "unmodified note" unmodified &&
565 test_when_finished git notes remove topic unmodified &&
566 git range-diff --no-color --no-notes main..topic main..unmodified \
568 cat >expect <<-EOF &&
569 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
570 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
571 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
572 4: $(test_oid t4) = 4: $(test_oid u4) s/12/B/
574 test_cmp expect actual
577 test_expect_success 'range-diff with multiple --notes' '
578 git notes --ref=note1 add -m "topic note1" topic &&
579 git notes --ref=note1 add -m "unmodified note1" unmodified &&
580 test_when_finished git notes --ref=note1 remove topic unmodified &&
581 git notes --ref=note2 add -m "topic note2" topic &&
582 git notes --ref=note2 add -m "unmodified note2" unmodified &&
583 test_when_finished git notes --ref=note2 remove topic unmodified &&
584 git range-diff --no-color --notes=note1 --notes=note2 main..topic main..unmodified \
586 sed s/Z/\ /g >expect <<-EOF &&
587 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
588 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
589 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
590 4: $(test_oid t4) ! 4: $(test_oid u4) s/12/B/
594 Z ## Notes (note1) ##
599 Z ## Notes (note2) ##
606 test_cmp expect actual
609 test_expect_success 'format-patch --range-diff does not compare notes by default' '
610 git notes add -m "topic note" topic &&
611 git notes add -m "unmodified note" unmodified &&
612 test_when_finished git notes remove topic unmodified &&
613 git format-patch --cover-letter --range-diff=$prev \
614 main..unmodified >actual &&
615 test_when_finished "rm 000?-*" &&
616 test_line_count = 5 actual &&
617 test_i18ngrep "^Range-diff:$" 0000-* &&
618 grep "= 1: .* s/5/A" 0000-* &&
619 grep "= 2: .* s/4/A" 0000-* &&
620 grep "= 3: .* s/11/B" 0000-* &&
621 grep "= 4: .* s/12/B" 0000-* &&
622 ! grep "Notes" 0000-* &&
626 test_expect_success 'format-patch --range-diff with --no-notes' '
627 git notes add -m "topic note" topic &&
628 git notes add -m "unmodified note" unmodified &&
629 test_when_finished git notes remove topic unmodified &&
630 git format-patch --no-notes --cover-letter --range-diff=$prev \
631 main..unmodified >actual &&
632 test_when_finished "rm 000?-*" &&
633 test_line_count = 5 actual &&
634 test_i18ngrep "^Range-diff:$" 0000-* &&
635 grep "= 1: .* s/5/A" 0000-* &&
636 grep "= 2: .* s/4/A" 0000-* &&
637 grep "= 3: .* s/11/B" 0000-* &&
638 grep "= 4: .* s/12/B" 0000-* &&
639 ! grep "Notes" 0000-* &&
643 test_expect_success 'format-patch --range-diff with --notes' '
644 git notes add -m "topic note" topic &&
645 git notes add -m "unmodified note" unmodified &&
646 test_when_finished git notes remove topic unmodified &&
647 git format-patch --notes --cover-letter --range-diff=$prev \
648 main..unmodified >actual &&
649 test_when_finished "rm 000?-*" &&
650 test_line_count = 5 actual &&
651 test_i18ngrep "^Range-diff:$" 0000-* &&
652 grep "= 1: .* s/5/A" 0000-* &&
653 grep "= 2: .* s/4/A" 0000-* &&
654 grep "= 3: .* s/11/B" 0000-* &&
655 grep "! 4: .* s/12/B" 0000-* &&
656 sed s/Z/\ /g >expect <<-EOF &&
667 sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
668 test_cmp expect actual
671 test_expect_success 'format-patch --range-diff with format.notes config' '
672 git notes add -m "topic note" topic &&
673 git notes add -m "unmodified note" unmodified &&
674 test_when_finished git notes remove topic unmodified &&
675 test_config format.notes true &&
676 git format-patch --cover-letter --range-diff=$prev \
677 main..unmodified >actual &&
678 test_when_finished "rm 000?-*" &&
679 test_line_count = 5 actual &&
680 test_i18ngrep "^Range-diff:$" 0000-* &&
681 grep "= 1: .* s/5/A" 0000-* &&
682 grep "= 2: .* s/4/A" 0000-* &&
683 grep "= 3: .* s/11/B" 0000-* &&
684 grep "! 4: .* s/12/B" 0000-* &&
685 sed s/Z/\ /g >expect <<-EOF &&
696 sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
697 test_cmp expect actual
700 test_expect_success 'format-patch --range-diff with multiple notes' '
701 git notes --ref=note1 add -m "topic note1" topic &&
702 git notes --ref=note1 add -m "unmodified note1" unmodified &&
703 test_when_finished git notes --ref=note1 remove topic unmodified &&
704 git notes --ref=note2 add -m "topic note2" topic &&
705 git notes --ref=note2 add -m "unmodified note2" unmodified &&
706 test_when_finished git notes --ref=note2 remove topic unmodified &&
707 git format-patch --notes=note1 --notes=note2 --cover-letter --range-diff=$prev \
708 main..unmodified >actual &&
709 test_when_finished "rm 000?-*" &&
710 test_line_count = 5 actual &&
711 test_i18ngrep "^Range-diff:$" 0000-* &&
712 grep "= 1: .* s/5/A" 0000-* &&
713 grep "= 2: .* s/4/A" 0000-* &&
714 grep "= 3: .* s/11/B" 0000-* &&
715 grep "! 4: .* s/12/B" 0000-* &&
716 sed s/Z/\ /g >expect <<-EOF &&
720 Z ## Notes (note1) ##
725 Z ## Notes (note2) ##
732 sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
733 test_cmp expect actual
736 test_expect_success '--left-only/--right-only' '
737 git switch --orphan left-right &&
739 test_commit unmatched &&
740 test_commit common &&
741 git switch -C left-right first &&
742 git cherry-pick common &&
744 git range-diff -s --left-only ...common >actual &&
745 head_oid=$(git rev-parse --short HEAD) &&
746 common_oid=$(git rev-parse --short common) &&
747 echo "1: $head_oid = 2: $common_oid common" >expect &&
748 test_cmp expect actual