refspec: s/refspec_item_init/&_or_die/g
[git/debian.git] / t / t6050-replace.sh
blobc630aba657e9c78b6dee4a72be34ed3bd03c01ee
1 #!/bin/sh
3 # Copyright (c) 2008 Christian Couder
5 test_description='Tests replace refs functionality'
7 exec </dev/null
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY/lib-gpg.sh"
12 add_and_commit_file ()
14 _file="$1"
15 _msg="$2"
17 git add $_file || return $?
18 test_tick || return $?
19 git commit --quiet -m "$_file: $_msg"
22 commit_buffer_contains_parents ()
24 git cat-file commit "$1" >payload &&
25 sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
26 shift &&
27 for _parent
29 echo "parent $_parent"
30 done >expected &&
31 test_cmp expected actual
34 commit_peeling_shows_parents ()
36 _parent_number=1
37 _commit="$1"
38 shift &&
39 for _parent
41 _found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
42 test "$_found" = "$_parent" || return 1
43 _parent_number=$(( $_parent_number + 1 ))
44 done &&
45 test_must_fail git rev-parse --verify $_commit^$_parent_number
48 commit_has_parents ()
50 commit_buffer_contains_parents "$@" &&
51 commit_peeling_shows_parents "$@"
54 HASH1=
55 HASH2=
56 HASH3=
57 HASH4=
58 HASH5=
59 HASH6=
60 HASH7=
62 test_expect_success 'set up buggy branch' '
63 echo "line 1" >>hello &&
64 echo "line 2" >>hello &&
65 echo "line 3" >>hello &&
66 echo "line 4" >>hello &&
67 add_and_commit_file hello "4 lines" &&
68 HASH1=$(git rev-parse --verify HEAD) &&
69 echo "line BUG" >>hello &&
70 echo "line 6" >>hello &&
71 echo "line 7" >>hello &&
72 echo "line 8" >>hello &&
73 add_and_commit_file hello "4 more lines with a BUG" &&
74 HASH2=$(git rev-parse --verify HEAD) &&
75 echo "line 9" >>hello &&
76 echo "line 10" >>hello &&
77 add_and_commit_file hello "2 more lines" &&
78 HASH3=$(git rev-parse --verify HEAD) &&
79 echo "line 11" >>hello &&
80 add_and_commit_file hello "1 more line" &&
81 HASH4=$(git rev-parse --verify HEAD) &&
82 sed -e "s/BUG/5/" hello >hello.new &&
83 mv hello.new hello &&
84 add_and_commit_file hello "BUG fixed" &&
85 HASH5=$(git rev-parse --verify HEAD) &&
86 echo "line 12" >>hello &&
87 echo "line 13" >>hello &&
88 add_and_commit_file hello "2 more lines" &&
89 HASH6=$(git rev-parse --verify HEAD) &&
90 echo "line 14" >>hello &&
91 echo "line 15" >>hello &&
92 echo "line 16" >>hello &&
93 add_and_commit_file hello "again 3 more lines" &&
94 HASH7=$(git rev-parse --verify HEAD)
97 test_expect_success 'replace the author' '
98 git cat-file commit $HASH2 | grep "author A U Thor" &&
99 R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
100 git cat-file commit $R | grep "author O Thor" &&
101 git update-ref refs/replace/$HASH2 $R &&
102 git show HEAD~5 | grep "O Thor" &&
103 git show $HASH2 | grep "O Thor"
106 test_expect_success 'test --no-replace-objects option' '
107 git cat-file commit $HASH2 | grep "author O Thor" &&
108 git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
109 git show $HASH2 | grep "O Thor" &&
110 git --no-replace-objects show $HASH2 | grep "A U Thor"
113 test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
114 GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
115 GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
118 cat >tag.sig <<EOF
119 object $HASH2
120 type commit
121 tag mytag
122 tagger T A Gger <> 0 +0000
126 test_expect_success 'tag replaced commit' '
127 git mktag <tag.sig >.git/refs/tags/mytag 2>message
130 test_expect_success '"git fsck" works' '
131 git fsck master >fsck_master.out &&
132 grep "dangling commit $R" fsck_master.out &&
133 grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
134 test -z "$(git fsck)"
137 test_expect_success 'repack, clone and fetch work' '
138 git repack -a -d &&
139 git clone --no-hardlinks . clone_dir &&
141 cd clone_dir &&
142 git show HEAD~5 | grep "A U Thor" &&
143 git show $HASH2 | grep "A U Thor" &&
144 git cat-file commit $R &&
145 git repack -a -d &&
146 test_must_fail git cat-file commit $R &&
147 git fetch ../ "refs/replace/*:refs/replace/*" &&
148 git show HEAD~5 | grep "O Thor" &&
149 git show $HASH2 | grep "O Thor" &&
150 git cat-file commit $R
154 test_expect_success '"git replace" listing and deleting' '
155 test "$HASH2" = "$(git replace -l)" &&
156 test "$HASH2" = "$(git replace)" &&
157 aa=${HASH2%??????????????????????????????????????} &&
158 test "$HASH2" = "$(git replace --list "$aa*")" &&
159 test_must_fail git replace -d $R &&
160 test_must_fail git replace --delete &&
161 test_must_fail git replace -l -d $HASH2 &&
162 git replace -d $HASH2 &&
163 git show $HASH2 | grep "A U Thor" &&
164 test -z "$(git replace -l)"
167 test_expect_success '"git replace" replacing' '
168 git replace $HASH2 $R &&
169 git show $HASH2 | grep "O Thor" &&
170 test_must_fail git replace $HASH2 $R &&
171 git replace -f $HASH2 $R &&
172 test_must_fail git replace -f &&
173 test "$HASH2" = "$(git replace)"
176 test_expect_success '"git replace" resolves sha1' '
177 SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
178 git replace -d $SHORTHASH2 &&
179 git replace $SHORTHASH2 $R &&
180 git show $HASH2 | grep "O Thor" &&
181 test_must_fail git replace $HASH2 $R &&
182 git replace -f $HASH2 $R &&
183 test_must_fail git replace --force &&
184 test "$HASH2" = "$(git replace)"
187 # This creates a side branch where the bug in H2
188 # does not appear because P2 is created by applying
189 # H2 and squashing H5 into it.
190 # P3, P4 and P6 are created by cherry-picking H3, H4
191 # and H6 respectively.
193 # At this point, we should have the following:
195 # P2--P3--P4--P6
197 # H1-H2-H3-H4-H5-H6-H7
199 # Then we replace H6 with P6.
201 test_expect_success 'create parallel branch without the bug' '
202 git replace -d $HASH2 &&
203 git show $HASH2 | grep "A U Thor" &&
204 git checkout $HASH1 &&
205 git cherry-pick $HASH2 &&
206 git show $HASH5 | git apply &&
207 git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
208 PARA2=$(git rev-parse --verify HEAD) &&
209 git cherry-pick $HASH3 &&
210 PARA3=$(git rev-parse --verify HEAD) &&
211 git cherry-pick $HASH4 &&
212 PARA4=$(git rev-parse --verify HEAD) &&
213 git cherry-pick $HASH6 &&
214 PARA6=$(git rev-parse --verify HEAD) &&
215 git replace $HASH6 $PARA6 &&
216 git checkout master &&
217 cur=$(git rev-parse --verify HEAD) &&
218 test "$cur" = "$HASH7" &&
219 git log --pretty=oneline | grep $PARA2 &&
220 git remote add cloned ./clone_dir
223 test_expect_success 'push to cloned repo' '
224 git push cloned $HASH6^:refs/heads/parallel &&
226 cd clone_dir &&
227 git checkout parallel &&
228 git log --pretty=oneline | grep $PARA2
232 test_expect_success 'push branch with replacement' '
233 git cat-file commit $PARA3 | grep "author A U Thor" &&
234 S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
235 git cat-file commit $S | grep "author O Thor" &&
236 git replace $PARA3 $S &&
237 git show $HASH6~2 | grep "O Thor" &&
238 git show $PARA3 | grep "O Thor" &&
239 git push cloned $HASH6^:refs/heads/parallel2 &&
241 cd clone_dir &&
242 git checkout parallel2 &&
243 git log --pretty=oneline | grep $PARA3 &&
244 git show $PARA3 | grep "A U Thor"
248 test_expect_success 'fetch branch with replacement' '
249 git branch tofetch $HASH6 &&
251 cd clone_dir &&
252 git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
253 git log --pretty=oneline parallel3 >output.txt &&
254 ! grep $PARA3 output.txt &&
255 git show $PARA3 >para3.txt &&
256 grep "A U Thor" para3.txt &&
257 git fetch origin "refs/replace/*:refs/replace/*" &&
258 git log --pretty=oneline parallel3 >output.txt &&
259 grep $PARA3 output.txt &&
260 git show $PARA3 >para3.txt &&
261 grep "O Thor" para3.txt
265 test_expect_success 'bisect and replacements' '
266 git bisect start $HASH7 $HASH1 &&
267 test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
268 git bisect reset &&
269 GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
270 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
271 git bisect reset &&
272 git --no-replace-objects bisect start $HASH7 $HASH1 &&
273 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
274 git bisect reset
277 test_expect_success 'index-pack and replacements' '
278 git --no-replace-objects rev-list --objects HEAD |
279 git --no-replace-objects pack-objects test- &&
280 git index-pack test-*.pack
283 test_expect_success 'not just commits' '
284 echo replaced >file &&
285 git add file &&
286 REPLACED=$(git rev-parse :file) &&
287 mv file file.replaced &&
289 echo original >file &&
290 git add file &&
291 ORIGINAL=$(git rev-parse :file) &&
292 git update-ref refs/replace/$ORIGINAL $REPLACED &&
293 mv file file.original &&
295 git checkout file &&
296 test_cmp file.replaced file
299 test_expect_success 'replaced and replacement objects must be of the same type' '
300 test_must_fail git replace mytag $HASH1 &&
301 test_must_fail git replace HEAD^{tree} HEAD~1 &&
302 BLOB=$(git rev-parse :file) &&
303 test_must_fail git replace HEAD^ $BLOB
306 test_expect_success '-f option bypasses the type check' '
307 git replace -f mytag $HASH1 &&
308 git replace --force HEAD^{tree} HEAD~1 &&
309 git replace -f HEAD^ $BLOB
312 test_expect_success 'git cat-file --batch works on replace objects' '
313 git replace | grep $PARA3 &&
314 echo $PARA3 | git cat-file --batch
317 test_expect_success 'test --format bogus' '
318 test_must_fail git replace --format bogus >/dev/null 2>&1
321 test_expect_success 'test --format short' '
322 git replace --format=short >actual &&
323 git replace >expected &&
324 test_cmp expected actual
327 test_expect_success 'test --format medium' '
328 H1=$(git --no-replace-objects rev-parse HEAD~1) &&
329 HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
330 MYTAG=$(git --no-replace-objects rev-parse mytag) &&
332 echo "$H1 -> $BLOB" &&
333 echo "$BLOB -> $REPLACED" &&
334 echo "$HT -> $H1" &&
335 echo "$PARA3 -> $S" &&
336 echo "$MYTAG -> $HASH1"
337 } | sort >expected &&
338 git replace -l --format medium | sort >actual &&
339 test_cmp expected actual
342 test_expect_success 'test --format long' '
344 echo "$H1 (commit) -> $BLOB (blob)" &&
345 echo "$BLOB (blob) -> $REPLACED (blob)" &&
346 echo "$HT (tree) -> $H1 (commit)" &&
347 echo "$PARA3 (commit) -> $S (commit)" &&
348 echo "$MYTAG (tag) -> $HASH1 (commit)"
349 } | sort >expected &&
350 git replace --format=long | sort >actual &&
351 test_cmp expected actual
354 test_expect_success 'setup fake editors' '
355 write_script fakeeditor <<-\EOF &&
356 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
357 mv "$1.new" "$1"
359 write_script failingfakeeditor <<-\EOF
360 ./fakeeditor "$@"
361 false
365 test_expect_success '--edit with and without already replaced object' '
366 test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
367 GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
368 git replace -l | grep "$PARA3" &&
369 git cat-file commit "$PARA3" | grep "A fake Thor" &&
370 git replace -d "$PARA3" &&
371 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
372 git replace -l | grep "$PARA3" &&
373 git cat-file commit "$PARA3" | grep "A fake Thor"
376 test_expect_success '--edit and change nothing or command failed' '
377 git replace -d "$PARA3" &&
378 test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
379 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
380 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
381 git replace -l | grep "$PARA3" &&
382 git cat-file commit "$PARA3" | grep "A fake Thor"
385 test_expect_success 'replace ref cleanup' '
386 test -n "$(git replace)" &&
387 git replace -d $(git replace) &&
388 test -z "$(git replace)"
391 test_expect_success '--graft with and without already replaced object' '
392 test $(git log --oneline | wc -l) = 7 &&
393 git replace --graft $HASH5 &&
394 test $(git log --oneline | wc -l) = 3 &&
395 commit_has_parents $HASH5 &&
396 test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
397 git replace --force -g $HASH5 $HASH4 $HASH3 &&
398 commit_has_parents $HASH5 $HASH4 $HASH3 &&
399 git replace -d $HASH5
402 test_expect_success GPG 'set up a signed commit' '
403 echo "line 17" >>hello &&
404 echo "line 18" >>hello &&
405 git add hello &&
406 test_tick &&
407 git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
408 HASH8=$(git rev-parse --verify HEAD) &&
409 git verify-commit $HASH8
412 test_expect_success GPG '--graft with a signed commit' '
413 git cat-file commit $HASH8 >orig &&
414 git replace --graft $HASH8 &&
415 git cat-file commit $HASH8 >repl &&
416 commit_has_parents $HASH8 &&
417 test_must_fail git verify-commit $HASH8 &&
418 sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
419 echo >>expected &&
420 sed -e "/^$/q" repl >actual &&
421 test_cmp expected actual &&
422 git replace -d $HASH8
425 test_expect_success GPG 'set up a merge commit with a mergetag' '
426 git reset --hard HEAD &&
427 git checkout -b test_branch HEAD~2 &&
428 echo "line 1 from test branch" >>hello &&
429 echo "line 2 from test branch" >>hello &&
430 git add hello &&
431 test_tick &&
432 git commit -m "hello: 2 more lines from a test branch" &&
433 HASH9=$(git rev-parse --verify HEAD) &&
434 git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
435 git checkout master &&
436 git merge -s ours test_tag &&
437 HASH10=$(git rev-parse --verify HEAD) &&
438 git cat-file commit $HASH10 | grep "^mergetag object"
441 test_expect_success GPG '--graft on a commit with a mergetag' '
442 test_must_fail git replace --graft $HASH10 $HASH8^1 &&
443 git replace --graft $HASH10 $HASH8^1 $HASH9 &&
444 git replace -d $HASH10
447 test_done