blob: 2d650d8f1032ca7c113fa0f48c72719fb08ab245 [file] [log] [blame]
Chris Torek8bfcb3a2020-06-12 16:19:59 +00001#!/bin/sh
2
Denton Liudf7dbab2020-09-20 04:22:24 -07003test_description='behavior of diff with symmetric-diff setups and --merge-base'
Chris Torek8bfcb3a2020-06-12 16:19:59 +00004
Johannes Schindelin8f378542020-11-18 23:44:27 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Chris Torek8bfcb3a2020-06-12 16:19:59 +00008. ./test-lib.sh
9
10# build these situations:
11# - normal merge with one merge base (br1...b2r);
Johannes Schindelin8f378542020-11-18 23:44:27 +000012# - criss-cross merge ie 2 merge bases (br1...main);
13# - disjoint subgraph (orphan branch, br3...main).
Chris Torek8bfcb3a2020-06-12 16:19:59 +000014#
Johannes Schindelin8f378542020-11-18 23:44:27 +000015# B---E <-- main
Chris Torek8bfcb3a2020-06-12 16:19:59 +000016# / \ /
17# A X
18# \ / \
19# C---D--G <-- br1
20# \ /
21# ---F <-- br2
22#
23# H <-- br3
24#
25# We put files into a few commits so that we can verify the
26# output as well.
27
28test_expect_success setup '
29 git commit --allow-empty -m A &&
30 echo b >b &&
31 git add b &&
32 git commit -m B &&
33 git checkout -b br1 HEAD^ &&
34 echo c >c &&
35 git add c &&
36 git commit -m C &&
37 git tag commit-C &&
Johannes Schindelin8f378542020-11-18 23:44:27 +000038 git merge -m D main &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000039 git tag commit-D &&
Johannes Schindelin8f378542020-11-18 23:44:27 +000040 git checkout main &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000041 git merge -m E commit-C &&
42 git checkout -b br2 commit-C &&
43 echo f >f &&
44 git add f &&
45 git commit -m F &&
46 git checkout br1 &&
47 git merge -m G br2 &&
48 git checkout --orphan br3 &&
49 git commit -m H
50'
51
52test_expect_success 'diff with one merge base' '
53 git diff commit-D...br1 >tmp &&
54 tail -n 1 tmp >actual &&
55 echo +f >expect &&
56 test_cmp expect actual
57'
58
59# The output (in tmp) can have +b or +c depending
60# on which merge base (commit B or C) is picked.
61# It should have one of those two, which comes out
62# to seven lines.
63test_expect_success 'diff with two merge bases' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000064 git diff br1...main >tmp 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000065 test_line_count = 7 tmp &&
66 test_line_count = 1 err
67'
68
69test_expect_success 'diff with no merge bases' '
Denton Liu8023a5e2020-09-17 00:44:05 -070070 test_must_fail git diff br2...br3 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000071 test_i18ngrep "fatal: br2...br3: no merge base" err
72'
73
74test_expect_success 'diff with too many symmetric differences' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000075 test_must_fail git diff br1...main br2...br3 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000076 test_i18ngrep "usage" err
77'
78
79test_expect_success 'diff with symmetric difference and extraneous arg' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000080 test_must_fail git diff main br1...main 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000081 test_i18ngrep "usage" err
82'
83
84test_expect_success 'diff with two ranges' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000085 test_must_fail git diff main br1..main br2..br3 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000086 test_i18ngrep "usage" err
87'
88
89test_expect_success 'diff with ranges and extra arg' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000090 test_must_fail git diff main br1..main commit-D 2>err &&
Chris Torek8bfcb3a2020-06-12 16:19:59 +000091 test_i18ngrep "usage" err
92'
93
Denton Liudf7dbab2020-09-20 04:22:24 -070094test_expect_success 'diff --merge-base with no commits' '
95 test_must_fail git diff --merge-base
96'
97
98test_expect_success 'diff --merge-base with three commits' '
Johannes Schindelin8f378542020-11-18 23:44:27 +000099 test_must_fail git diff --merge-base br1 br2 main 2>err &&
Denton Liudf7dbab2020-09-20 04:22:24 -0700100 test_i18ngrep "usage" err
101'
102
Denton Liu0f5a1d42020-09-20 04:22:25 -0700103for cmd in diff-index diff
104do
105 test_expect_success "$cmd --merge-base with one commit" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000106 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700107 git $cmd commit-C >expect &&
108 git $cmd --merge-base br2 >actual &&
109 test_cmp expect actual
110 '
111
112 test_expect_success "$cmd --merge-base with one commit and unstaged changes" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000113 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700114 test_when_finished git reset --hard &&
115 echo unstaged >>c &&
116 git $cmd commit-C >expect &&
117 git $cmd --merge-base br2 >actual &&
118 test_cmp expect actual
119 '
120
121 test_expect_success "$cmd --merge-base with one commit and staged and unstaged changes" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000122 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700123 test_when_finished git reset --hard &&
124 echo staged >>c &&
125 git add c &&
126 echo unstaged >>c &&
127 git $cmd commit-C >expect &&
128 git $cmd --merge-base br2 >actual &&
129 test_cmp expect actual
130 '
131
132 test_expect_success "$cmd --merge-base --cached with one commit and staged and unstaged changes" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000133 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700134 test_when_finished git reset --hard &&
135 echo staged >>c &&
136 git add c &&
137 echo unstaged >>c &&
138 git $cmd --cached commit-C >expect &&
139 git $cmd --cached --merge-base br2 >actual &&
140 test_cmp expect actual
141 '
142
143 test_expect_success "$cmd --merge-base with non-commit" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000144 git checkout main &&
145 test_must_fail git $cmd --merge-base main^{tree} 2>err &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700146 test_i18ngrep "fatal: --merge-base only works with commits" err
147 '
148
149 test_expect_success "$cmd --merge-base with no merge bases and one commit" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000150 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700151 test_must_fail git $cmd --merge-base br3 2>err &&
152 test_i18ngrep "fatal: no merge base found" err
153 '
154
155 test_expect_success "$cmd --merge-base with multiple merge bases and one commit" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000156 git checkout main &&
Denton Liu0f5a1d42020-09-20 04:22:25 -0700157 test_must_fail git $cmd --merge-base br1 2>err &&
158 test_i18ngrep "fatal: multiple merge bases found" err
159 '
160done
161
Denton Liu3d09c222020-09-14 11:36:52 -0700162for cmd in diff-tree diff
163do
164 test_expect_success "$cmd --merge-base with two commits" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000165 git $cmd commit-C main >expect &&
166 git $cmd --merge-base br2 main >actual &&
Denton Liu3d09c222020-09-14 11:36:52 -0700167 test_cmp expect actual
168 '
169
170 test_expect_success "$cmd --merge-base commit and non-commit" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000171 test_must_fail git $cmd --merge-base br2 main^{tree} 2>err &&
Denton Liu3d09c222020-09-14 11:36:52 -0700172 test_i18ngrep "fatal: --merge-base only works with commits" err
173 '
174
175 test_expect_success "$cmd --merge-base with no merge bases and two commits" '
176 test_must_fail git $cmd --merge-base br2 br3 2>err &&
177 test_i18ngrep "fatal: no merge base found" err
178 '
179
180 test_expect_success "$cmd --merge-base with multiple merge bases and two commits" '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000181 test_must_fail git $cmd --merge-base main br1 2>err &&
Denton Liu3d09c222020-09-14 11:36:52 -0700182 test_i18ngrep "fatal: multiple merge bases found" err
183 '
184done
185
186test_expect_success 'diff-tree --merge-base with one commit' '
Johannes Schindelin8f378542020-11-18 23:44:27 +0000187 test_must_fail git diff-tree --merge-base main 2>err &&
Denton Liu3d09c222020-09-14 11:36:52 -0700188 test_i18ngrep "fatal: --merge-base only works with two commits" err
189'
190
191test_expect_success 'diff --merge-base with range' '
192 test_must_fail git diff --merge-base br2..br3 2>err &&
193 test_i18ngrep "fatal: --merge-base does not work with ranges" err
194'
195
Chris Torek8bfcb3a2020-06-12 16:19:59 +0000196test_done