1#!/bin/sh
2
3test_description='test git rev-list --cherry-pick -- file'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export 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
19test_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
54cat >expect <<EOF
55<tags/B
56>tags/C
57EOF
58
59test_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
66test_expect_success '--count' '
67	git rev-list --count B...C > actual &&
68	test "$(cat actual)" = 2
69'
70
71test_expect_success '--cherry-pick foo comes up empty' '
72	test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
73'
74
75cat >expect <<EOF
76>tags/C
77EOF
78
79test_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
86test_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
93cat >expect <<EOF
94<tags/F
95>tags/E
96EOF
97
98test_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
105test_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
112cat >expect <<EOF
113<tags/F
114EOF
115
116test_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
124cat >expect <<EOF
125<tags/F
126EOF
127
128test_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
136test_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
143cat >expect <<EOF
144+tags/F
145=tags/D
146+tags/E
147=tags/C
148EOF
149
150test_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
157cat >expect <<EOF
158<tags/F
159=tags/D
160>tags/E
161=tags/C
162EOF
163
164test_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
171cat >expect <<EOF
172tags/E
173EOF
174
175test_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
182test_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
189cat >expect <<EOF
190+tags/E
191=tags/C
192EOF
193
194test_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
201cat >expect <<EOF
2021	1
203EOF
204
205test_expect_success '--cherry --count' '
206	git rev-list --cherry --count F...E -- bar > actual &&
207	test_cmp expect actual
208'
209
210cat >expect <<EOF
2112	2
212EOF
213
214test_expect_success '--cherry-mark --count' '
215	git rev-list --cherry-mark --count F...E -- bar > actual &&
216	test_cmp expect actual
217'
218
219cat >expect <<EOF
2201	1	2
221EOF
222
223test_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
228test_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
242cat >expect <<EOF
2431	2
244EOF
245
246test_expect_success '--count --left-right' '
247	git rev-list --count --left-right C...D > actual &&
248	test_cmp expect actual
249'
250
251test_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.
265remove_loose_object () {
266	sha1="$(git rev-parse "$1")" &&
267	remainder=${sha1#??} &&
268	firsttwo=${sha1%$remainder} &&
269	rm .git/objects/$firsttwo/$remainder
270}
271
272test_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
284test_done
285