Merge branch 'dl/complete-stash-updates'
[git] / t / t6007-rev-list-cherry-pick-file.sh
1 #!/bin/sh
2
3 test_description='test git rev-list --cherry-pick -- file'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 # A---B---D---F
11 #  \
12 #   \
13 #    C---E
14 #
15 # B changes a file foo.c, adding a line of text.  C changes foo.c as
16 # well as bar.c, but the change in foo.c was identical to change B.
17 # D and C change bar in the same way, E and F differently.
18
19 test_expect_success setup '
20         echo Hallo > foo &&
21         git add foo &&
22         test_tick &&
23         git commit -m "A" &&
24         git tag A &&
25         git checkout -b branch &&
26         echo Bello > foo &&
27         echo Cello > bar &&
28         git add foo bar &&
29         test_tick &&
30         git commit -m "C" &&
31         git tag C &&
32         echo Dello > bar &&
33         git add bar &&
34         test_tick &&
35         git commit -m "E" &&
36         git tag E &&
37         git checkout main &&
38         git checkout branch foo &&
39         test_tick &&
40         git commit -m "B" &&
41         git tag B &&
42         echo Cello > bar &&
43         git add bar &&
44         test_tick &&
45         git commit -m "D" &&
46         git tag D &&
47         echo Nello > bar &&
48         git add bar &&
49         test_tick &&
50         git commit -m "F" &&
51         git tag F
52 '
53
54 cat >expect <<EOF
55 <tags/B
56 >tags/C
57 EOF
58
59 test_expect_success '--left-right' '
60         git rev-list --left-right B...C > actual &&
61         git name-rev --stdin --name-only --refs="*tags/*" \
62                 < actual > actual.named &&
63         test_cmp expect actual.named
64 '
65
66 test_expect_success '--count' '
67         git rev-list --count B...C > actual &&
68         test "$(cat actual)" = 2
69 '
70
71 test_expect_success '--cherry-pick foo comes up empty' '
72         test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
73 '
74
75 cat >expect <<EOF
76 >tags/C
77 EOF
78
79 test_expect_success '--cherry-pick bar does not come up empty' '
80         git rev-list --left-right --cherry-pick B...C -- bar > actual &&
81         git name-rev --stdin --name-only --refs="*tags/*" \
82                 < actual > actual.named &&
83         test_cmp expect actual.named
84 '
85
86 test_expect_success 'bar does not come up empty' '
87         git rev-list --left-right B...C -- bar > actual &&
88         git name-rev --stdin --name-only --refs="*tags/*" \
89                 < actual > actual.named &&
90         test_cmp expect actual.named
91 '
92
93 cat >expect <<EOF
94 <tags/F
95 >tags/E
96 EOF
97
98 test_expect_success '--cherry-pick bar does not come up empty (II)' '
99         git rev-list --left-right --cherry-pick F...E -- bar > actual &&
100         git name-rev --stdin --name-only --refs="*tags/*" \
101                 < actual > actual.named &&
102         test_cmp expect actual.named
103 '
104
105 test_expect_success 'name-rev multiple --refs combine inclusive' '
106         git rev-list --left-right --cherry-pick F...E -- bar >actual &&
107         git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" \
108                 <actual >actual.named &&
109         test_cmp expect actual.named
110 '
111
112 cat >expect <<EOF
113 <tags/F
114 EOF
115
116 test_expect_success 'name-rev --refs excludes non-matched patterns' '
117         git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
118         git rev-list --left-right --cherry-pick F...E -- bar >actual &&
119         git name-rev --stdin --name-only --refs="*tags/F" \
120                 <actual >actual.named &&
121         test_cmp expect actual.named
122 '
123
124 cat >expect <<EOF
125 <tags/F
126 EOF
127
128 test_expect_success 'name-rev --exclude excludes matched patterns' '
129         git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
130         git rev-list --left-right --cherry-pick F...E -- bar >actual &&
131         git name-rev --stdin --name-only --refs="*tags/*" --exclude="*E" \
132                 <actual >actual.named &&
133         test_cmp expect actual.named
134 '
135
136 test_expect_success 'name-rev --no-refs clears the refs list' '
137         git rev-list --left-right --cherry-pick F...E -- bar >expect &&
138         git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" --no-refs --refs="*tags/G" \
139                 <expect >actual &&
140         test_cmp expect actual
141 '
142
143 cat >expect <<EOF
144 +tags/F
145 =tags/D
146 +tags/E
147 =tags/C
148 EOF
149
150 test_expect_success '--cherry-mark' '
151         git rev-list --cherry-mark F...E -- bar > actual &&
152         git name-rev --stdin --name-only --refs="*tags/*" \
153                 < actual > actual.named &&
154         test_cmp expect actual.named
155 '
156
157 cat >expect <<EOF
158 <tags/F
159 =tags/D
160 >tags/E
161 =tags/C
162 EOF
163
164 test_expect_success '--cherry-mark --left-right' '
165         git rev-list --cherry-mark --left-right F...E -- bar > actual &&
166         git name-rev --stdin --name-only --refs="*tags/*" \
167                 < actual > actual.named &&
168         test_cmp expect actual.named
169 '
170
171 cat >expect <<EOF
172 tags/E
173 EOF
174
175 test_expect_success '--cherry-pick --right-only' '
176         git rev-list --cherry-pick --right-only F...E -- bar > actual &&
177         git name-rev --stdin --name-only --refs="*tags/*" \
178                 < actual > actual.named &&
179         test_cmp expect actual.named
180 '
181
182 test_expect_success '--cherry-pick --left-only' '
183         git rev-list --cherry-pick --left-only E...F -- bar > actual &&
184         git name-rev --stdin --name-only --refs="*tags/*" \
185                 < actual > actual.named &&
186         test_cmp expect actual.named
187 '
188
189 cat >expect <<EOF
190 +tags/E
191 =tags/C
192 EOF
193
194 test_expect_success '--cherry' '
195         git rev-list --cherry F...E -- bar > actual &&
196         git name-rev --stdin --name-only --refs="*tags/*" \
197                 < actual > actual.named &&
198         test_cmp expect actual.named
199 '
200
201 cat >expect <<EOF
202 1       1
203 EOF
204
205 test_expect_success '--cherry --count' '
206         git rev-list --cherry --count F...E -- bar > actual &&
207         test_cmp expect actual
208 '
209
210 cat >expect <<EOF
211 2       2
212 EOF
213
214 test_expect_success '--cherry-mark --count' '
215         git rev-list --cherry-mark --count F...E -- bar > actual &&
216         test_cmp expect actual
217 '
218
219 cat >expect <<EOF
220 1       1       2
221 EOF
222
223 test_expect_success '--cherry-mark --left-right --count' '
224         git rev-list --cherry-mark --left-right --count F...E -- bar > actual &&
225         test_cmp expect actual
226 '
227
228 test_expect_success '--cherry-pick with independent, but identical branches' '
229         git symbolic-ref HEAD refs/heads/independent &&
230         rm .git/index &&
231         echo Hallo > foo &&
232         git add foo &&
233         test_tick &&
234         git commit -m "independent" &&
235         echo Bello > foo &&
236         test_tick &&
237         git commit -m "independent, too" foo &&
238         test -z "$(git rev-list --left-right --cherry-pick \
239                 HEAD...main -- foo)"
240 '
241
242 cat >expect <<EOF
243 1       2
244 EOF
245
246 test_expect_success '--count --left-right' '
247         git rev-list --count --left-right C...D > actual &&
248         test_cmp expect actual
249 '
250
251 test_expect_success '--cherry-pick with duplicates on each side' '
252         git checkout -b dup-orig &&
253         test_commit dup-base &&
254         git revert dup-base &&
255         git cherry-pick dup-base &&
256         git checkout -b dup-side HEAD~3 &&
257         test_tick &&
258         git cherry-pick -3 dup-orig &&
259         git rev-list --cherry-pick dup-orig...dup-side >actual &&
260         test_must_be_empty actual
261 '
262
263 # Corrupt the object store deliberately to make sure
264 # the object is not even checked for its existence.
265 remove_loose_object () {
266         sha1="$(git rev-parse "$1")" &&
267         remainder=${sha1#??} &&
268         firsttwo=${sha1%$remainder} &&
269         rm .git/objects/$firsttwo/$remainder
270 }
271
272 test_expect_success '--cherry-pick avoids looking at full diffs' '
273         git checkout -b shy-diff &&
274         test_commit dont-look-at-me &&
275         echo Hello >dont-look-at-me.t &&
276         test_tick &&
277         git commit -m tip dont-look-at-me.t &&
278         git checkout -b mainline HEAD^ &&
279         test_commit to-cherry-pick &&
280         remove_loose_object shy-diff^:dont-look-at-me.t &&
281         git rev-list --cherry-pick ...shy-diff
282 '
283
284 test_done