blob: 20f7110ec108fda462f5e9a0a43221d2c66e7fe1 [file] [log] [blame]
Junio C Hamano7be1d622006-09-23 03:40:17 -07001#!/bin/sh
2# Copyright (c) 2006, Junio C Hamano.
3
4test_description='Per branch config variables affects "git fetch".
5
6'
7
8. ./test-lib.sh
Ævar Arnfjörð Bjarmasone8a8e7f2021-02-09 22:41:52 +01009. "$TEST_DIRECTORY"/lib-bundle.sh
Junio C Hamano7be1d622006-09-23 03:40:17 -070010
Elia Pintoa9d32be2015-12-23 14:45:55 +010011D=$(pwd)
Junio C Hamano7be1d622006-09-23 03:40:17 -070012
Junio C Hamano7be1d622006-09-23 03:40:17 -070013test_expect_success setup '
14 echo >file original &&
15 git add file &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000016 git commit -a -m original &&
17 git branch -M main
18'
Junio C Hamano7be1d622006-09-23 03:40:17 -070019
20test_expect_success "clone and setup child repos" '
21 git clone . one &&
Jonathan Nieder18a82692010-09-06 20:42:54 -050022 (
23 cd one &&
24 echo >file updated by one &&
25 git commit -a -m "updated by one"
Jens Lehmannfd4ec4f2010-09-06 20:39:54 +020026 ) &&
Junio C Hamano7be1d622006-09-23 03:40:17 -070027 git clone . two &&
Jonathan Nieder18a82692010-09-06 20:42:54 -050028 (
29 cd two &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000030 git config branch.main.remote one &&
Jonathan Nieder18a82692010-09-06 20:42:54 -050031 git config remote.one.url ../one/.git/ &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000032 git config remote.one.fetch refs/heads/main:refs/heads/one
Jens Lehmannfd4ec4f2010-09-06 20:39:54 +020033 ) &&
Santi Béjar6cc7c362006-09-23 22:55:35 +020034 git clone . three &&
Jonathan Nieder18a82692010-09-06 20:42:54 -050035 (
36 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000037 git config branch.main.remote two &&
38 git config branch.main.merge refs/heads/one &&
Jonathan Nieder18a82692010-09-06 20:42:54 -050039 mkdir -p .git/remotes &&
Eric Sunshine020b8132021-12-09 00:11:04 -050040 cat >.git/remotes/two <<-\EOF
41 URL: ../two/.git/
42 Pull: refs/heads/main:refs/heads/two
43 Pull: refs/heads/one:refs/heads/one
44 EOF
Jens Lehmannfd4ec4f2010-09-06 20:39:54 +020045 ) &&
Jeff King9a7bbd12008-06-16 12:15:02 -040046 git clone . bundle &&
47 git clone . seven
Junio C Hamano7be1d622006-09-23 03:40:17 -070048'
49
50test_expect_success "fetch test" '
51 cd "$D" &&
52 echo >file updated by origin &&
53 git commit -a -m "updated by origin" &&
54 cd two &&
55 git fetch &&
David Turnercbc5cf72018-05-23 07:25:17 +020056 git rev-parse --verify refs/heads/one &&
Elia Pintoa9d32be2015-12-23 14:45:55 +010057 mine=$(git rev-parse refs/heads/one) &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000058 his=$(cd ../one && git rev-parse refs/heads/main) &&
Junio C Hamano7be1d622006-09-23 03:40:17 -070059 test "z$mine" = "z$his"
60'
61
Santi Béjar6cc7c362006-09-23 22:55:35 +020062test_expect_success "fetch test for-merge" '
63 cd "$D" &&
64 cd three &&
65 git fetch &&
David Turnercbc5cf72018-05-23 07:25:17 +020066 git rev-parse --verify refs/heads/two &&
67 git rev-parse --verify refs/heads/one &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000068 main_in_two=$(cd ../two && git rev-parse main) &&
Elia Pintoa9d32be2015-12-23 14:45:55 +010069 one_in_two=$(cd ../two && git rev-parse one) &&
Santi Béjar6cc7c362006-09-23 22:55:35 +020070 {
Eric Sunshine7abcbcb2021-12-09 00:11:08 -050071 echo "$one_in_two " &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000072 echo "$main_in_two not-for-merge"
Santi Béjar6cc7c362006-09-23 22:55:35 +020073 } >expected &&
74 cut -f -2 .git/FETCH_HEAD >actual &&
Gary V. Vaughan4fdf71b2010-05-14 09:31:37 +000075 test_cmp expected actual'
Santi Béjar6cc7c362006-09-23 22:55:35 +020076
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020077test_expect_success 'fetch --prune on its own works as expected' '
78 cd "$D" &&
79 git clone . prune &&
80 cd prune &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000081 git update-ref refs/remotes/origin/extrabranch main &&
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020082
83 git fetch --prune origin &&
84 test_must_fail git rev-parse origin/extrabranch
85'
86
Carlos Martín Nietoed43de62011-10-15 07:04:25 +020087test_expect_success 'fetch --prune with a branch name keeps branches' '
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020088 cd "$D" &&
89 git clone . prune-branch &&
90 cd prune-branch &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +000091 git update-ref refs/remotes/origin/extrabranch main &&
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020092
Johannes Schindelin83ecf262020-12-17 01:07:06 +000093 git fetch --prune origin main &&
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020094 git rev-parse origin/extrabranch
95'
96
Carlos Martín Nietoed43de62011-10-15 07:04:25 +020097test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +020098 cd "$D" &&
99 git clone . prune-namespace &&
100 cd prune-namespace &&
101
102 git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000103 git rev-parse origin/main
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200104'
105
Carlos Martín Nietoe6f63712014-02-27 10:00:10 +0100106test_expect_success 'fetch --prune handles overlapping refspecs' '
Carlos Martín Nietof377e7a2014-02-27 10:00:09 +0100107 cd "$D" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000108 git update-ref refs/pull/42/head main &&
Carlos Martín Nietof377e7a2014-02-27 10:00:09 +0100109 git clone . prune-overlapping &&
110 cd prune-overlapping &&
111 git config --add remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
112
113 git fetch --prune origin &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000114 git rev-parse origin/main &&
Carlos Martín Nietof377e7a2014-02-27 10:00:09 +0100115 git rev-parse origin/pr/42 &&
116
Jeff King8fb26872015-03-20 06:06:15 -0400117 git config --unset-all remote.origin.fetch &&
Carlos Martín Nietof377e7a2014-02-27 10:00:09 +0100118 git config remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
119 git config --add remote.origin.fetch refs/heads/*:refs/remotes/origin/* &&
120
121 git fetch --prune origin &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000122 git rev-parse origin/main &&
Carlos Martín Nietof377e7a2014-02-27 10:00:09 +0100123 git rev-parse origin/pr/42
124'
125
Michael Haggerty0838bf42013-10-30 06:33:00 +0100126test_expect_success 'fetch --prune --tags prunes branches but not tags' '
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200127 cd "$D" &&
128 git clone . prune-tags &&
129 cd prune-tags &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000130 git tag sometag main &&
Michael Haggerty68a304d2013-10-23 17:50:36 +0200131 # Create what looks like a remote-tracking branch from an earlier
132 # fetch that has since been deleted from the remote:
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000133 git update-ref refs/remotes/origin/fake-remote main &&
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200134
135 git fetch --prune --tags origin &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000136 git rev-parse origin/main &&
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100137 test_must_fail git rev-parse origin/fake-remote &&
Michael Haggerty0838bf42013-10-30 06:33:00 +0100138 git rev-parse sometag
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200139'
140
Michael Haggerty0838bf42013-10-30 06:33:00 +0100141test_expect_success 'fetch --prune --tags with branch does not prune other things' '
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200142 cd "$D" &&
143 git clone . prune-tags-branch &&
144 cd prune-tags-branch &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000145 git tag sometag main &&
146 git update-ref refs/remotes/origin/extrabranch main &&
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200147
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000148 git fetch --prune --tags origin main &&
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100149 git rev-parse origin/extrabranch &&
Michael Haggerty0838bf42013-10-30 06:33:00 +0100150 git rev-parse sometag
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100151'
152
153test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
154 cd "$D" &&
155 git clone . prune-tags-refspec &&
156 cd prune-tags-refspec &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000157 git tag sometag main &&
158 git update-ref refs/remotes/origin/foo/otherbranch main &&
159 git update-ref refs/remotes/origin/extrabranch main &&
Michael Haggertyc5a84e92013-10-30 06:32:59 +0100160
161 git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
162 test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
163 git rev-parse origin/extrabranch &&
Michael Haggerty0838bf42013-10-30 06:33:00 +0100164 git rev-parse sometag
Carlos Martín Nieto43a8a042011-10-08 00:51:07 +0200165'
166
Patrick Steinhardtc7b190d2021-01-12 13:27:52 +0100167test_expect_success 'fetch --atomic works with a single branch' '
168 test_when_finished "rm -rf \"$D\"/atomic" &&
169
170 cd "$D" &&
171 git clone . atomic &&
172 git branch atomic-branch &&
173 oid=$(git rev-parse atomic-branch) &&
174 echo "$oid" >expected &&
175
176 git -C atomic fetch --atomic origin &&
177 git -C atomic rev-parse origin/atomic-branch >actual &&
178 test_cmp expected actual &&
179 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
180'
181
182test_expect_success 'fetch --atomic works with multiple branches' '
183 test_when_finished "rm -rf \"$D\"/atomic" &&
184
185 cd "$D" &&
186 git clone . atomic &&
187 git branch atomic-branch-1 &&
188 git branch atomic-branch-2 &&
189 git branch atomic-branch-3 &&
190 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
191
192 git -C atomic fetch --atomic origin &&
193 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
194 test_cmp expected actual
195'
196
197test_expect_success 'fetch --atomic works with mixed branches and tags' '
198 test_when_finished "rm -rf \"$D\"/atomic" &&
199
200 cd "$D" &&
201 git clone . atomic &&
202 git branch atomic-mixed-branch &&
203 git tag atomic-mixed-tag &&
204 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
205
206 git -C atomic fetch --tags --atomic origin &&
207 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
208 test_cmp expected actual
209'
210
211test_expect_success 'fetch --atomic prunes references' '
212 test_when_finished "rm -rf \"$D\"/atomic" &&
213
214 cd "$D" &&
215 git branch atomic-prune-delete &&
216 git clone . atomic &&
217 git branch --delete atomic-prune-delete &&
218 git branch atomic-prune-create &&
219 git rev-parse refs/heads/atomic-prune-create >actual &&
220
221 git -C atomic fetch --prune --atomic origin &&
222 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
223 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
224 test_cmp expected actual
225'
226
227test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
228 test_when_finished "rm -rf \"$D\"/atomic" &&
229
230 cd "$D" &&
231 git branch atomic-non-ff &&
232 git clone . atomic &&
233 git rev-parse HEAD >actual &&
234
235 git branch atomic-new-branch &&
236 parent_commit=$(git rev-parse atomic-non-ff~) &&
237 git update-ref refs/heads/atomic-non-ff $parent_commit &&
238
239 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
240 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
241 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
242 test_cmp expected actual &&
243 test_must_be_empty atomic/.git/FETCH_HEAD
244'
245
246test_expect_success 'fetch --atomic executes a single reference transaction only' '
247 test_when_finished "rm -rf \"$D\"/atomic" &&
248
249 cd "$D" &&
250 git clone . atomic &&
251 git branch atomic-hooks-1 &&
252 git branch atomic-hooks-2 &&
253 head_oid=$(git rev-parse HEAD) &&
254
255 cat >expected <<-EOF &&
256 prepared
257 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
258 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
259 committed
260 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
261 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
262 EOF
263
264 rm -f atomic/actual &&
265 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
266 ( echo "$*" && cat ) >>actual
267 EOF
268
269 git -C atomic fetch --atomic origin &&
270 test_cmp expected atomic/actual
271'
272
273test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
274 test_when_finished "rm -rf \"$D\"/atomic" &&
275
276 cd "$D" &&
277 git clone . atomic &&
278 git branch atomic-hooks-abort-1 &&
279 git branch atomic-hooks-abort-2 &&
280 git branch atomic-hooks-abort-3 &&
281 git tag atomic-hooks-abort &&
282 head_oid=$(git rev-parse HEAD) &&
283
284 cat >expected <<-EOF &&
285 prepared
286 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
287 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
288 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
289 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
290 aborted
291 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
292 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
293 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
294 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
295 EOF
296
297 rm -f atomic/actual &&
298 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
299 ( echo "$*" && cat ) >>actual
300 exit 1
301 EOF
302
303 git -C atomic for-each-ref >expected-refs &&
304 test_must_fail git -C atomic fetch --tags --atomic origin &&
305 git -C atomic for-each-ref >actual-refs &&
306 test_cmp expected-refs actual-refs &&
307 test_must_be_empty atomic/.git/FETCH_HEAD
308'
309
310test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
311 test_when_finished "rm -rf \"$D\"/atomic" &&
312
313 cd "$D" &&
314 git clone . atomic &&
315 oid=$(git rev-parse HEAD) &&
316
317 git branch atomic-fetch-head-1 &&
318 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
319 test_line_count = 1 atomic/.git/FETCH_HEAD &&
320
321 git branch atomic-fetch-head-2 &&
322 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
323 test_line_count = 2 atomic/.git/FETCH_HEAD &&
324 cp atomic/.git/FETCH_HEAD expected &&
325
326 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
327 exit 1
328 EOF
329
330 git branch atomic-fetch-head-3 &&
331 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
332 test_cmp expected atomic/.git/FETCH_HEAD
333'
334
Derrick Stoleeb40a5022020-01-21 01:38:12 +0000335test_expect_success '--refmap="" ignores configured refspec' '
336 cd "$TRASH_DIRECTORY" &&
337 git clone "$D" remote-refs &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000338 git -C remote-refs rev-parse remotes/origin/main >old &&
339 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
340 git -C remote-refs rev-parse remotes/origin/main >new &&
Derrick Stoleeb40a5022020-01-21 01:38:12 +0000341 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000342 git -C remote-refs rev-parse remotes/origin/main >actual &&
Derrick Stoleeb40a5022020-01-21 01:38:12 +0000343 test_cmp new actual &&
344 git -C remote-refs fetch origin &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000345 git -C remote-refs rev-parse remotes/origin/main >actual &&
Derrick Stoleeb40a5022020-01-21 01:38:12 +0000346 test_cmp old actual
347'
348
349test_expect_success '--refmap="" and --prune' '
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000350 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
351 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
Derrick Stoleeb40a5022020-01-21 01:38:12 +0000352 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
353 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
354 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
355 git -C remote-refs fetch --prune origin &&
356 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
357'
358
Väinö Järveläf539d0d2007-10-09 11:51:07 +0300359test_expect_success 'fetch tags when there is no tags' '
360
361 cd "$D" &&
362
363 mkdir notags &&
364 cd notags &&
365 git init &&
366
367 git fetch -t ..
368
369'
370
Junio C Hamano6c96c0f2006-11-18 21:39:17 -0800371test_expect_success 'fetch following tags' '
372
373 cd "$D" &&
Martin Ågrenc76b84a2020-08-06 22:08:53 +0200374 git tag -a -m "annotated" anno HEAD &&
Junio C Hamano6c96c0f2006-11-18 21:39:17 -0800375 git tag light HEAD &&
376
377 mkdir four &&
378 cd four &&
Nicolas Pitre5c94f872007-01-12 16:01:46 -0500379 git init &&
Junio C Hamano6c96c0f2006-11-18 21:39:17 -0800380
381 git fetch .. :track &&
382 git show-ref --verify refs/tags/anno &&
383 git show-ref --verify refs/tags/light
384
385'
386
Marc Branchaud0997ada2012-04-16 18:08:50 -0400387test_expect_success 'fetch uses remote ref names to describe new refs' '
388 cd "$D" &&
389 git init descriptive &&
390 (
391 cd descriptive &&
392 git config remote.o.url .. &&
393 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
394 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
395 git fetch o
396 ) &&
397 git tag -a -m "Descriptive tag" descriptive-tag &&
398 git branch descriptive-branch &&
399 git checkout descriptive-branch &&
400 echo "Nuts" >crazy &&
401 git add crazy &&
402 git commit -a -m "descriptive commit" &&
403 git update-ref refs/others/crazy HEAD &&
404 (
405 cd descriptive &&
406 git fetch o 2>actual &&
SZEDER Gábor927c1a62018-02-08 16:56:52 +0100407 test_i18ngrep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
408 test_i18ngrep "new tag.* -> descriptive-tag$" actual &&
409 test_i18ngrep "new ref.* -> crazy$" actual
Marc Branchaud0997ada2012-04-16 18:08:50 -0400410 ) &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000411 git checkout main
Marc Branchaud0997ada2012-04-16 18:08:50 -0400412'
413
Junio C Hamano41ac4142008-02-01 01:50:53 -0800414test_expect_success 'fetch must not resolve short tag name' '
Steffen Prohaska605b4972007-11-11 15:01:48 +0100415
416 cd "$D" &&
417
418 mkdir five &&
419 cd five &&
420 git init &&
421
Stephan Beyerd492b312008-07-12 17:47:52 +0200422 test_must_fail git fetch .. anno:five
Steffen Prohaska605b4972007-11-11 15:01:48 +0100423
424'
425
Junio C Hamanodd621df2011-11-04 14:14:05 -0700426test_expect_success 'fetch can now resolve short remote name' '
Steffen Prohaska605b4972007-11-11 15:01:48 +0100427
428 cd "$D" &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500429 git update-ref refs/remotes/six/HEAD HEAD &&
Steffen Prohaska605b4972007-11-11 15:01:48 +0100430
431 mkdir six &&
432 cd six &&
433 git init &&
434
Junio C Hamanodd621df2011-11-04 14:14:05 -0700435 git fetch .. six:six
Steffen Prohaska605b4972007-11-11 15:01:48 +0100436'
437
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100438test_expect_success 'create bundle 1' '
439 cd "$D" &&
440 echo >file updated again by origin &&
441 git commit -a -m "tip" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000442 git bundle create --version=3 bundle1 main^..main
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100443'
444
Johannes Schindelin83155882007-03-06 22:57:07 +0100445test_expect_success 'header of bundle looks right' '
brian m. carlsonc5aecfc2020-07-29 23:14:20 +0000446 cat >expect <<-EOF &&
447 # v3 git bundle
448 @object-format=$(test_oid algo)
449 -OID updated by origin
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000450 OID refs/heads/main
brian m. carlsonc5aecfc2020-07-29 23:14:20 +0000451
452 EOF
453 sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
454 test_cmp expect actual
Johannes Schindelin83155882007-03-06 22:57:07 +0100455'
456
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100457test_expect_success 'create bundle 2' '
458 cd "$D" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000459 git bundle create bundle2 main~2..main
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100460'
461
Junio C Hamano41ac4142008-02-01 01:50:53 -0800462test_expect_success 'unbundle 1' '
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100463 cd "$D/bundle" &&
464 git checkout -b some-branch &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000465 test_must_fail git fetch "$D/bundle1" main:main
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100466'
467
Junio C Hamano2e674a92009-08-07 20:12:13 -0700468
Johannes Schindelin83155882007-03-06 22:57:07 +0100469test_expect_success 'bundle 1 has only 3 files ' '
470 cd "$D" &&
Jiang Xin99011642021-01-11 21:27:01 -0500471 test_bundle_object_count bundle1 3
Johannes Schindelin83155882007-03-06 22:57:07 +0100472'
473
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100474test_expect_success 'unbundle 2' '
475 cd "$D/bundle" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000476 git fetch ../bundle2 main:main &&
477 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100478'
479
Johannes Schindelin18449ab2007-03-08 00:43:05 +0100480test_expect_success 'bundle does not prerequisite objects' '
481 cd "$D" &&
482 touch file2 &&
483 git add file2 &&
484 git commit -m add.file2 file2 &&
485 git bundle create bundle3 -1 HEAD &&
Jiang Xin99011642021-01-11 21:27:01 -0500486 test_bundle_object_count bundle3 3
Johannes Schindelin18449ab2007-03-08 00:43:05 +0100487'
488
Junio C Hamano7fa82542007-08-08 17:01:49 -0700489test_expect_success 'bundle should be able to create a full history' '
490
491 cd "$D" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000492 git tag -a -m "1.0" v1.0 main &&
Junio C Hamano7fa82542007-08-08 17:01:49 -0700493 git bundle create bundle4 v1.0
494
495'
496
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100497test_expect_success 'fetch with a non-applying branch.<name>.merge' '
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000498 git config branch.main.remote yeti &&
499 git config branch.main.merge refs/heads/bigfoot &&
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100500 git config remote.blub.url one &&
501 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
502 git fetch blub
503'
504
Brandon Casey042cca32010-09-09 13:56:37 -0500505# URL supplied to fetch does not match the url of the configured branch's remote
506test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
507 one_head=$(cd one && git rev-parse HEAD) &&
508 this_head=$(git rev-parse HEAD) &&
Brandon Casey6106ce42010-08-25 12:52:55 -0500509 git update-ref -d FETCH_HEAD &&
510 git fetch one &&
Brandon Casey042cca32010-09-09 13:56:37 -0500511 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
512 test $this_head = "$(git rev-parse --verify HEAD)"
513'
514
515# URL supplied to fetch matches the url of the configured branch's remote and
516# the merge spec matches the branch the remote HEAD points to
517test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
518 one_ref=$(cd one && git symbolic-ref HEAD) &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000519 git config branch.main.remote blub &&
520 git config branch.main.merge "$one_ref" &&
Brandon Casey042cca32010-09-09 13:56:37 -0500521 git update-ref -d FETCH_HEAD &&
522 git fetch one &&
523 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
524 test $this_head = "$(git rev-parse --verify HEAD)"
525'
526
527# URL supplied to fetch matches the url of the configured branch's remote, but
528# the merge spec does not match the branch the remote HEAD points to
529test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000530 git config branch.main.merge "${one_ref}_not" &&
Brandon Casey042cca32010-09-09 13:56:37 -0500531 git update-ref -d FETCH_HEAD &&
532 git fetch one &&
533 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
534 test $this_head = "$(git rev-parse --verify HEAD)"
Brandon Casey6106ce42010-08-25 12:52:55 -0500535'
536
Johannes Sixtc2015b32007-11-04 21:26:22 +0100537# the strange name is: a\!'b
538test_expect_success 'quoting of a strangely named repo' '
Stephan Beyerd492b312008-07-12 17:47:52 +0200539 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
Johannes Sixtc2015b32007-11-04 21:26:22 +0100540 grep "fatal: '\''a\\\\!'\''b'\''" result
541'
542
Johannes Schindelinc5546e82007-11-22 12:24:59 +0000543test_expect_success 'bundle should record HEAD correctly' '
544
545 cd "$D" &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000546 git bundle create bundle5 HEAD main &&
Johannes Schindelinc5546e82007-11-22 12:24:59 +0000547 git bundle list-heads bundle5 >actual &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000548 for h in HEAD refs/heads/main
Johannes Schindelinc5546e82007-11-22 12:24:59 +0000549 do
Eric Sunshined0fd9932021-12-09 00:11:14 -0500550 echo "$(git rev-parse --verify $h) $h" || return 1
Johannes Schindelinc5546e82007-11-22 12:24:59 +0000551 done >expect &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400552 test_cmp expect actual
Johannes Schindelinc5546e82007-11-22 12:24:59 +0000553
554'
555
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000556test_expect_success 'mark initial state of origin/main' '
Jeff King51f8c812013-05-11 18:14:03 +0200557 (
558 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000559 git tag base-origin-main refs/remotes/origin/main
Jeff King51f8c812013-05-11 18:14:03 +0200560 )
561'
562
Jeff Kingf2690482013-05-11 18:16:52 +0200563test_expect_success 'explicit fetch should update tracking' '
Junio C Hamanoc7015962007-12-04 21:58:42 -0800564
565 cd "$D" &&
566 git branch -f side &&
567 (
568 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000569 git update-ref refs/remotes/origin/main base-origin-main &&
570 o=$(git rev-parse --verify refs/remotes/origin/main) &&
571 git fetch origin main &&
572 n=$(git rev-parse --verify refs/remotes/origin/main) &&
Jeff Kingf2690482013-05-11 18:16:52 +0200573 test "$o" != "$n" &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200574 test_must_fail git rev-parse --verify refs/remotes/origin/side
Junio C Hamanoc7015962007-12-04 21:58:42 -0800575 )
576'
577
Jeff Kingf2690482013-05-11 18:16:52 +0200578test_expect_success 'explicit pull should update tracking' '
Junio C Hamanoc7015962007-12-04 21:58:42 -0800579
580 cd "$D" &&
581 git branch -f side &&
582 (
583 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000584 git update-ref refs/remotes/origin/main base-origin-main &&
585 o=$(git rev-parse --verify refs/remotes/origin/main) &&
586 git pull origin main &&
587 n=$(git rev-parse --verify refs/remotes/origin/main) &&
Jeff Kingf2690482013-05-11 18:16:52 +0200588 test "$o" != "$n" &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200589 test_must_fail git rev-parse --verify refs/remotes/origin/side
Junio C Hamanoc7015962007-12-04 21:58:42 -0800590 )
591'
592
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700593test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
594 cd "$D" &&
595 (
596 cd three &&
597 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
598 )
599'
600
601test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
602 cd "$D" &&
603 git branch -f side &&
604 (
605 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000606 git update-ref refs/remotes/origin/main base-origin-main &&
607 o=$(git rev-parse --verify refs/remotes/origin/main) &&
608 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
609 n=$(git rev-parse --verify refs/remotes/origin/main) &&
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700610 test "$o" = "$n" &&
611 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000612 git rev-parse --verify refs/remotes/other/main
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700613 )
614'
615
616test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
617 cd "$D" &&
618 git branch -f side &&
619 (
620 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000621 git update-ref refs/remotes/origin/main base-origin-main &&
622 o=$(git rev-parse --verify refs/remotes/origin/main) &&
623 git fetch --refmap="" origin main &&
624 n=$(git rev-parse --verify refs/remotes/origin/main) &&
Junio C Hamanoc5558f82014-05-29 15:21:31 -0700625 test "$o" = "$n" &&
626 test_must_fail git rev-parse --verify refs/remotes/origin/side
627 )
628'
629
Junio C Hamanoc7015962007-12-04 21:58:42 -0800630test_expect_success 'configured fetch updates tracking' '
631
632 cd "$D" &&
633 git branch -f side &&
634 (
635 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000636 git update-ref refs/remotes/origin/main base-origin-main &&
637 o=$(git rev-parse --verify refs/remotes/origin/main) &&
Junio C Hamanoc7015962007-12-04 21:58:42 -0800638 git fetch origin &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000639 n=$(git rev-parse --verify refs/remotes/origin/main) &&
Junio C Hamanoc7015962007-12-04 21:58:42 -0800640 test "$o" != "$n" &&
641 git rev-parse --verify refs/remotes/origin/side
642 )
643'
644
John Keeping823c6d52013-05-27 17:33:09 +0100645test_expect_success 'non-matching refspecs do not confuse tracking update' '
646 cd "$D" &&
647 git update-ref refs/odd/location HEAD &&
648 (
649 cd three &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000650 git update-ref refs/remotes/origin/main base-origin-main &&
John Keeping823c6d52013-05-27 17:33:09 +0100651 git config --add remote.origin.fetch \
652 refs/odd/location:refs/remotes/origin/odd &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000653 o=$(git rev-parse --verify refs/remotes/origin/main) &&
654 git fetch origin main &&
655 n=$(git rev-parse --verify refs/remotes/origin/main) &&
John Keeping823c6d52013-05-27 17:33:09 +0100656 test "$o" != "$n" &&
657 test_must_fail git rev-parse --verify refs/remotes/origin/odd
658 )
659'
660
Jeff King9a7bbd12008-06-16 12:15:02 -0400661test_expect_success 'pushing nonexistent branch by mistake should not segv' '
662
663 cd "$D" &&
664 test_must_fail git push seven no:no
665
666'
667
Junio C Hamano49420252008-09-21 23:50:01 -0700668test_expect_success 'auto tag following fetches minimum' '
669
670 cd "$D" &&
671 git clone .git follow &&
672 git checkout HEAD^0 &&
673 (
674 for i in 1 2 3 4 5 6 7
675 do
676 echo $i >>file &&
677 git commit -m $i -a &&
678 git tag -a -m $i excess-$i || exit 1
679 done
680 ) &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000681 git checkout main &&
Junio C Hamano49420252008-09-21 23:50:01 -0700682 (
683 cd follow &&
684 git fetch
685 )
686'
687
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200688test_expect_success 'refuse to fetch into the current branch' '
689
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000690 test_must_fail git fetch . side:main
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200691
692'
693
694test_expect_success 'fetch into the current branch with --update-head-ok' '
695
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000696 git fetch --update-head-ok . side:main
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200697
698'
699
Jonathan Tandb3c2932020-09-02 14:05:39 -0700700test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
701 rm -f .git/FETCH_HEAD err &&
702 git fetch --dry-run . 2>err &&
703 ! test -f .git/FETCH_HEAD &&
704 grep FETCH_HEAD err
Johannes Sixt3fdcdbd2010-02-25 21:03:44 +0100705'
706
Jonathan Tandb3c2932020-09-02 14:05:39 -0700707test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
708 rm -f .git/FETCH_HEAD err &&
709 git fetch --no-write-fetch-head . 2>err &&
710 ! test -f .git/FETCH_HEAD &&
711 ! grep FETCH_HEAD err
Junio C Hamano887952b2020-08-18 14:25:22 +0000712'
713
714test_expect_success '--write-fetch-head gets defeated by --dry-run' '
715 rm -f .git/FETCH_HEAD &&
716 git fetch --dry-run --write-fetch-head . &&
717 ! test -f .git/FETCH_HEAD
718'
719
Julian Phillips95c96d42009-11-13 21:25:56 +0000720test_expect_success "should be able to fetch with duplicate refspecs" '
Thomas Rastaa982852012-03-01 22:40:49 +0100721 mkdir dups &&
722 (
723 cd dups &&
724 git init &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +0000725 git config branch.main.remote three &&
Thomas Rastaa982852012-03-01 22:40:49 +0100726 git config remote.three.url ../three/.git &&
727 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
728 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
729 git fetch three
730 )
Julian Phillips95c96d42009-11-13 21:25:56 +0000731'
732
Junio C Hamano60650a42018-08-01 09:22:37 -0700733test_expect_success 'LHS of refspec follows ref disambiguation rules' '
734 mkdir lhs-ambiguous &&
735 (
736 cd lhs-ambiguous &&
737 git init server &&
738 test_commit -C server unwanted &&
739 test_commit -C server wanted &&
740
741 git init client &&
742
743 # Check a name coming after "refs" alphabetically ...
744 git -C server update-ref refs/heads/s wanted &&
745 git -C server update-ref refs/heads/refs/heads/s unwanted &&
746 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
747 git -C server rev-parse wanted >expect &&
748 git -C client rev-parse checkthis >actual &&
749 test_cmp expect actual &&
750
751 # ... and one before.
752 git -C server update-ref refs/heads/q wanted &&
753 git -C server update-ref refs/heads/refs/heads/q unwanted &&
754 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
755 git -C server rev-parse wanted >expect &&
756 git -C client rev-parse checkthis >actual &&
757 test_cmp expect actual &&
758
759 # Tags are preferred over branches like refs/{heads,tags}/*
760 git -C server update-ref refs/tags/t wanted &&
761 git -C server update-ref refs/heads/t unwanted &&
762 git -C client fetch ../server +t:refs/heads/checkthis &&
763 git -C server rev-parse wanted >expect &&
764 git -C client rev-parse checkthis >actual
765 )
766'
767
Derrick Stolee50f26bd2019-09-02 19:22:02 -0700768test_expect_success 'fetch.writeCommitGraph' '
769 git clone three write &&
770 (
771 cd three &&
772 test_commit new
773 ) &&
774 (
775 cd write &&
776 git -c fetch.writeCommitGraph fetch origin &&
777 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
778 )
779'
780
Derrick Stoleecb99a342019-10-24 13:40:42 +0000781test_expect_success 'fetch.writeCommitGraph with submodules' '
Derrick Stoleee88aab92019-10-24 13:40:41 +0000782 git clone dups super &&
783 (
784 cd super &&
785 git submodule add "file://$TRASH_DIRECTORY/three" &&
786 git commit -m "add submodule"
787 ) &&
788 git clone "super" super-clone &&
789 (
790 cd super-clone &&
791 rm -rf .git/objects/info &&
792 git -c fetch.writeCommitGraph=true fetch origin &&
793 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
794 )
795'
796
Michael Schubert737c5a92013-07-13 11:36:24 +0200797# configured prune tests
798
799set_config_tristate () {
800 # var=$1 val=$2
801 case "$2" in
Ævar Arnfjörð Bjarmason59caf522018-02-09 20:32:09 +0000802 unset)
803 test_unconfig "$1"
804 ;;
805 *)
806 git config "$1" "$2"
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000807 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
808 git_fetch_c="$git_fetch_c -c $key=$2"
Ævar Arnfjörð Bjarmason59caf522018-02-09 20:32:09 +0000809 ;;
Michael Schubert737c5a92013-07-13 11:36:24 +0200810 esac
811}
812
813test_configured_prune () {
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000814 test_configured_prune_type "$@" "name"
815 test_configured_prune_type "$@" "link"
816}
Michael Schubert737c5a92013-07-13 11:36:24 +0200817
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000818test_configured_prune_type () {
Ævar Arnfjörð Bjarmasoneca142d2018-02-09 20:32:04 +0000819 fetch_prune=$1
820 remote_origin_prune=$2
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000821 fetch_prune_tags=$3
822 remote_origin_prune_tags=$4
823 expected_branch=$5
824 expected_tag=$6
825 cmdline=$7
826 mode=$8
Michael Schubert737c5a92013-07-13 11:36:24 +0200827
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000828 if test -z "$cmdline_setup"
829 then
830 test_expect_success 'setup cmdline_setup variable for subsequent test' '
831 remote_url="file://$(git -C one config remote.origin.url)" &&
832 remote_fetch="$(git -C one config remote.origin.fetch)" &&
833 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
834 '
835 fi
836
837 if test "$mode" = 'link'
838 then
839 new_cmdline=""
840
841 if test "$cmdline" = ""
842 then
843 new_cmdline=$cmdline_setup
844 else
845 new_cmdline=$(printf "%s" "$cmdline" | perl -pe 's[origin(?!/)]["'"$remote_url"'"]g')
846 fi
847
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000848 if test "$fetch_prune_tags" = 'true' ||
849 test "$remote_origin_prune_tags" = 'true'
850 then
851 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
852 then
853 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
854 fi
855 fi
856
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000857 cmdline="$new_cmdline"
858 fi
859
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000860 test_expect_success "$mode prune fetch.prune=$1 remote.origin.prune=$2 fetch.pruneTags=$3 remote.origin.pruneTags=$4${7:+ $7}; branch:$5 tag:$6" '
Michael Schubert737c5a92013-07-13 11:36:24 +0200861 # make sure a newbranch is there in . and also in one
862 git branch -f newbranch &&
Ævar Arnfjörð Bjarmasonca3065e2018-02-09 20:32:06 +0000863 git tag -f newtag &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200864 (
865 cd one &&
866 test_unconfig fetch.prune &&
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000867 test_unconfig fetch.pruneTags &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200868 test_unconfig remote.origin.prune &&
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000869 test_unconfig remote.origin.pruneTags &&
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000870 git fetch '"$cmdline_setup"' &&
Ævar Arnfjörð Bjarmasonca3065e2018-02-09 20:32:06 +0000871 git rev-parse --verify refs/remotes/origin/newbranch &&
872 git rev-parse --verify refs/tags/newtag
Jeff King99094a72015-03-20 06:07:15 -0400873 ) &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200874
Ævar Arnfjörð Bjarmasond3651122018-08-13 19:22:48 +0000875 # now remove them
Michael Schubert737c5a92013-07-13 11:36:24 +0200876 git branch -d newbranch &&
Ævar Arnfjörð Bjarmasonca3065e2018-02-09 20:32:06 +0000877 git tag -d newtag &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200878
879 # then test
880 (
881 cd one &&
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000882 git_fetch_c="" &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200883 set_config_tristate fetch.prune $fetch_prune &&
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000884 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200885 set_config_tristate remote.origin.prune $remote_origin_prune &&
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000886 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
Michael Schubert737c5a92013-07-13 11:36:24 +0200887
Ævar Arnfjörð Bjarmasone1790f92018-02-09 20:32:10 +0000888 if test "$mode" != "link"
889 then
890 git_fetch_c=""
891 fi &&
892 git$git_fetch_c fetch '"$cmdline"' &&
Ævar Arnfjörð Bjarmasoneca142d2018-02-09 20:32:04 +0000893 case "$expected_branch" in
Michael Schubert737c5a92013-07-13 11:36:24 +0200894 pruned)
895 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
896 ;;
897 kept)
898 git rev-parse --verify refs/remotes/origin/newbranch
899 ;;
Ævar Arnfjörð Bjarmasonca3065e2018-02-09 20:32:06 +0000900 esac &&
901 case "$expected_tag" in
902 pruned)
903 test_must_fail git rev-parse --verify refs/tags/newtag
904 ;;
905 kept)
906 git rev-parse --verify refs/tags/newtag
907 ;;
Michael Schubert737c5a92013-07-13 11:36:24 +0200908 esac
909 )
910 '
911}
912
Ævar Arnfjörð Bjarmasonbf16ab72018-02-09 20:32:05 +0000913# $1 config: fetch.prune
914# $2 config: remote.<name>.prune
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000915# $3 config: fetch.pruneTags
916# $4 config: remote.<name>.pruneTags
917# $5 expect: branch to be pruned?
918# $6 expect: tag to be pruned?
919# $7 git-fetch $cmdline:
Ævar Arnfjörð Bjarmasonbf16ab72018-02-09 20:32:05 +0000920#
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000921# $1 $2 $3 $4 $5 $6 $7
922test_configured_prune unset unset unset unset kept kept ""
923test_configured_prune unset unset unset unset kept kept "--no-prune"
924test_configured_prune unset unset unset unset pruned kept "--prune"
925test_configured_prune unset unset unset unset kept pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000926 "--prune origin refs/tags/*:refs/tags/*"
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000927test_configured_prune unset unset unset unset pruned pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000928 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
Michael Schubert737c5a92013-07-13 11:36:24 +0200929
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000930test_configured_prune false unset unset unset kept kept ""
931test_configured_prune false unset unset unset kept kept "--no-prune"
932test_configured_prune false unset unset unset pruned kept "--prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200933
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000934test_configured_prune true unset unset unset pruned kept ""
935test_configured_prune true unset unset unset pruned kept "--prune"
936test_configured_prune true unset unset unset kept kept "--no-prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200937
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000938test_configured_prune unset false unset unset kept kept ""
939test_configured_prune unset false unset unset kept kept "--no-prune"
940test_configured_prune unset false unset unset pruned kept "--prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200941
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000942test_configured_prune false false unset unset kept kept ""
943test_configured_prune false false unset unset kept kept "--no-prune"
944test_configured_prune false false unset unset pruned kept "--prune"
945test_configured_prune false false unset unset kept pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000946 "--prune origin refs/tags/*:refs/tags/*"
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000947test_configured_prune false false unset unset pruned pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000948 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
Michael Schubert737c5a92013-07-13 11:36:24 +0200949
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000950test_configured_prune true false unset unset kept kept ""
951test_configured_prune true false unset unset pruned kept "--prune"
952test_configured_prune true false unset unset kept kept "--no-prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200953
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000954test_configured_prune unset true unset unset pruned kept ""
955test_configured_prune unset true unset unset kept kept "--no-prune"
956test_configured_prune unset true unset unset pruned kept "--prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200957
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000958test_configured_prune false true unset unset pruned kept ""
959test_configured_prune false true unset unset kept kept "--no-prune"
960test_configured_prune false true unset unset pruned kept "--prune"
Michael Schubert737c5a92013-07-13 11:36:24 +0200961
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000962test_configured_prune true true unset unset pruned kept ""
963test_configured_prune true true unset unset pruned kept "--prune"
964test_configured_prune true true unset unset kept kept "--no-prune"
965test_configured_prune true true unset unset kept pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000966 "--prune origin refs/tags/*:refs/tags/*"
Ævar Arnfjörð Bjarmasone249ce02018-02-09 20:32:14 +0000967test_configured_prune true true unset unset pruned pruned \
Ævar Arnfjörð Bjarmason6fb23f52018-02-09 20:32:07 +0000968 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
Michael Schubert737c5a92013-07-13 11:36:24 +0200969
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000970# --prune-tags on its own does nothing, needs --prune as well, same
Elijah Newren6d12b532020-07-28 20:45:38 +0000971# for fetch.pruneTags without fetch.prune
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000972test_configured_prune unset unset unset unset kept kept "--prune-tags"
973test_configured_prune unset unset true unset kept kept ""
974test_configured_prune unset unset unset true kept kept ""
975
976# These will prune the tags
977test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
978test_configured_prune true unset true unset pruned pruned ""
979test_configured_prune unset true unset true pruned pruned ""
980
981# remote.<name>.pruneTags overrides fetch.pruneTags, just like
982# remote.<name>.prune overrides fetch.prune if set.
983test_configured_prune true unset true unset pruned pruned ""
984test_configured_prune false true false true pruned pruned ""
985test_configured_prune true false true false kept kept ""
986
987# When --prune-tags is supplied it's ignored if an explicit refspec is
988# given, same for the configuration options.
989test_configured_prune unset unset unset unset pruned kept \
990 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
991test_configured_prune unset unset true unset pruned kept \
992 "--prune origin +refs/heads/*:refs/remotes/origin/*"
993test_configured_prune unset unset unset true pruned kept \
994 "--prune origin +refs/heads/*:refs/remotes/origin/*"
995
996# Pruning that also takes place if a file:// url replaces a named
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +0000997# remote. However, because there's no implicit
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +0000998# +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
999# command-line negates --prune-tags, the branches will not be pruned.
1000test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1001test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1002test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001003test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001004test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001005test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001006test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1007test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1008test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1009test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1010test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1011test_configured_prune_type true unset true unset kept pruned "origin" "link"
1012test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1013test_configured_prune_type unset true true unset kept pruned "origin" "link"
1014test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1015test_configured_prune_type unset true unset true kept pruned "origin" "link"
1016
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001017# When all remote.origin.fetch settings are deleted a --prune
1018# --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1019# tags, but not tracking branches, will be deleted.
Ævar Arnfjörð Bjarmason97716d22018-02-09 20:32:15 +00001020test_expect_success 'remove remote.origin.fetch "one"' '
1021 (
1022 cd one &&
1023 git config --unset-all remote.origin.fetch
1024 )
1025'
1026test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
Ævar Arnfjörð Bjarmason63179722018-02-09 20:32:16 +00001027test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
Michael Schubert737c5a92013-07-13 11:36:24 +02001028
Thomas Rastefe4be12012-03-01 22:40:51 +01001029test_expect_success 'all boundary commits are excluded' '
1030 test_commit base &&
1031 test_commit oneside &&
1032 git checkout HEAD^ &&
1033 test_commit otherside &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +00001034 git checkout main &&
Thomas Rastefe4be12012-03-01 22:40:51 +01001035 test_tick &&
1036 git merge otherside &&
1037 ad=$(git log --no-walk --format=%ad HEAD) &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +00001038 git bundle create twoside-boundary.bdl main --since="$ad" &&
Jiang Xin99011642021-01-11 21:27:01 -05001039 test_bundle_object_count --thin twoside-boundary.bdl 3
Junio C Hamano7be1d622006-09-23 03:40:17 -07001040'
1041
Tom Miller4b3b33a2014-01-02 20:28:51 -06001042test_expect_success 'fetch --prune prints the remotes url' '
1043 git branch goodbye &&
1044 git clone . only-prunes &&
1045 git branch -D goodbye &&
1046 (
1047 cd only-prunes &&
1048 git fetch --prune origin 2>&1 | head -n1 >../actual
1049 ) &&
1050 echo "From ${D}/." >expect &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +01001051 test_cmp expect actual
Tom Miller4b3b33a2014-01-02 20:28:51 -06001052'
1053
Tom Miller10a6cc82014-01-02 20:28:52 -06001054test_expect_success 'branchname D/F conflict resolved by --prune' '
1055 git branch dir/file &&
1056 git clone . prune-df-conflict &&
1057 git branch -D dir/file &&
1058 git branch dir &&
1059 (
1060 cd prune-df-conflict &&
1061 git fetch --prune &&
1062 git rev-parse origin/dir >../actual
1063 ) &&
1064 git rev-parse dir >expect &&
1065 test_cmp expect actual
1066'
1067
Jeff King4c224082014-01-15 05:46:13 -05001068test_expect_success 'fetching a one-level ref works' '
1069 test_commit extra &&
1070 git reset --hard HEAD^ &&
1071 git update-ref refs/foo extra &&
1072 git init one-level &&
1073 (
1074 cd one-level &&
1075 git fetch .. HEAD refs/foo
1076 )
1077'
1078
Johannes Schindelin0898c962016-01-13 18:20:11 +01001079test_expect_success 'fetching with auto-gc does not lock up' '
1080 write_script askyesno <<-\EOF &&
1081 echo "$*" &&
1082 false
1083 EOF
1084 git clone "file://$D" auto-gc &&
1085 test_commit test2 &&
Michael J Gruber816c1932016-03-04 11:53:50 +01001086 (
1087 cd auto-gc &&
Kim Gybels12e73a32018-07-09 22:37:27 +02001088 git config fetch.unpackLimit 1 &&
Michael J Gruber816c1932016-03-04 11:53:50 +01001089 git config gc.autoPackLimit 1 &&
SZEDER Gáborbb055102016-05-01 17:37:43 +02001090 git config gc.autoDetach false &&
Derrick Stoleea95ce122020-09-17 18:11:44 +00001091 GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
Kim Gybels12e73a32018-07-09 22:37:27 +02001092 test_i18ngrep "Auto packing the repository" fetch.out &&
Michael J Gruber816c1932016-03-04 11:53:50 +01001093 ! grep "Should I try again" fetch.out
1094 )
Johannes Schindelin0898c962016-01-13 18:20:11 +01001095'
1096
Ævar Arnfjörð Bjarmasona926c4b2021-02-11 02:53:51 +01001097test_expect_success 'fetch aligned output' '
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +02001098 git clone . full-output &&
1099 test_commit looooooooooooong-tag &&
1100 (
1101 cd full-output &&
Pratik Karkia4d4e322018-03-27 23:16:37 +05451102 git -c fetch.output=full fetch origin >actual 2>&1 &&
1103 grep -e "->" actual | cut -c 22- >../actual
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +02001104 ) &&
1105 cat >expect <<-\EOF &&
Johannes Schindelin66713e82020-10-23 14:00:05 +00001106 main -> origin/main
Nguyễn Thái Ngọc Duy6bc91f22016-07-01 18:03:30 +02001107 looooooooooooong-tag -> looooooooooooong-tag
1108 EOF
1109 test_cmp expect actual
1110'
1111
Ævar Arnfjörð Bjarmasona926c4b2021-02-11 02:53:51 +01001112test_expect_success 'fetch compact output' '
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +02001113 git clone . compact &&
1114 test_commit extraaa &&
1115 (
1116 cd compact &&
Pratik Karkia4d4e322018-03-27 23:16:37 +05451117 git -c fetch.output=compact fetch origin >actual 2>&1 &&
1118 grep -e "->" actual | cut -c 22- >../actual
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +02001119 ) &&
1120 cat >expect <<-\EOF &&
Johannes Schindelin66713e82020-10-23 14:00:05 +00001121 main -> origin/*
Nguyễn Thái Ngọc Duybc437d12016-07-01 18:03:31 +02001122 extraaa -> *
1123 EOF
1124 test_cmp expect actual
1125'
1126
SZEDER Gábor12b18262019-08-01 17:53:07 +02001127test_expect_success '--no-show-forced-updates' '
1128 mkdir forced-updates &&
1129 (
1130 cd forced-updates &&
1131 git init &&
1132 test_commit 1 &&
1133 test_commit 2
1134 ) &&
1135 git clone forced-updates forced-update-clone &&
1136 git clone forced-updates no-forced-update-clone &&
1137 git -C forced-updates reset --hard HEAD~1 &&
1138 (
1139 cd forced-update-clone &&
1140 git fetch --show-forced-updates origin 2>output &&
1141 test_i18ngrep "(forced update)" output
1142 ) &&
1143 (
1144 cd no-forced-update-clone &&
1145 git fetch --no-show-forced-updates origin 2>output &&
1146 test_i18ngrep ! "(forced update)" output
1147 )
1148'
1149
Jonathan Tan3390e422018-07-02 15:39:44 -07001150setup_negotiation_tip () {
1151 SERVER="$1"
1152 URL="$2"
1153 USE_PROTOCOL_V2="$3"
1154
1155 rm -rf "$SERVER" client trace &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +00001156 git init -b main "$SERVER" &&
Jonathan Tan3390e422018-07-02 15:39:44 -07001157 test_commit -C "$SERVER" alpha_1 &&
1158 test_commit -C "$SERVER" alpha_2 &&
1159 git -C "$SERVER" checkout --orphan beta &&
1160 test_commit -C "$SERVER" beta_1 &&
1161 test_commit -C "$SERVER" beta_2 &&
1162
1163 git clone "$URL" client &&
1164
1165 if test "$USE_PROTOCOL_V2" -eq 1
1166 then
1167 git -C "$SERVER" config protocol.version 2 &&
1168 git -C client config protocol.version 2
1169 fi &&
1170
1171 test_commit -C "$SERVER" beta_s &&
Johannes Schindelin83ecf262020-12-17 01:07:06 +00001172 git -C "$SERVER" checkout main &&
Jonathan Tan3390e422018-07-02 15:39:44 -07001173 test_commit -C "$SERVER" alpha_s &&
1174 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1175}
1176
1177check_negotiation_tip () {
1178 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1179 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1180 grep "fetch> have $ALPHA_1" trace &&
1181 BETA_1=$(git -C client rev-parse beta_1) &&
1182 grep "fetch> have $BETA_1" trace &&
1183 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1184 ! grep "fetch> have $ALPHA_2" trace &&
1185 BETA_2=$(git -C client rev-parse beta_2) &&
1186 ! grep "fetch> have $BETA_2" trace
1187}
1188
1189test_expect_success '--negotiation-tip limits "have" lines sent' '
1190 setup_negotiation_tip server server 0 &&
1191 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1192 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1193 origin alpha_s beta_s &&
1194 check_negotiation_tip
1195'
1196
1197test_expect_success '--negotiation-tip understands globs' '
1198 setup_negotiation_tip server server 0 &&
1199 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1200 --negotiation-tip=*_1 \
1201 origin alpha_s beta_s &&
1202 check_negotiation_tip
1203'
1204
1205test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1206 setup_negotiation_tip server server 0 &&
1207 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1208 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1209 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1210 origin alpha_s beta_s &&
1211 check_negotiation_tip
1212'
1213
Jonathan Tan82823112021-07-15 10:44:32 -07001214test_expect_success '--negotiation-tip rejects missing OIDs' '
1215 setup_negotiation_tip server server 0 &&
1216 test_must_fail git -C client fetch \
1217 --negotiation-tip=alpha_1 \
1218 --negotiation-tip=$(test_oid zero) \
1219 origin alpha_s beta_s 2>err &&
1220 cat >fatal-expect <<-EOF &&
1221 fatal: the object $(test_oid zero) does not exist
1222EOF
1223 grep fatal: err >fatal-actual &&
1224 test_cmp fatal-expect fatal-actual
1225'
1226
Jonathan Tan3390e422018-07-02 15:39:44 -07001227. "$TEST_DIRECTORY"/lib-httpd.sh
1228start_httpd
1229
1230test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1231 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1232 "$HTTPD_URL/smart/server" 1 &&
1233 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1234 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1235 origin alpha_s beta_s &&
1236 check_negotiation_tip
1237'
1238
SZEDER Gábor12b18262019-08-01 17:53:07 +02001239# DO NOT add non-httpd-specific tests here, because the last part of this
1240# test script is only executed when httpd is available and enabled.
Derrick Stoleecdbd70c2019-06-18 13:25:26 -07001241
Junio C Hamano7be1d622006-09-23 03:40:17 -07001242test_done