1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6test_description='See why rewinding head breaks send-pack
7
8'
9. ./test-lib.sh
10
11cnt=64
12test_expect_success setup '
13 test_tick &&
14 mkdir mozart mozart/is &&
15 echo "Commit #0" >mozart/is/pink &&
16 git update-index --add mozart/is/pink &&
17 tree=$(git write-tree) &&
18 commit=$(echo "Commit #0" | git commit-tree $tree) &&
19 zero=$commit &&
20 parent=$zero &&
21 i=0 &&
22 while test $i -le $cnt
23 do
24 i=$(($i+1)) &&
25 test_tick &&
26 echo "Commit #$i" >mozart/is/pink &&
27 git update-index --add mozart/is/pink &&
28 tree=$(git write-tree) &&
29 commit=$(echo "Commit #$i" | git commit-tree $tree -p $parent) &&
30 git update-ref refs/tags/commit$i $commit &&
31 parent=$commit || return 1
32 done &&
33 git update-ref HEAD "$commit" &&
34 git clone ./. victim &&
35 ( cd victim && git config receive.denyCurrentBranch warn && git log ) &&
36 git update-ref HEAD "$zero" &&
37 parent=$zero &&
38 i=0 &&
39 while test $i -le $cnt
40 do
41 i=$(($i+1)) &&
42 test_tick &&
43 echo "Rebase #$i" >mozart/is/pink &&
44 git update-index --add mozart/is/pink &&
45 tree=$(git write-tree) &&
46 commit=$(echo "Rebase #$i" | git commit-tree $tree -p $parent) &&
47 git update-ref refs/tags/rebase$i $commit &&
48 parent=$commit || return 1
49 done &&
50 git update-ref HEAD "$commit" &&
51 echo Rebase &&
52 git log'
53
54test_expect_success 'pack the source repository' '
55 git repack -a -d &&
56 git prune
57'
58
59test_expect_success 'pack the destination repository' '
60 (
61 cd victim &&
62 git repack -a -d &&
63 git prune
64 )
65'
66
67test_expect_success 'refuse pushing rewound head without --force' '
68 pushed_head=$(git rev-parse --verify master) &&
69 victim_orig=$(cd victim && git rev-parse --verify master) &&
70 test_must_fail git send-pack ./victim master &&
71 victim_head=$(cd victim && git rev-parse --verify master) &&
72 test "$victim_head" = "$victim_orig" &&
73 # this should update
74 git send-pack --force ./victim master &&
75 victim_head=$(cd victim && git rev-parse --verify master) &&
76 test "$victim_head" = "$pushed_head"
77'
78
79test_expect_success \
80 'push can be used to delete a ref' '
81 ( cd victim && git branch extra master ) &&
82 git send-pack ./victim :extra master &&
83 ( cd victim &&
84 test_must_fail git rev-parse --verify extra )
85'
86
87test_expect_success 'refuse deleting push with denyDeletes' '
88 (
89 cd victim &&
90 ( git branch -D extra || : ) &&
91 git config receive.denyDeletes true &&
92 git branch extra master
93 ) &&
94 test_must_fail git send-pack ./victim :extra master
95'
96
97test_expect_success 'cannot override denyDeletes with git -c send-pack' '
98 (
99 cd victim &&
100 test_might_fail git branch -D extra &&
101 git config receive.denyDeletes true &&
102 git branch extra master
103 ) &&
104 test_must_fail git -c receive.denyDeletes=false \
105 send-pack ./victim :extra master
106'
107
108test_expect_success 'override denyDeletes with git -c receive-pack' '
109 (
110 cd victim &&
111 test_might_fail git branch -D extra &&
112 git config receive.denyDeletes true &&
113 git branch extra master
114 ) &&
115 git send-pack \
116 --receive-pack="git -c receive.denyDeletes=false receive-pack" \
117 ./victim :extra master
118'
119
120test_expect_success 'denyNonFastforwards trumps --force' '
121 (
122 cd victim &&
123 ( git branch -D extra || : ) &&
124 git config receive.denyNonFastforwards true
125 ) &&
126 victim_orig=$(cd victim && git rev-parse --verify master) &&
127 test_must_fail git send-pack --force ./victim master^:master &&
128 victim_head=$(cd victim && git rev-parse --verify master) &&
129 test "$victim_orig" = "$victim_head"
130'
131
132test_expect_success 'push --all excludes remote-tracking hierarchy' '
133 mkdir parent &&
134 (
135 cd parent &&
136 git init && : >file && git add file && git commit -m add
137 ) &&
138 git clone parent child &&
139 (
140 cd child && git push --all
141 ) &&
142 (
143 cd parent &&
144 test -z "$(git for-each-ref refs/remotes/origin)"
145 )
146'
147
148test_expect_success 'receive-pack runs auto-gc in remote repo' '
149 rm -rf parent child &&
150 git init parent &&
151 (
152 # Setup a repo with 2 packs
153 cd parent &&
154 echo "Some text" >file.txt &&
155 git add . &&
156 git commit -m "Initial commit" &&
157 git repack -adl &&
158 echo "Some more text" >>file.txt &&
159 git commit -a -m "Second commit" &&
160 git repack
161 ) &&
162 cp -a parent child &&
163 (
164 # Set the child to auto-pack if more than one pack exists
165 cd child &&
166 git config gc.autopacklimit 1 &&
167 git branch test_auto_gc &&
168 # And create a file that follows the temporary object naming
169 # convention for the auto-gc to remove
170 : >.git/objects/tmp_test_object &&
171 test-chmtime =-1209601 .git/objects/tmp_test_object
172 ) &&
173 (
174 cd parent &&
175 echo "Even more text" >>file.txt &&
176 git commit -a -m "Third commit" &&
177 git send-pack ../child HEAD:refs/heads/test_auto_gc
178 ) &&
179 test ! -e child/.git/objects/tmp_test_object
180'
181
182rewound_push_setup() {
183 rm -rf parent child &&
184 mkdir parent &&
185 (
186 cd parent &&
187 git init &&
188 echo one >file && git add file && git commit -m one &&
189 git config receive.denyCurrentBranch warn &&
190 echo two >file && git commit -a -m two
191 ) &&
192 git clone parent child &&
193 (
194 cd child && git reset --hard HEAD^
195 )
196}
197
198rewound_push_succeeded() {
199 cmp ../parent/.git/refs/heads/master .git/refs/heads/master
200}
201
202rewound_push_failed() {
203 if rewound_push_succeeded
204 then
205 false
206 else
207 true
208 fi
209}
210
211test_expect_success 'pushing explicit refspecs respects forcing' '
212 rewound_push_setup &&
213 parent_orig=$(cd parent && git rev-parse --verify master) &&
214 (
215 cd child &&
216 test_must_fail git send-pack ../parent \
217 refs/heads/master:refs/heads/master
218 ) &&
219 parent_head=$(cd parent && git rev-parse --verify master) &&
220 test "$parent_orig" = "$parent_head" &&
221 (
222 cd child &&
223 git send-pack ../parent \
224 +refs/heads/master:refs/heads/master
225 ) &&
226 parent_head=$(cd parent && git rev-parse --verify master) &&
227 child_head=$(cd child && git rev-parse --verify master) &&
228 test "$parent_head" = "$child_head"
229'
230
231test_expect_success 'pushing wildcard refspecs respects forcing' '
232 rewound_push_setup &&
233 parent_orig=$(cd parent && git rev-parse --verify master) &&
234 (
235 cd child &&
236 test_must_fail git send-pack ../parent \
237 "refs/heads/*:refs/heads/*"
238 ) &&
239 parent_head=$(cd parent && git rev-parse --verify master) &&
240 test "$parent_orig" = "$parent_head" &&
241 (
242 cd child &&
243 git send-pack ../parent \
244 "+refs/heads/*:refs/heads/*"
245 ) &&
246 parent_head=$(cd parent && git rev-parse --verify master) &&
247 child_head=$(cd child && git rev-parse --verify master) &&
248 test "$parent_head" = "$child_head"
249'
250
251test_expect_success 'deny pushing to delete current branch' '
252 rewound_push_setup &&
253 (
254 cd child &&
255 test_must_fail git send-pack ../parent :refs/heads/master 2>errs
256 )
257'
258
259test_done