3 test_description='behavior of diff with symmetric-diff setups and --merge-base'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 # build these situations:
11 # - normal merge with one merge base (br1...b2r);
12 # - criss-cross merge ie 2 merge bases (br1...main);
13 # - disjoint subgraph (orphan branch, br3...main).
25 # We put files into a few commits so that we can verify the
28 test_expect_success setup '
29 git commit --allow-empty -m A &&
33 git checkout -b br1 HEAD^ &&
38 git merge -m D main &&
41 git merge -m E commit-C &&
42 git checkout -b br2 commit-C &&
48 git checkout --orphan br3 &&
52 test_expect_success 'diff with one merge base' '
53 git diff commit-D...br1 >tmp &&
54 tail -n 1 tmp >actual &&
56 test_cmp expect actual
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
63 test_expect_success 'diff with two merge bases' '
64 git diff br1...main >tmp 2>err &&
65 test_line_count = 7 tmp &&
66 test_line_count = 1 err
69 test_expect_success 'diff with no merge bases' '
70 test_must_fail git diff br2...br3 2>err &&
71 test_i18ngrep "fatal: br2...br3: no merge base" err
74 test_expect_success 'diff with too many symmetric differences' '
75 test_must_fail git diff br1...main br2...br3 2>err &&
76 test_i18ngrep "usage" err
79 test_expect_success 'diff with symmetric difference and extraneous arg' '
80 test_must_fail git diff main br1...main 2>err &&
81 test_i18ngrep "usage" err
84 test_expect_success 'diff with two ranges' '
85 test_must_fail git diff main br1..main br2..br3 2>err &&
86 test_i18ngrep "usage" err
89 test_expect_success 'diff with ranges and extra arg' '
90 test_must_fail git diff main br1..main commit-D 2>err &&
91 test_i18ngrep "usage" err
94 test_expect_success 'diff --merge-base with no commits' '
95 test_must_fail git diff --merge-base
98 test_expect_success 'diff --merge-base with three commits' '
99 test_must_fail git diff --merge-base br1 br2 main 2>err &&
100 test_i18ngrep "usage" err
103 for cmd in diff-index diff
105 test_expect_success "$cmd --merge-base with one commit" '
107 git $cmd commit-C >expect &&
108 git $cmd --merge-base br2 >actual &&
109 test_cmp expect actual
112 test_expect_success "$cmd --merge-base with one commit and unstaged changes" '
114 test_when_finished git reset --hard &&
116 git $cmd commit-C >expect &&
117 git $cmd --merge-base br2 >actual &&
118 test_cmp expect actual
121 test_expect_success "$cmd --merge-base with one commit and staged and unstaged changes" '
123 test_when_finished git reset --hard &&
127 git $cmd commit-C >expect &&
128 git $cmd --merge-base br2 >actual &&
129 test_cmp expect actual
132 test_expect_success "$cmd --merge-base --cached with one commit and staged and unstaged changes" '
134 test_when_finished git reset --hard &&
138 git $cmd --cached commit-C >expect &&
139 git $cmd --cached --merge-base br2 >actual &&
140 test_cmp expect actual
143 test_expect_success "$cmd --merge-base with non-commit" '
145 test_must_fail git $cmd --merge-base main^{tree} 2>err &&
146 test_i18ngrep "fatal: --merge-base only works with commits" err
149 test_expect_success "$cmd --merge-base with no merge bases and one commit" '
151 test_must_fail git $cmd --merge-base br3 2>err &&
152 test_i18ngrep "fatal: no merge base found" err
155 test_expect_success "$cmd --merge-base with multiple merge bases and one commit" '
157 test_must_fail git $cmd --merge-base br1 2>err &&
158 test_i18ngrep "fatal: multiple merge bases found" err
162 for cmd in diff-tree diff
164 test_expect_success "$cmd --merge-base with two commits" '
165 git $cmd commit-C main >expect &&
166 git $cmd --merge-base br2 main >actual &&
167 test_cmp expect actual
170 test_expect_success "$cmd --merge-base commit and non-commit" '
171 test_must_fail git $cmd --merge-base br2 main^{tree} 2>err &&
172 test_i18ngrep "fatal: --merge-base only works with commits" err
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
180 test_expect_success "$cmd --merge-base with multiple merge bases and two commits" '
181 test_must_fail git $cmd --merge-base main br1 2>err &&
182 test_i18ngrep "fatal: multiple merge bases found" err
186 test_expect_success 'diff-tree --merge-base with one commit' '
187 test_must_fail git diff-tree --merge-base main 2>err &&
188 test_i18ngrep "fatal: --merge-base only works with two commits" err
191 test_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