2 # Copyright (c) 2020, Jacob Keller.
4 test_description
='"git fetch" with negative refspecs.
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 TEST_PASSES_SANITIZE_LEAK
=true
14 test_expect_success setup
'
15 echo >file original &&
17 git commit -a -m original
20 test_expect_success
"clone and setup child repos" '
24 echo >file updated by one &&
25 git commit -a -m "updated by one" &&
26 git switch -c alternate &&
27 echo >file updated again by one &&
28 git commit -a -m "updated by one again" &&
34 git config branch.main.remote one &&
35 git config remote.one.url ../one/.git/ &&
36 git config remote.one.fetch +refs/heads/*:refs/remotes/one/* &&
37 git config --add remote.one.fetch ^refs/heads/alternate
42 test_expect_success
"fetch one" '
43 echo >file updated by origin &&
44 git commit -a -m "updated by origin" &&
47 test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
49 test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
50 git rev-parse --verify refs/remotes/one/main &&
51 mine=$(git rev-parse refs/remotes/one/main) &&
52 his=$(cd ../one && git rev-parse refs/heads/main) &&
53 test "z$mine" = "z$his"
57 test_expect_success
"fetch with negative refspec on commandline" '
58 echo >file updated by origin again &&
59 git commit -a -m "updated by origin again" &&
62 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
63 echo $alternate_in_one >expect &&
64 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/main &&
65 cut -f -1 .git/FETCH_HEAD >actual &&
66 test_cmp expect actual
70 test_expect_success
"fetch with negative sha1 refspec fails" '
71 echo >file updated by origin yet again &&
72 git commit -a -m "updated by origin yet again" &&
75 main_in_one=$(cd ../one && git rev-parse refs/heads/main) &&
76 test_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$main_in_one
80 test_expect_success
"fetch with negative pattern refspec" '
81 echo >file updated by origin once more &&
82 git commit -a -m "updated by origin once more" &&
85 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
86 echo $alternate_in_one >expect &&
87 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&
88 cut -f -1 .git/FETCH_HEAD >actual &&
89 test_cmp expect actual
93 test_expect_success
"fetch with negative pattern refspec does not expand prefix" '
94 echo >file updated by origin another time &&
95 git commit -a -m "updated by origin another time" &&
98 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
99 main_in_one=$(cd ../one && git rev-parse refs/heads/main) &&
100 echo $alternate_in_one >expect &&
101 echo $main_in_one >>expect &&
102 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^main &&
103 cut -f -1 .git/FETCH_HEAD >actual &&
104 test_cmp expect actual
108 test_expect_success
"fetch with negative refspec avoids duplicate conflict" '
114 git branch other/a &&
115 git rev-parse --verify refs/heads/other/a >../expect &&
116 git rev-parse --verify refs/heads/dups/b >>../expect &&
117 git rev-parse --verify refs/heads/dups/c >>../expect
121 git fetch ../one/.git ^refs/heads/dups/a refs/heads/dups/*:refs/dups/* refs/heads/other/a:refs/dups/a &&
122 git rev-parse --verify refs/dups/a >../actual &&
123 git rev-parse --verify refs/dups/b >>../actual &&
124 git rev-parse --verify refs/dups/c >>../actual
126 test_cmp expect actual
129 test_expect_success
"push --prune with negative refspec" '
132 git branch prune/a &&
133 git branch prune/b &&
134 git branch prune/c &&
135 git push ../three refs/heads/prune/* &&
136 git branch -d prune/a &&
137 git branch -d prune/b &&
138 git push --prune ../three refs/heads/prune/* ^refs/heads/prune/b
142 test_write_lines b c >expect &&
143 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/prune/ >actual &&
144 test_cmp expect actual
148 test_expect_success
"push --prune with negative refspec apply to the destination" '
154 git push ../three refs/heads/ours/*:refs/heads/theirs/* &&
155 git branch -d ours/a &&
156 git branch -d ours/b &&
157 git push --prune ../three refs/heads/ours/*:refs/heads/theirs/* ^refs/heads/theirs/b
161 test_write_lines b c >expect &&
162 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/theirs/ >actual &&
163 test_cmp expect actual
167 test_expect_success
"fetch --prune with negative refspec" '
170 git branch fetch/a &&
171 git branch fetch/b &&
176 git fetch ../two/.git refs/heads/fetch/*:refs/heads/copied/*
180 git branch -d fetch/a &&
181 git branch -d fetch/b
185 test_write_lines b c >expect &&
186 git fetch -v ../two/.git --prune refs/heads/fetch/*:refs/heads/copied/* ^refs/heads/fetch/b &&
187 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/copied/ >actual &&
188 test_cmp expect actual
192 test_expect_success
"push with matching : and negative refspec" '
193 # Manually handle cleanup, since test_config is not
194 # prepared to take arbitrary options like --add
195 test_when_finished "test_unconfig -C two remote.one.push" &&
197 # For convenience, we use "master" to refer to the name of
198 # the branch created by default in the following.
200 # Repositories two and one have branches other than "master"
201 # but they have no overlap---"master" is the only one that
202 # is shared between them. And the master branch at two is
203 # behind the master branch at one by one commit.
204 git -C two config --add remote.one.push : &&
206 # A matching push tries to update master, fails due to non-ff
207 test_must_fail git -C two push one &&
209 # "master" may actually not be "master"---find it out.
210 current=$(git symbolic-ref HEAD) &&
212 # If master is in negative refspec, then the command will not attempt
213 # to push and succeed.
214 git -C two config --add remote.one.push "^$current" &&
216 # With "master" excluded, this push is a no-op. Nothing gets
217 # pushed and it succeeds.
218 git -C two push -v one
221 test_expect_success
"push with matching +: and negative refspec" '
222 test_when_finished "test_unconfig -C two remote.one.push" &&
224 # The same set-up as above, whose side-effect was a no-op.
225 git -C two config --add remote.one.push +: &&
227 # The push refuses to update the "master" branch that is checked
228 # out in the "one" repository, even when it is forced with +:
229 test_must_fail git -C two push one &&
231 # "master" may actually not be "master"---find it out.
232 current=$(git symbolic-ref HEAD) &&
234 # If master is in negative refspec, then the command will not attempt
235 # to push and succeed
236 git -C two config --add remote.one.push "^$current" &&
238 # With "master" excluded, this push is a no-op. Nothing gets
239 # pushed and it succeeds.
240 git -C two push -v one
243 test_expect_success
'--prefetch correctly modifies refspecs' '
244 git -C one config --unset-all remote.origin.fetch &&
245 git -C one config --add remote.origin.fetch ^refs/heads/bogus/ignore &&
246 git -C one config --add remote.origin.fetch "refs/tags/*:refs/tags/*" &&
247 git -C one config --add remote.origin.fetch "refs/heads/bogus/*:bogus/*" &&
249 git tag -a -m never never-fetch-tag HEAD &&
251 git branch bogus/fetched HEAD~1 &&
252 git branch bogus/ignore HEAD &&
254 git -C one fetch --prefetch --no-tags &&
255 test_must_fail git -C one rev-parse never-fetch-tag &&
256 git -C one rev-parse refs/prefetch/bogus/fetched &&
257 test_must_fail git -C one rev-parse refs/prefetch/bogus/ignore &&
259 # correctly handle when refspec set becomes empty
260 # after removing the refs/tags/* refspec.
261 git -C one config --unset-all remote.origin.fetch &&
262 git -C one config --add remote.origin.fetch "refs/tags/*:refs/tags/*" &&
264 git -C one fetch --prefetch --no-tags &&
265 test_must_fail git -C one rev-parse never-fetch-tag &&
267 # The refspec for refs that are not fully qualified
268 # are filtered multiple times.
269 git -C one rev-parse refs/prefetch/bogus/fetched &&
270 test_must_fail git -C one rev-parse refs/prefetch/bogus/ignore
273 test_expect_success
'--prefetch succeeds when refspec becomes empty' '
274 git checkout bogus/fetched &&
277 git -C one config --unset-all remote.origin.fetch &&
278 git -C one config --unset branch.main.remote &&
279 git -C one config remote.origin.fetch "+refs/tags/extra" &&
280 git -C one config remote.origin.skipfetchall true &&
281 git -C one config remote.origin.tagopt "--no-tags" &&
283 git -C one fetch --prefetch
286 test_expect_success
'--prefetch succeeds with empty command line refspec' '
287 git -C one fetch --prefetch origin +refs/tags/extra