test-mergesort: add generate subcommand
[git/debian.git] / t / t5582-fetch-negative-refspec.sh
blob2f3b064d0e7267fe41f41cfa8ac7dd0dd130685f
1 #!/bin/sh
2 # Copyright (c) 2020, Jacob Keller.
4 test_description='"git fetch" with negative refspecs.
8 . ./test-lib.sh
10 test_expect_success setup '
11 echo >file original &&
12 git add file &&
13 git commit -a -m original
16 test_expect_success "clone and setup child repos" '
17 git clone . one &&
19 cd one &&
20 echo >file updated by one &&
21 git commit -a -m "updated by one" &&
22 git switch -c alternate &&
23 echo >file updated again by one &&
24 git commit -a -m "updated by one again" &&
25 git switch master
26 ) &&
27 git clone . two &&
29 cd two &&
30 git config branch.master.remote one &&
31 git config remote.one.url ../one/.git/ &&
32 git config remote.one.fetch +refs/heads/*:refs/remotes/one/* &&
33 git config --add remote.one.fetch ^refs/heads/alternate
34 ) &&
35 git clone . three
38 test_expect_success "fetch one" '
39 echo >file updated by origin &&
40 git commit -a -m "updated by origin" &&
42 cd two &&
43 test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
44 git fetch one &&
45 test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
46 git rev-parse --verify refs/remotes/one/master &&
47 mine=$(git rev-parse refs/remotes/one/master) &&
48 his=$(cd ../one && git rev-parse refs/heads/master) &&
49 test "z$mine" = "z$his"
53 test_expect_success "fetch with negative refspec on commandline" '
54 echo >file updated by origin again &&
55 git commit -a -m "updated by origin again" &&
57 cd three &&
58 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
59 echo $alternate_in_one >expect &&
60 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/master &&
61 cut -f -1 .git/FETCH_HEAD >actual &&
62 test_cmp expect actual
66 test_expect_success "fetch with negative sha1 refspec fails" '
67 echo >file updated by origin yet again &&
68 git commit -a -m "updated by origin yet again" &&
70 cd three &&
71 master_in_one=$(cd ../one && git rev-parse refs/heads/master) &&
72 test_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$master_in_one
76 test_expect_success "fetch with negative pattern refspec" '
77 echo >file updated by origin once more &&
78 git commit -a -m "updated by origin once more" &&
80 cd three &&
81 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
82 echo $alternate_in_one >expect &&
83 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&
84 cut -f -1 .git/FETCH_HEAD >actual &&
85 test_cmp expect actual
89 test_expect_success "fetch with negative pattern refspec does not expand prefix" '
90 echo >file updated by origin another time &&
91 git commit -a -m "updated by origin another time" &&
93 cd three &&
94 alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
95 master_in_one=$(cd ../one && git rev-parse refs/heads/master) &&
96 echo $alternate_in_one >expect &&
97 echo $master_in_one >>expect &&
98 git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^master &&
99 cut -f -1 .git/FETCH_HEAD >actual &&
100 test_cmp expect actual
104 test_expect_success "fetch with negative refspec avoids duplicate conflict" '
105 cd "$D" &&
107 cd one &&
108 git branch dups/a &&
109 git branch dups/b &&
110 git branch dups/c &&
111 git branch other/a &&
112 git rev-parse --verify refs/heads/other/a >../expect &&
113 git rev-parse --verify refs/heads/dups/b >>../expect &&
114 git rev-parse --verify refs/heads/dups/c >>../expect
115 ) &&
117 cd three &&
118 git fetch ../one/.git ^refs/heads/dups/a refs/heads/dups/*:refs/dups/* refs/heads/other/a:refs/dups/a &&
119 git rev-parse --verify refs/dups/a >../actual &&
120 git rev-parse --verify refs/dups/b >>../actual &&
121 git rev-parse --verify refs/dups/c >>../actual
122 ) &&
123 test_cmp expect actual
126 test_expect_success "push --prune with negative refspec" '
128 cd two &&
129 git branch prune/a &&
130 git branch prune/b &&
131 git branch prune/c &&
132 git push ../three refs/heads/prune/* &&
133 git branch -d prune/a &&
134 git branch -d prune/b &&
135 git push --prune ../three refs/heads/prune/* ^refs/heads/prune/b
136 ) &&
138 cd three &&
139 test_write_lines b c >expect &&
140 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/prune/ >actual &&
141 test_cmp expect actual
145 test_expect_success "push --prune with negative refspec apply to the destination" '
147 cd two &&
148 git branch ours/a &&
149 git branch ours/b &&
150 git branch ours/c &&
151 git push ../three refs/heads/ours/*:refs/heads/theirs/* &&
152 git branch -d ours/a &&
153 git branch -d ours/b &&
154 git push --prune ../three refs/heads/ours/*:refs/heads/theirs/* ^refs/heads/theirs/b
155 ) &&
157 cd three &&
158 test_write_lines b c >expect &&
159 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/theirs/ >actual &&
160 test_cmp expect actual
164 test_expect_success "fetch --prune with negative refspec" '
166 cd two &&
167 git branch fetch/a &&
168 git branch fetch/b &&
169 git branch fetch/c
170 ) &&
172 cd three &&
173 git fetch ../two/.git refs/heads/fetch/*:refs/heads/copied/*
174 ) &&
176 cd two &&
177 git branch -d fetch/a &&
178 git branch -d fetch/b
179 ) &&
181 cd three &&
182 test_write_lines b c >expect &&
183 git fetch -v ../two/.git --prune refs/heads/fetch/*:refs/heads/copied/* ^refs/heads/fetch/b &&
184 git for-each-ref --format="%(refname:lstrip=3)" refs/heads/copied/ >actual &&
185 test_cmp expect actual
189 test_expect_success "push with matching : and negative refspec" '
190 # Manually handle cleanup, since test_config is not
191 # prepared to take arbitrary options like --add
192 test_when_finished "test_unconfig -C two remote.one.push" &&
194 # For convenience, we use "master" to refer to the name of
195 # the branch created by default in the following.
197 # Repositories two and one have branches other than "master"
198 # but they have no overlap---"master" is the only one that
199 # is shared between them. And the master branch at two is
200 # behind the master branch at one by one commit.
201 git -C two config --add remote.one.push : &&
203 # A matching push tries to update master, fails due to non-ff
204 test_must_fail git -C two push one &&
206 # "master" may actually not be "master"---find it out.
207 current=$(git symbolic-ref HEAD) &&
209 # If master is in negative refspec, then the command will not attempt
210 # to push and succeed.
211 git -C two config --add remote.one.push "^$current" &&
213 # With "master" excluded, this push is a no-op. Nothing gets
214 # pushed and it succeeds.
215 git -C two push -v one
218 test_expect_success "push with matching +: and negative refspec" '
219 test_when_finished "test_unconfig -C two remote.one.push" &&
221 # The same set-up as above, whose side-effect was a no-op.
222 git -C two config --add remote.one.push +: &&
224 # The push refuses to update the "master" branch that is checked
225 # out in the "one" repository, even when it is forced with +:
226 test_must_fail git -C two push one &&
228 # "master" may actually not be "master"---find it out.
229 current=$(git symbolic-ref HEAD) &&
231 # If master is in negative refspec, then the command will not attempt
232 # to push and succeed
233 git -C two config --add remote.one.push "^$current" &&
235 # With "master" excluded, this push is a no-op. Nothing gets
236 # pushed and it succeeds.
237 git -C two push -v one
240 test_done