1#!/bin/sh
2
3test_description='git remote porcelain-ish'
4
5. ./test-lib.sh
6
7setup_repository () {
8 mkdir "$1" && (
9 cd "$1" &&
10 git init &&
11 >file &&
12 git add file &&
13 test_tick &&
14 git commit -m "Initial" &&
15 git checkout -b side &&
16 >elif &&
17 git add elif &&
18 test_tick &&
19 git commit -m "Second" &&
20 git checkout master
21 )
22}
23
24tokens_match () {
25 echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
26 echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
27 test_cmp expect actual
28}
29
30check_remote_track () {
31 actual=$(git remote show "$1" | sed -ne 's|^ \(.*\) tracked$|\1|p')
32 shift &&
33 tokens_match "$*" "$actual"
34}
35
36check_tracking_branch () {
37 f="" &&
38 r=$(git for-each-ref "--format=%(refname)" |
39 sed -ne "s|^refs/remotes/$1/||p") &&
40 shift &&
41 tokens_match "$*" "$r"
42}
43
44test_expect_success setup '
45
46 setup_repository one &&
47 setup_repository two &&
48 (
49 cd two && git branch another
50 ) &&
51 git clone one test
52
53'
54
55test_expect_success 'remote information for the origin' '
56(
57 cd test &&
58 tokens_match origin "$(git remote)" &&
59 check_remote_track origin master side &&
60 check_tracking_branch origin HEAD master side
61)
62'
63
64test_expect_success 'add another remote' '
65(
66 cd test &&
67 git remote add -f second ../two &&
68 tokens_match "origin second" "$(git remote)" &&
69 check_remote_track origin master side &&
70 check_remote_track second master side another &&
71 check_tracking_branch second master side another &&
72 git for-each-ref "--format=%(refname)" refs/remotes |
73 sed -e "/^refs\/remotes\/origin\//d" \
74 -e "/^refs\/remotes\/second\//d" >actual &&
75 >expect &&
76 test_cmp expect actual
77)
78'
79
80test_expect_success 'remote forces tracking branches' '
81(
82 cd test &&
83 case `git config remote.second.fetch` in
84 +*) true ;;
85 *) false ;;
86 esac
87)
88'
89
90test_expect_success 'remove remote' '
91(
92 cd test &&
93 git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/master &&
94 git remote rm second
95)
96'
97
98test_expect_success 'remove remote' '
99(
100 cd test &&
101 tokens_match origin "$(git remote)" &&
102 check_remote_track origin master side &&
103 git for-each-ref "--format=%(refname)" refs/remotes |
104 sed -e "/^refs\/remotes\/origin\//d" >actual &&
105 >expect &&
106 test_cmp expect actual
107)
108'
109
110test_expect_success 'remove remote protects non-remote branches' '
111(
112 cd test &&
113 (cat >expect1 <<EOF
114Note: A non-remote branch was not removed; to delete it, use:
115 git branch -d master
116EOF
117 cat >expect2 <<EOF
118Note: Non-remote branches were not removed; to delete them, use:
119 git branch -d foobranch
120 git branch -d master
121EOF
122) &&
123 git tag footag
124 git config --add remote.oops.fetch "+refs/*:refs/*" &&
125 git remote rm oops 2>actual1 &&
126 git branch foobranch &&
127 git config --add remote.oops.fetch "+refs/*:refs/*" &&
128 git remote rm oops 2>actual2 &&
129 git branch -d foobranch &&
130 git tag -d footag &&
131 test_cmp expect1 actual1 &&
132 test_cmp expect2 actual2
133)
134'
135
136cat > test/expect << EOF
137* remote origin
138 URL: $(pwd)/one
139 HEAD branch: master
140 Remote branches:
141 master new (next fetch will store in remotes/origin)
142 side tracked
143 Local branches configured for 'git pull':
144 master merges with remote master
145 octopus merges with remote topic-a
146 and with remote topic-b
147 and with remote topic-c
148 rebase rebases onto remote master
149 Local branches pushed with 'git push'
150 master:upstream
151 +refs/tags/lastbackup
152* remote two
153 URL: ../two
154 HEAD branch (remote HEAD is ambiguous, may be one of the following):
155 another
156 master
157EOF
158
159test_expect_success 'show' '
160 (cd test &&
161 git config --add remote.origin.fetch refs/heads/master:refs/heads/upstream &&
162 git fetch &&
163 git branch --track octopus origin/master &&
164 git branch --track rebase origin/master &&
165 git branch -d -r origin/master &&
166 git config --add remote.two.url ../two &&
167 git config branch.rebase.rebase true &&
168 git config branch.octopus.merge "topic-a topic-b topic-c" &&
169 (cd ../one &&
170 echo 1 > file &&
171 test_tick &&
172 git commit -m update file) &&
173 git config remote.origin.push refs/heads/master:refs/heads/upstream &&
174 git config --add remote.origin.push +refs/tags/lastbackup &&
175 git remote show origin two > output &&
176 git branch -d rebase octopus &&
177 test_cmp expect output)
178'
179
180cat > test/expect << EOF
181* remote origin
182 URL: $(pwd)/one
183 HEAD branch: (not queried)
184 Remote branches: (status not queried)
185 master
186 side
187 Local branch configured for 'git pull':
188 master merges with remote master
189 Local branches pushed with 'git push'
190 master:upstream
191 +refs/tags/lastbackup
192EOF
193
194test_expect_success 'show -n' '
195 (mv one one.unreachable &&
196 cd test &&
197 git remote show -n origin > output &&
198 mv ../one.unreachable ../one &&
199 test_cmp expect output)
200'
201
202test_expect_success 'prune' '
203 (cd one &&
204 git branch -m side side2) &&
205 (cd test &&
206 git fetch origin &&
207 git remote prune origin &&
208 git rev-parse refs/remotes/origin/side2 &&
209 test_must_fail git rev-parse refs/remotes/origin/side)
210'
211
212test_expect_success 'set-head --delete' '
213 (cd test &&
214 git symbolic-ref refs/remotes/origin/HEAD &&
215 git remote set-head --delete origin &&
216 test_must_fail git symbolic-ref refs/remotes/origin/HEAD)
217'
218
219test_expect_success 'set-head --auto' '
220 (cd test &&
221 git remote set-head --auto origin &&
222 echo refs/remotes/origin/master >expect &&
223 git symbolic-ref refs/remotes/origin/HEAD >output &&
224 test_cmp expect output
225 )
226'
227
228cat >test/expect <<EOF
229error: Multiple remote HEAD branches. Please choose one explicitly with:
230 git remote set-head two another
231 git remote set-head two master
232EOF
233
234test_expect_success 'set-head --auto fails w/multiple HEADs' '
235 (cd test &&
236 test_must_fail git remote set-head --auto two >output 2>&1 &&
237 test_cmp expect output)
238'
239
240cat >test/expect <<EOF
241refs/remotes/origin/side2
242EOF
243
244test_expect_success 'set-head explicit' '
245 (cd test &&
246 git remote set-head origin side2 &&
247 git symbolic-ref refs/remotes/origin/HEAD >output &&
248 git remote set-head origin master &&
249 test_cmp expect output)
250'
251
252cat > test/expect << EOF
253Pruning origin
254URL: $(pwd)/one
255 * [would prune] origin/side2
256EOF
257
258test_expect_success 'prune --dry-run' '
259 (cd one &&
260 git branch -m side2 side) &&
261 (cd test &&
262 git remote prune --dry-run origin > output &&
263 git rev-parse refs/remotes/origin/side2 &&
264 test_must_fail git rev-parse refs/remotes/origin/side &&
265 (cd ../one &&
266 git branch -m side side2) &&
267 test_cmp expect output)
268'
269
270test_expect_success 'add --mirror && prune' '
271 (mkdir mirror &&
272 cd mirror &&
273 git init --bare &&
274 git remote add --mirror -f origin ../one) &&
275 (cd one &&
276 git branch -m side2 side) &&
277 (cd mirror &&
278 git rev-parse --verify refs/heads/side2 &&
279 test_must_fail git rev-parse --verify refs/heads/side &&
280 git fetch origin &&
281 git remote prune origin &&
282 test_must_fail git rev-parse --verify refs/heads/side2 &&
283 git rev-parse --verify refs/heads/side)
284'
285
286test_expect_success 'add alt && prune' '
287 (mkdir alttst &&
288 cd alttst &&
289 git init &&
290 git remote add -f origin ../one &&
291 git config remote.alt.url ../one &&
292 git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*") &&
293 (cd one &&
294 git branch -m side side2) &&
295 (cd alttst &&
296 git rev-parse --verify refs/remotes/origin/side &&
297 test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
298 git fetch alt &&
299 git remote prune alt &&
300 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
301 git rev-parse --verify refs/remotes/origin/side2)
302'
303
304cat > one/expect << EOF
305 apis/master
306 apis/side
307 drosophila/another
308 drosophila/master
309 drosophila/side
310EOF
311
312test_expect_success 'update' '
313
314 (cd one &&
315 git remote add drosophila ../two &&
316 git remote add apis ../mirror &&
317 git remote update &&
318 git branch -r > output &&
319 test_cmp expect output)
320
321'
322
323cat > one/expect << EOF
324 drosophila/another
325 drosophila/master
326 drosophila/side
327 manduca/master
328 manduca/side
329 megaloprepus/master
330 megaloprepus/side
331EOF
332
333test_expect_success 'update with arguments' '
334
335 (cd one &&
336 for b in $(git branch -r)
337 do
338 git branch -r -d $b || break
339 done &&
340 git remote add manduca ../mirror &&
341 git remote add megaloprepus ../mirror &&
342 git config remotes.phobaeticus "drosophila megaloprepus" &&
343 git config remotes.titanus manduca &&
344 git remote update phobaeticus titanus &&
345 git branch -r > output &&
346 test_cmp expect output)
347
348'
349
350cat > one/expect << EOF
351 apis/master
352 apis/side
353 manduca/master
354 manduca/side
355 megaloprepus/master
356 megaloprepus/side
357EOF
358
359test_expect_success 'update default' '
360
361 (cd one &&
362 for b in $(git branch -r)
363 do
364 git branch -r -d $b || break
365 done &&
366 git config remote.drosophila.skipDefaultUpdate true &&
367 git remote update default &&
368 git branch -r > output &&
369 test_cmp expect output)
370
371'
372
373cat > one/expect << EOF
374 drosophila/another
375 drosophila/master
376 drosophila/side
377EOF
378
379test_expect_success 'update default (overridden, with funny whitespace)' '
380
381 (cd one &&
382 for b in $(git branch -r)
383 do
384 git branch -r -d $b || break
385 done &&
386 git config remotes.default "$(printf "\t drosophila \n")" &&
387 git remote update default &&
388 git branch -r > output &&
389 test_cmp expect output)
390
391'
392
393test_expect_success '"remote show" does not show symbolic refs' '
394
395 git clone one three &&
396 (cd three &&
397 git remote show origin > output &&
398 ! grep "^ *HEAD$" < output &&
399 ! grep -i stale < output)
400
401'
402
403test_expect_success 'reject adding remote with an invalid name' '
404
405 test_must_fail git remote add some:url desired-name
406
407'
408
409# The first three test if the tracking branches are properly renamed,
410# the last two ones check if the config is updated.
411
412test_expect_success 'rename a remote' '
413
414 git clone one four &&
415 (cd four &&
416 git remote rename origin upstream &&
417 rmdir .git/refs/remotes/origin &&
418 test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/master" &&
419 test "$(git rev-parse upstream/master)" = "$(git rev-parse master)" &&
420 test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
421 test "$(git config branch.master.remote)" = "upstream")
422
423'
424
425cat > remotes_origin << EOF
426URL: $(pwd)/one
427Push: refs/heads/master:refs/heads/upstream
428Pull: refs/heads/master:refs/heads/origin
429EOF
430
431test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
432 git clone one five &&
433 origin_url=$(pwd)/one &&
434 (cd five &&
435 git remote rm origin &&
436 mkdir -p .git/remotes &&
437 cat ../remotes_origin > .git/remotes/origin &&
438 git remote rename origin origin &&
439 ! test -f .git/remotes/origin &&
440 test "$(git config remote.origin.url)" = "$origin_url" &&
441 test "$(git config remote.origin.push)" = "refs/heads/master:refs/heads/upstream" &&
442 test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
443'
444
445test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
446 git clone one six &&
447 origin_url=$(pwd)/one &&
448 (cd six &&
449 git remote rm origin &&
450 echo "$origin_url" > .git/branches/origin &&
451 git remote rename origin origin &&
452 ! test -f .git/branches/origin &&
453 test "$(git config remote.origin.url)" = "$origin_url" &&
454 test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
455'
456
457test_expect_success 'remote prune to cause a dangling symref' '
458 git clone one seven &&
459 (
460 cd one &&
461 git checkout side2 &&
462 git branch -D master
463 ) &&
464 (
465 cd seven &&
466 git remote prune origin
467 ) 2>err &&
468 grep "has become dangling" err &&
469
470 : And the dangling symref will not cause other annoying errors
471 (
472 cd seven &&
473 git branch -a
474 ) 2>err &&
475 ! grep "points nowhere" err
476 (
477 cd seven &&
478 test_must_fail git branch nomore origin
479 ) 2>err &&
480 grep "dangling symref" err
481'
482
483test_done
484