3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='See why rewinding head breaks send-pack
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 test_expect_success setup
'
17 mkdir mozart mozart/is &&
18 echo "Commit #0" >mozart/is/pink &&
19 git update-index --add mozart/is/pink &&
20 tree=$(git write-tree) &&
21 commit=$(echo "Commit #0" | git commit-tree $tree) &&
25 while test $i -le $cnt
29 echo "Commit #$i" >mozart/is/pink &&
30 git update-index --add mozart/is/pink &&
31 tree=$(git write-tree) &&
32 commit=$(echo "Commit #$i" |
33 git commit-tree $tree -p $parent) &&
34 git update-ref refs/tags/commit$i $commit &&
35 parent=$commit || return 1
37 git update-ref HEAD "$commit" &&
38 git clone ./. victim &&
39 ( cd victim && git config receive.denyCurrentBranch warn && git log ) &&
40 git update-ref HEAD "$zero" &&
43 while test $i -le $cnt
47 echo "Rebase #$i" >mozart/is/pink &&
48 git update-index --add mozart/is/pink &&
49 tree=$(git write-tree) &&
50 commit=$(echo "Rebase #$i" | git commit-tree $tree -p $parent) &&
51 git update-ref refs/tags/rebase$i $commit &&
52 parent=$commit || return 1
54 git update-ref HEAD "$commit" &&
58 test_expect_success
'pack the source repository' '
63 test_expect_success
'pack the destination repository' '
71 test_expect_success
'refuse pushing rewound head without --force' '
72 pushed_head=$(git rev-parse --verify main) &&
73 victim_orig=$(cd victim && git rev-parse --verify main) &&
74 test_must_fail git send-pack ./victim main &&
75 victim_head=$(cd victim && git rev-parse --verify main) &&
76 test "$victim_head" = "$victim_orig" &&
78 git send-pack --force ./victim main &&
79 victim_head=$(cd victim && git rev-parse --verify main) &&
80 test "$victim_head" = "$pushed_head"
83 test_expect_success
'push can be used to delete a ref' '
84 ( cd victim && git branch extra main ) &&
85 git send-pack ./victim :extra main &&
87 test_must_fail git rev-parse --verify extra )
90 test_expect_success
'refuse deleting push with denyDeletes' '
93 test_might_fail git branch -D extra &&
94 git config receive.denyDeletes true &&
97 test_must_fail git send-pack ./victim :extra main
100 test_expect_success
'cannot override denyDeletes with git -c send-pack' '
103 test_might_fail git branch -D extra &&
104 git config receive.denyDeletes true &&
105 git branch extra main
107 test_must_fail git -c receive.denyDeletes=false \
108 send-pack ./victim :extra main
111 test_expect_success
'override denyDeletes with git -c receive-pack' '
114 test_might_fail git branch -D extra &&
115 git config receive.denyDeletes true &&
116 git branch extra main
119 --receive-pack="git -c receive.denyDeletes=false receive-pack" \
123 test_expect_success
'denyNonFastforwards trumps --force' '
126 test_might_fail git branch -D extra &&
127 git config receive.denyNonFastforwards true
129 victim_orig=$(cd victim && git rev-parse --verify main) &&
130 test_must_fail git send-pack --force ./victim main^:main &&
131 victim_head=$(cd victim && git rev-parse --verify main) &&
132 test "$victim_orig" = "$victim_head"
135 test_expect_success
'send-pack --all sends all branches' '
136 # make sure we have at least 2 branches with different
137 # values, just to be thorough
138 git branch other-branch HEAD^ &&
140 git init --bare all.git &&
141 git send-pack --all all.git &&
142 git for-each-ref refs/heads >expect &&
143 git -C all.git for-each-ref refs/heads >actual &&
144 test_cmp expect actual
147 test_expect_success
'push --all excludes remote-tracking hierarchy' '
151 git init && : >file && git add file && git commit -m add
153 git clone parent child &&
155 cd child && git push --all
159 test -z "$(git for-each-ref refs/remotes/origin)"
163 test_expect_success
'receive-pack runs auto-gc in remote repo' '
164 rm -rf parent child &&
167 # Setup a repo with 2 packs
169 echo "Some text" >file.txt &&
171 git commit -m "Initial commit" &&
173 echo "Some more text" >>file.txt &&
174 git commit -a -m "Second commit" &&
177 cp -R parent child &&
179 # Set the child to auto-pack if more than one pack exists
181 git config gc.autopacklimit 1 &&
182 git config gc.autodetach false &&
183 git branch test_auto_gc &&
184 # And create a file that follows the temporary object naming
185 # convention for the auto-gc to remove
186 : >.git/objects/tmp_test_object &&
187 test-tool chmtime =-1209601 .git/objects/tmp_test_object
191 echo "Even more text" >>file.txt &&
192 git commit -a -m "Third commit" &&
193 git send-pack ../child HEAD:refs/heads/test_auto_gc
195 test ! -e child/.git/objects/tmp_test_object
198 rewound_push_setup
() {
199 rm -rf parent child
&&
204 echo one
>file && git add
file && git commit
-m one
&&
205 git config receive.denyCurrentBranch warn
&&
206 echo two
>file && git commit
-a -m two
208 git clone parent child
&&
210 cd child
&& git
reset --hard HEAD^
214 test_expect_success
'pushing explicit refspecs respects forcing' '
215 rewound_push_setup &&
216 parent_orig=$(cd parent && git rev-parse --verify main) &&
219 test_must_fail git send-pack ../parent \
220 refs/heads/main:refs/heads/main
222 parent_head=$(cd parent && git rev-parse --verify main) &&
223 test "$parent_orig" = "$parent_head" &&
226 git send-pack ../parent \
227 +refs/heads/main:refs/heads/main
229 parent_head=$(cd parent && git rev-parse --verify main) &&
230 child_head=$(cd child && git rev-parse --verify main) &&
231 test "$parent_head" = "$child_head"
234 test_expect_success
'pushing wildcard refspecs respects forcing' '
235 rewound_push_setup &&
236 parent_orig=$(cd parent && git rev-parse --verify main) &&
239 test_must_fail git send-pack ../parent \
240 "refs/heads/*:refs/heads/*"
242 parent_head=$(cd parent && git rev-parse --verify main) &&
243 test "$parent_orig" = "$parent_head" &&
246 git send-pack ../parent \
247 "+refs/heads/*:refs/heads/*"
249 parent_head=$(cd parent && git rev-parse --verify main) &&
250 child_head=$(cd child && git rev-parse --verify main) &&
251 test "$parent_head" = "$child_head"
254 test_expect_success
'deny pushing to delete current branch' '
255 rewound_push_setup &&
258 test_must_fail git send-pack ../parent :refs/heads/main 2>errs
262 extract_ref_advertisement
() {
264 # \\ is there to skip capabilities after \0
265 /push< ([^\\]+)/ or next;
266 exit 0 if $1 eq "0000";
271 test_expect_success
'receive-pack de-dupes .have lines' '
273 git -C shared commit --allow-empty -m both &&
274 git clone -s shared fork &&
277 git checkout -b only-shared &&
278 git commit --allow-empty -m only-shared &&
279 git update-ref refs/heads/foo HEAD
282 # Notable things in this expectation:
283 # - local refs are not de-duped
284 # - .have does not duplicate locals
285 # - .have does not duplicate itself
286 local=$(git -C fork rev-parse HEAD) &&
287 shared=$(git -C shared rev-parse only-shared) &&
288 cat >expect <<-EOF &&
289 $local refs/heads/main
290 $local refs/remotes/origin/HEAD
291 $local refs/remotes/origin/main
295 GIT_TRACE_PACKET=$(pwd)/trace GIT_TEST_PROTOCOL_VERSION=0 \
297 --receive-pack="unset GIT_TRACE_PACKET; git-receive-pack" \
299 extract_ref_advertisement <trace >refs &&