]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4068-diff-symmetric-merge-base.sh
The third batch
[thirdparty/git.git] / t / t4068-diff-symmetric-merge-base.sh
CommitLineData
8bfcb3a6
CT
1#!/bin/sh
2
df7dbab8 3test_description='behavior of diff with symmetric-diff setups and --merge-base'
8bfcb3a6 4
8f37854b 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8bfcb3a6
CT
8. ./test-lib.sh
9
10# build these situations:
11# - normal merge with one merge base (br1...b2r);
8f37854b
JS
12# - criss-cross merge ie 2 merge bases (br1...main);
13# - disjoint subgraph (orphan branch, br3...main).
8bfcb3a6 14#
8f37854b 15# B---E <-- main
8bfcb3a6
CT
16# / \ /
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 &&
4adceb5a 37 git tag -m commit-C commit-C &&
8f37854b 38 git merge -m D main &&
8bfcb3a6 39 git tag commit-D &&
8f37854b 40 git checkout main &&
8bfcb3a6
CT
41 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' '
8f37854b 64 git diff br1...main >tmp 2>err &&
8bfcb3a6
CT
65 test_line_count = 7 tmp &&
66 test_line_count = 1 err
67'
68
69test_expect_success 'diff with no merge bases' '
8023a5e8 70 test_must_fail git diff br2...br3 2>err &&
6789275d 71 test_grep "fatal: br2...br3: no merge base" err
8bfcb3a6
CT
72'
73
74test_expect_success 'diff with too many symmetric differences' '
8f37854b 75 test_must_fail git diff br1...main br2...br3 2>err &&
6789275d 76 test_grep "usage" err
8bfcb3a6
CT
77'
78
79test_expect_success 'diff with symmetric difference and extraneous arg' '
8f37854b 80 test_must_fail git diff main br1...main 2>err &&
6789275d 81 test_grep "usage" err
8bfcb3a6
CT
82'
83
84test_expect_success 'diff with two ranges' '
8f37854b 85 test_must_fail git diff main br1..main br2..br3 2>err &&
6789275d 86 test_grep "usage" err
8bfcb3a6
CT
87'
88
89test_expect_success 'diff with ranges and extra arg' '
8f37854b 90 test_must_fail git diff main br1..main commit-D 2>err &&
6789275d 91 test_grep "usage" err
8bfcb3a6
CT
92'
93
df7dbab8
DL
94test_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' '
8f37854b 99 test_must_fail git diff --merge-base br1 br2 main 2>err &&
6789275d 100 test_grep "usage" err
df7dbab8
DL
101'
102
0f5a1d44
DL
103for cmd in diff-index diff
104do
105 test_expect_success "$cmd --merge-base with one commit" '
8f37854b 106 git checkout main &&
0f5a1d44
DL
107 git $cmd commit-C >expect &&
108 git $cmd --merge-base br2 >actual &&
109 test_cmp expect actual
110 '
111
4adceb5a
AR
112 test_expect_success "$cmd --merge-base with annotated tag" '
113 git checkout main &&
114 git $cmd commit-C >expect &&
115 git $cmd --merge-base commit-C >actual &&
116 test_cmp expect actual
117 '
118
0f5a1d44 119 test_expect_success "$cmd --merge-base with one commit and unstaged changes" '
8f37854b 120 git checkout main &&
0f5a1d44
DL
121 test_when_finished git reset --hard &&
122 echo unstaged >>c &&
123 git $cmd commit-C >expect &&
124 git $cmd --merge-base br2 >actual &&
125 test_cmp expect actual
126 '
127
128 test_expect_success "$cmd --merge-base with one commit and staged and unstaged changes" '
8f37854b 129 git checkout main &&
0f5a1d44
DL
130 test_when_finished git reset --hard &&
131 echo staged >>c &&
132 git add c &&
133 echo unstaged >>c &&
134 git $cmd commit-C >expect &&
135 git $cmd --merge-base br2 >actual &&
136 test_cmp expect actual
137 '
138
139 test_expect_success "$cmd --merge-base --cached with one commit and staged and unstaged changes" '
8f37854b 140 git checkout main &&
0f5a1d44
DL
141 test_when_finished git reset --hard &&
142 echo staged >>c &&
143 git add c &&
144 echo unstaged >>c &&
145 git $cmd --cached commit-C >expect &&
146 git $cmd --cached --merge-base br2 >actual &&
147 test_cmp expect actual
148 '
149
150 test_expect_success "$cmd --merge-base with non-commit" '
8f37854b
JS
151 git checkout main &&
152 test_must_fail git $cmd --merge-base main^{tree} 2>err &&
a8e23947 153 test_grep "is a tree, not a commit" err
0f5a1d44
DL
154 '
155
156 test_expect_success "$cmd --merge-base with no merge bases and one commit" '
8f37854b 157 git checkout main &&
0f5a1d44 158 test_must_fail git $cmd --merge-base br3 2>err &&
6789275d 159 test_grep "fatal: no merge base found" err
0f5a1d44
DL
160 '
161
162 test_expect_success "$cmd --merge-base with multiple merge bases and one commit" '
8f37854b 163 git checkout main &&
0f5a1d44 164 test_must_fail git $cmd --merge-base br1 2>err &&
6789275d 165 test_grep "fatal: multiple merge bases found" err
0f5a1d44
DL
166 '
167done
168
3d09c228
DL
169for cmd in diff-tree diff
170do
171 test_expect_success "$cmd --merge-base with two commits" '
8f37854b
JS
172 git $cmd commit-C main >expect &&
173 git $cmd --merge-base br2 main >actual &&
3d09c228
DL
174 test_cmp expect actual
175 '
176
177 test_expect_success "$cmd --merge-base commit and non-commit" '
8f37854b 178 test_must_fail git $cmd --merge-base br2 main^{tree} 2>err &&
a8e23947 179 test_grep "is a tree, not a commit" err
3d09c228
DL
180 '
181
182 test_expect_success "$cmd --merge-base with no merge bases and two commits" '
183 test_must_fail git $cmd --merge-base br2 br3 2>err &&
6789275d 184 test_grep "fatal: no merge base found" err
3d09c228
DL
185 '
186
187 test_expect_success "$cmd --merge-base with multiple merge bases and two commits" '
8f37854b 188 test_must_fail git $cmd --merge-base main br1 2>err &&
6789275d 189 test_grep "fatal: multiple merge bases found" err
3d09c228
DL
190 '
191done
192
193test_expect_success 'diff-tree --merge-base with one commit' '
8f37854b 194 test_must_fail git diff-tree --merge-base main 2>err &&
6789275d 195 test_grep "fatal: --merge-base only works with two commits" err
3d09c228
DL
196'
197
198test_expect_success 'diff --merge-base with range' '
199 test_must_fail git diff --merge-base br2..br3 2>err &&
6789275d 200 test_grep "fatal: --merge-base does not work with ranges" err
3d09c228
DL
201'
202
8bfcb3a6 203test_done