3 # Copyright (c) 2008 Christian Couder
5 test_description
='Tests replace refs functionality'
8 .
"$TEST_DIRECTORY/lib-gpg.sh"
10 add_and_commit_file
()
15 git add
$_file ||
return $?
16 test_tick ||
return $?
17 git commit
--quiet -m "$_file: $_msg"
20 commit_buffer_contains_parents
()
22 git cat-file commit
"$1" >payload
&&
23 sed -n -e '/^$/q' -e '/^parent /p' <payload
>actual
&&
27 echo "parent $_parent"
29 test_cmp expected actual
32 commit_peeling_shows_parents
()
39 _found
=$
(git rev-parse
--verify $_commit^
$_parent_number) ||
return 1
40 test "$_found" = "$_parent" ||
return 1
41 _parent_number
=$
(( $_parent_number + 1 ))
43 test_must_fail git rev-parse
--verify $_commit^
$_parent_number
48 commit_buffer_contains_parents
"$@" &&
49 commit_peeling_shows_parents
"$@"
60 test_expect_success
'set up buggy branch' '
61 echo "line 1" >>hello &&
62 echo "line 2" >>hello &&
63 echo "line 3" >>hello &&
64 echo "line 4" >>hello &&
65 add_and_commit_file hello "4 lines" &&
66 HASH1=$(git rev-parse --verify HEAD) &&
67 echo "line BUG" >>hello &&
68 echo "line 6" >>hello &&
69 echo "line 7" >>hello &&
70 echo "line 8" >>hello &&
71 add_and_commit_file hello "4 more lines with a BUG" &&
72 HASH2=$(git rev-parse --verify HEAD) &&
73 echo "line 9" >>hello &&
74 echo "line 10" >>hello &&
75 add_and_commit_file hello "2 more lines" &&
76 HASH3=$(git rev-parse --verify HEAD) &&
77 echo "line 11" >>hello &&
78 add_and_commit_file hello "1 more line" &&
79 HASH4=$(git rev-parse --verify HEAD) &&
80 sed -e "s/BUG/5/" hello >hello.new &&
82 add_and_commit_file hello "BUG fixed" &&
83 HASH5=$(git rev-parse --verify HEAD) &&
84 echo "line 12" >>hello &&
85 echo "line 13" >>hello &&
86 add_and_commit_file hello "2 more lines" &&
87 HASH6=$(git rev-parse --verify HEAD) &&
88 echo "line 14" >>hello &&
89 echo "line 15" >>hello &&
90 echo "line 16" >>hello &&
91 add_and_commit_file hello "again 3 more lines" &&
92 HASH7=$(git rev-parse --verify HEAD)
95 test_expect_success
'replace the author' '
96 git cat-file commit $HASH2 | grep "author A U Thor" &&
97 R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
98 git cat-file commit $R | grep "author O Thor" &&
99 git update-ref refs/replace/$HASH2 $R &&
100 git show HEAD~5 | grep "O Thor" &&
101 git show $HASH2 | grep "O Thor"
104 test_expect_success
'test --no-replace-objects option' '
105 git cat-file commit $HASH2 | grep "author O Thor" &&
106 git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
107 git show $HASH2 | grep "O Thor" &&
108 git --no-replace-objects show $HASH2 | grep "A U Thor"
111 test_expect_success
'test GIT_NO_REPLACE_OBJECTS env variable' '
112 GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
113 GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
120 tagger T A Gger <> 0 +0000
124 test_expect_success
'tag replaced commit' '
125 git mktag <tag.sig >.git/refs/tags/mytag 2>message
128 test_expect_success
'"git fsck" works' '
129 git fsck master >fsck_master.out &&
130 grep "dangling commit $R" fsck_master.out &&
131 grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
132 test -z "$(git fsck)"
135 test_expect_success
'repack, clone and fetch work' '
137 git clone --no-hardlinks . clone_dir &&
140 git show HEAD~5 | grep "A U Thor" &&
141 git show $HASH2 | grep "A U Thor" &&
142 git cat-file commit $R &&
144 test_must_fail git cat-file commit $R &&
145 git fetch ../ "refs/replace/*:refs/replace/*" &&
146 git show HEAD~5 | grep "O Thor" &&
147 git show $HASH2 | grep "O Thor" &&
148 git cat-file commit $R
152 test_expect_success
'"git replace" listing and deleting' '
153 test "$HASH2" = "$(git replace -l)" &&
154 test "$HASH2" = "$(git replace)" &&
155 aa=${HASH2%??????????????????????????????????????} &&
156 test "$HASH2" = "$(git replace --list "$aa*")" &&
157 test_must_fail git replace -d $R &&
158 test_must_fail git replace --delete &&
159 test_must_fail git replace -l -d $HASH2 &&
160 git replace -d $HASH2 &&
161 git show $HASH2 | grep "A U Thor" &&
162 test -z "$(git replace -l)"
165 test_expect_success
'"git replace" replacing' '
166 git replace $HASH2 $R &&
167 git show $HASH2 | grep "O Thor" &&
168 test_must_fail git replace $HASH2 $R &&
169 git replace -f $HASH2 $R &&
170 test_must_fail git replace -f &&
171 test "$HASH2" = "$(git replace)"
174 test_expect_success
'"git replace" resolves sha1' '
175 SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
176 git replace -d $SHORTHASH2 &&
177 git replace $SHORTHASH2 $R &&
178 git show $HASH2 | grep "O Thor" &&
179 test_must_fail git replace $HASH2 $R &&
180 git replace -f $HASH2 $R &&
181 test_must_fail git replace --force &&
182 test "$HASH2" = "$(git replace)"
185 # This creates a side branch where the bug in H2
186 # does not appear because P2 is created by applying
187 # H2 and squashing H5 into it.
188 # P3, P4 and P6 are created by cherry-picking H3, H4
189 # and H6 respectively.
191 # At this point, we should have the following:
195 # H1-H2-H3-H4-H5-H6-H7
197 # Then we replace H6 with P6.
199 test_expect_success
'create parallel branch without the bug' '
200 git replace -d $HASH2 &&
201 git show $HASH2 | grep "A U Thor" &&
202 git checkout $HASH1 &&
203 git cherry-pick $HASH2 &&
204 git show $HASH5 | git apply &&
205 git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
206 PARA2=$(git rev-parse --verify HEAD) &&
207 git cherry-pick $HASH3 &&
208 PARA3=$(git rev-parse --verify HEAD) &&
209 git cherry-pick $HASH4 &&
210 PARA4=$(git rev-parse --verify HEAD) &&
211 git cherry-pick $HASH6 &&
212 PARA6=$(git rev-parse --verify HEAD) &&
213 git replace $HASH6 $PARA6 &&
214 git checkout master &&
215 cur=$(git rev-parse --verify HEAD) &&
216 test "$cur" = "$HASH7" &&
217 git log --pretty=oneline | grep $PARA2 &&
218 git remote add cloned ./clone_dir
221 test_expect_success
'push to cloned repo' '
222 git push cloned $HASH6^:refs/heads/parallel &&
225 git checkout parallel &&
226 git log --pretty=oneline | grep $PARA2
230 test_expect_success
'push branch with replacement' '
231 git cat-file commit $PARA3 | grep "author A U Thor" &&
232 S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
233 git cat-file commit $S | grep "author O Thor" &&
234 git replace $PARA3 $S &&
235 git show $HASH6~2 | grep "O Thor" &&
236 git show $PARA3 | grep "O Thor" &&
237 git push cloned $HASH6^:refs/heads/parallel2 &&
240 git checkout parallel2 &&
241 git log --pretty=oneline | grep $PARA3 &&
242 git show $PARA3 | grep "A U Thor"
246 test_expect_success
'fetch branch with replacement' '
247 git branch tofetch $HASH6 &&
250 git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
251 git log --pretty=oneline parallel3 >output.txt &&
252 ! grep $PARA3 output.txt &&
253 git show $PARA3 >para3.txt &&
254 grep "A U Thor" para3.txt &&
255 git fetch origin "refs/replace/*:refs/replace/*" &&
256 git log --pretty=oneline parallel3 >output.txt &&
257 grep $PARA3 output.txt &&
258 git show $PARA3 >para3.txt &&
259 grep "O Thor" para3.txt
263 test_expect_success
'bisect and replacements' '
264 git bisect start $HASH7 $HASH1 &&
265 test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
267 GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
268 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
270 git --no-replace-objects bisect start $HASH7 $HASH1 &&
271 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
275 test_expect_success
'index-pack and replacements' '
276 git --no-replace-objects rev-list --objects HEAD |
277 git --no-replace-objects pack-objects test- &&
278 git index-pack test-*.pack
281 test_expect_success
'not just commits' '
282 echo replaced >file &&
284 REPLACED=$(git rev-parse :file) &&
285 mv file file.replaced &&
287 echo original >file &&
289 ORIGINAL=$(git rev-parse :file) &&
290 git update-ref refs/replace/$ORIGINAL $REPLACED &&
291 mv file file.original &&
294 test_cmp file.replaced file
297 test_expect_success
'replaced and replacement objects must be of the same type' '
298 test_must_fail git replace mytag $HASH1 &&
299 test_must_fail git replace HEAD^{tree} HEAD~1 &&
300 BLOB=$(git rev-parse :file) &&
301 test_must_fail git replace HEAD^ $BLOB
304 test_expect_success
'-f option bypasses the type check' '
305 git replace -f mytag $HASH1 &&
306 git replace --force HEAD^{tree} HEAD~1 &&
307 git replace -f HEAD^ $BLOB
310 test_expect_success
'git cat-file --batch works on replace objects' '
311 git replace | grep $PARA3 &&
312 echo $PARA3 | git cat-file --batch
315 test_expect_success
'test --format bogus' '
316 test_must_fail git replace --format bogus >/dev/null 2>&1
319 test_expect_success
'test --format short' '
320 git replace --format=short >actual &&
321 git replace >expected &&
322 test_cmp expected actual
325 test_expect_success
'test --format medium' '
326 H1=$(git --no-replace-objects rev-parse HEAD~1) &&
327 HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
328 MYTAG=$(git --no-replace-objects rev-parse mytag) &&
330 echo "$H1 -> $BLOB" &&
331 echo "$BLOB -> $REPLACED" &&
333 echo "$PARA3 -> $S" &&
334 echo "$MYTAG -> $HASH1"
335 } | sort >expected &&
336 git replace -l --format medium | sort >actual &&
337 test_cmp expected actual
340 test_expect_success
'test --format long' '
342 echo "$H1 (commit) -> $BLOB (blob)" &&
343 echo "$BLOB (blob) -> $REPLACED (blob)" &&
344 echo "$HT (tree) -> $H1 (commit)" &&
345 echo "$PARA3 (commit) -> $S (commit)" &&
346 echo "$MYTAG (tag) -> $HASH1 (commit)"
347 } | sort >expected &&
348 git replace --format=long | sort >actual &&
349 test_cmp expected actual
352 test_expect_success
'setup fake editors' '
353 write_script fakeeditor <<-\EOF &&
354 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
357 write_script failingfakeeditor <<-\EOF
363 test_expect_success
'--edit with and without already replaced object' '
364 test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
365 GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
366 git replace -l | grep "$PARA3" &&
367 git cat-file commit "$PARA3" | grep "A fake Thor" &&
368 git replace -d "$PARA3" &&
369 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
370 git replace -l | grep "$PARA3" &&
371 git cat-file commit "$PARA3" | grep "A fake Thor"
374 test_expect_success
'--edit and change nothing or command failed' '
375 git replace -d "$PARA3" &&
376 test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
377 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
378 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
379 git replace -l | grep "$PARA3" &&
380 git cat-file commit "$PARA3" | grep "A fake Thor"
383 test_expect_success
'replace ref cleanup' '
384 test -n "$(git replace)" &&
385 git replace -d $(git replace) &&
386 test -z "$(git replace)"
389 test_expect_success
'--graft with and without already replaced object' '
390 test $(git log --oneline | wc -l) = 7 &&
391 git replace --graft $HASH5 &&
392 test $(git log --oneline | wc -l) = 3 &&
393 commit_has_parents $HASH5 &&
394 test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
395 git replace --force -g $HASH5 $HASH4 $HASH3 &&
396 commit_has_parents $HASH5 $HASH4 $HASH3 &&
397 git replace -d $HASH5
400 test_expect_success GPG
'set up a signed commit' '
401 echo "line 17" >>hello &&
402 echo "line 18" >>hello &&
405 git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
406 HASH8=$(git rev-parse --verify HEAD) &&
407 git verify-commit $HASH8
410 test_expect_success GPG
'--graft with a signed commit' '
411 git cat-file commit $HASH8 >orig &&
412 git replace --graft $HASH8 &&
413 git cat-file commit $HASH8 >repl &&
414 commit_has_parents $HASH8 &&
415 test_must_fail git verify-commit $HASH8 &&
416 sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
418 sed -e "/^$/q" repl >actual &&
419 test_cmp expected actual &&
420 git replace -d $HASH8
423 test_expect_success GPG
'set up a merge commit with a mergetag' '
424 git reset --hard HEAD &&
425 git checkout -b test_branch HEAD~2 &&
426 echo "line 1 from test branch" >>hello &&
427 echo "line 2 from test branch" >>hello &&
430 git commit -m "hello: 2 more lines from a test branch" &&
431 HASH9=$(git rev-parse --verify HEAD) &&
432 git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
433 git checkout master &&
434 git merge -s ours test_tag &&
435 HASH10=$(git rev-parse --verify HEAD) &&
436 git cat-file commit $HASH10 | grep "^mergetag object"
439 test_expect_success GPG
'--graft on a commit with a mergetag' '
440 test_must_fail git replace --graft $HASH10 $HASH8^1 &&
441 git replace --graft $HASH10 $HASH8^1 $HASH9 &&
442 git replace -d $HASH10
445 test_expect_success
'--convert-graft-file' '
446 git checkout -b with-graft-file &&
448 git reset --hard root2^ &&
450 test_commit after-root1 &&
452 git merge -m merge-root2 root2 &&
454 : add and convert graft file &&
455 printf "%s\n%s %s\n\n# comment\n%s\n" \
456 $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
458 git replace --convert-graft-file &&
459 test_path_is_missing .git/info/grafts &&
461 : verify that the history is now "grafted" &&
462 git rev-list HEAD >out &&
463 test_line_count = 4 out &&
465 : create invalid graft file and verify that it is not deleted &&
466 test_when_finished "rm -f .git/info/grafts" &&
467 echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
468 test_must_fail git replace --convert-graft-file 2>err &&
469 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
470 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts