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"
116 test_expect_success 'test core.usereplacerefs config option' '
117 test_config core.usereplacerefs false &&
118 git cat-file commit $HASH2 | grep "author A U Thor" &&
119 git show $HASH2 | grep "A U Thor"
126 tagger T A Gger <> 0 +0000
130 test_expect_success 'tag replaced commit' '
131 git mktag <tag.sig >.git/refs/tags/mytag 2>message
134 test_expect_success '"git fsck" works' '
135 git fsck master >fsck_master.out &&
136 grep "dangling commit $R" fsck_master.out &&
137 grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
138 test -z "$(git fsck)"
141 test_expect_success 'repack, clone and fetch work' '
143 git clone --no-hardlinks . clone_dir &&
146 git show HEAD~5 | grep "A U Thor" &&
147 git show $HASH2 | grep "A U Thor" &&
148 git cat-file commit $R &&
150 test_must_fail git cat-file commit $R &&
151 git fetch ../ "refs/replace/*:refs/replace/*" &&
152 git show HEAD~5 | grep "O Thor" &&
153 git show $HASH2 | grep "O Thor" &&
154 git cat-file commit $R
158 test_expect_success '"git replace" listing and deleting' '
159 test "$HASH2" = "$(git replace -l)" &&
160 test "$HASH2" = "$(git replace)" &&
161 aa=${HASH2%??????????????????????????????????????} &&
162 test "$HASH2" = "$(git replace --list "$aa*")" &&
163 test_must_fail git replace -d $R &&
164 test_must_fail git replace --delete &&
165 test_must_fail git replace -l -d $HASH2 &&
166 git replace -d $HASH2 &&
167 git show $HASH2 | grep "A U Thor" &&
168 test -z "$(git replace -l)"
171 test_expect_success '"git replace" replacing' '
172 git replace $HASH2 $R &&
173 git show $HASH2 | grep "O Thor" &&
174 test_must_fail git replace $HASH2 $R &&
175 git replace -f $HASH2 $R &&
176 test_must_fail git replace -f &&
177 test "$HASH2" = "$(git replace)"
180 test_expect_success '"git replace" resolves sha1' '
181 SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
182 git replace -d $SHORTHASH2 &&
183 git replace $SHORTHASH2 $R &&
184 git show $HASH2 | grep "O Thor" &&
185 test_must_fail git replace $HASH2 $R &&
186 git replace -f $HASH2 $R &&
187 test_must_fail git replace --force &&
188 test "$HASH2" = "$(git replace)"
191 # This creates a side branch where the bug in H2
192 # does not appear because P2 is created by applying
193 # H2 and squashing H5 into it.
194 # P3, P4 and P6 are created by cherry-picking H3, H4
195 # and H6 respectively.
197 # At this point, we should have the following:
201 # H1-H2-H3-H4-H5-H6-H7
203 # Then we replace H6 with P6.
205 test_expect_success 'create parallel branch without the bug' '
206 git replace -d $HASH2 &&
207 git show $HASH2 | grep "A U Thor" &&
208 git checkout $HASH1 &&
209 git cherry-pick $HASH2 &&
210 git show $HASH5 | git apply &&
211 git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
212 PARA2=$(git rev-parse --verify HEAD) &&
213 git cherry-pick $HASH3 &&
214 PARA3=$(git rev-parse --verify HEAD) &&
215 git cherry-pick $HASH4 &&
216 PARA4=$(git rev-parse --verify HEAD) &&
217 git cherry-pick $HASH6 &&
218 PARA6=$(git rev-parse --verify HEAD) &&
219 git replace $HASH6 $PARA6 &&
220 git checkout master &&
221 cur=$(git rev-parse --verify HEAD) &&
222 test "$cur" = "$HASH7" &&
223 git log --pretty=oneline | grep $PARA2 &&
224 git remote add cloned ./clone_dir
227 test_expect_success 'push to cloned repo' '
228 git push cloned $HASH6^:refs/heads/parallel &&
231 git checkout parallel &&
232 git log --pretty=oneline | grep $PARA2
236 test_expect_success 'push branch with replacement' '
237 git cat-file commit $PARA3 | grep "author A U Thor" &&
238 S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
239 git cat-file commit $S | grep "author O Thor" &&
240 git replace $PARA3 $S &&
241 git show $HASH6~2 | grep "O Thor" &&
242 git show $PARA3 | grep "O Thor" &&
243 git push cloned $HASH6^:refs/heads/parallel2 &&
246 git checkout parallel2 &&
247 git log --pretty=oneline | grep $PARA3 &&
248 git show $PARA3 | grep "A U Thor"
252 test_expect_success 'fetch branch with replacement' '
253 git branch tofetch $HASH6 &&
256 git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
257 git log --pretty=oneline parallel3 >output.txt &&
258 ! grep $PARA3 output.txt &&
259 git show $PARA3 >para3.txt &&
260 grep "A U Thor" para3.txt &&
261 git fetch origin "refs/replace/*:refs/replace/*" &&
262 git log --pretty=oneline parallel3 >output.txt &&
263 grep $PARA3 output.txt &&
264 git show $PARA3 >para3.txt &&
265 grep "O Thor" para3.txt
269 test_expect_success 'bisect and replacements' '
270 git bisect start $HASH7 $HASH1 &&
271 test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
273 GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
274 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
276 git --no-replace-objects bisect start $HASH7 $HASH1 &&
277 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
281 test_expect_success 'index-pack and replacements' '
282 git --no-replace-objects rev-list --objects HEAD |
283 git --no-replace-objects pack-objects test- &&
284 git index-pack test-*.pack
287 test_expect_success 'not just commits' '
288 echo replaced >file &&
290 REPLACED=$(git rev-parse :file) &&
291 mv file file.replaced &&
293 echo original >file &&
295 ORIGINAL=$(git rev-parse :file) &&
296 git update-ref refs/replace/$ORIGINAL $REPLACED &&
297 mv file file.original &&
300 test_cmp file.replaced file
303 test_expect_success 'replaced and replacement objects must be of the same type' '
304 test_must_fail git replace mytag $HASH1 &&
305 test_must_fail git replace HEAD^{tree} HEAD~1 &&
306 BLOB=$(git rev-parse :file) &&
307 test_must_fail git replace HEAD^ $BLOB
310 test_expect_success '-f option bypasses the type check' '
311 git replace -f mytag $HASH1 &&
312 git replace --force HEAD^{tree} HEAD~1 &&
313 git replace -f HEAD^ $BLOB
316 test_expect_success 'git cat-file --batch works on replace objects' '
317 git replace | grep $PARA3 &&
318 echo $PARA3 | git cat-file --batch
321 test_expect_success 'test --format bogus' '
322 test_must_fail git replace --format bogus >/dev/null 2>&1
325 test_expect_success 'test --format short' '
326 git replace --format=short >actual &&
327 git replace >expected &&
328 test_cmp expected actual
331 test_expect_success 'test --format medium' '
332 H1=$(git --no-replace-objects rev-parse HEAD~1) &&
333 HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
334 MYTAG=$(git --no-replace-objects rev-parse mytag) &&
336 echo "$H1 -> $BLOB" &&
337 echo "$BLOB -> $REPLACED" &&
339 echo "$PARA3 -> $S" &&
340 echo "$MYTAG -> $HASH1"
341 } | sort >expected &&
342 git replace -l --format medium | sort >actual &&
343 test_cmp expected actual
346 test_expect_success 'test --format long' '
348 echo "$H1 (commit) -> $BLOB (blob)" &&
349 echo "$BLOB (blob) -> $REPLACED (blob)" &&
350 echo "$HT (tree) -> $H1 (commit)" &&
351 echo "$PARA3 (commit) -> $S (commit)" &&
352 echo "$MYTAG (tag) -> $HASH1 (commit)"
353 } | sort >expected &&
354 git replace --format=long | sort >actual &&
355 test_cmp expected actual
358 test_expect_success 'setup fake editors' '
359 write_script fakeeditor <<-\EOF &&
360 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
363 write_script failingfakeeditor <<-\EOF
369 test_expect_success '--edit with and without already replaced object' '
370 test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
371 GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
372 git replace -l | grep "$PARA3" &&
373 git cat-file commit "$PARA3" | grep "A fake Thor" &&
374 git replace -d "$PARA3" &&
375 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
376 git replace -l | grep "$PARA3" &&
377 git cat-file commit "$PARA3" | grep "A fake Thor"
380 test_expect_success '--edit and change nothing or command failed' '
381 git replace -d "$PARA3" &&
382 test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
383 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
384 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
385 git replace -l | grep "$PARA3" &&
386 git cat-file commit "$PARA3" | grep "A fake Thor"
389 test_expect_success 'replace ref cleanup' '
390 test -n "$(git replace)" &&
391 git replace -d $(git replace) &&
392 test -z "$(git replace)"
395 test_expect_success '--graft with and without already replaced object' '
396 test $(git log --oneline | wc -l) = 7 &&
397 git replace --graft $HASH5 &&
398 test $(git log --oneline | wc -l) = 3 &&
399 commit_has_parents $HASH5 &&
400 test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
401 git replace --force -g $HASH5 $HASH4 $HASH3 &&
402 commit_has_parents $HASH5 $HASH4 $HASH3 &&
403 git replace -d $HASH5
406 test_expect_success GPG 'set up a signed commit' '
407 echo "line 17" >>hello &&
408 echo "line 18" >>hello &&
411 git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
412 HASH8=$(git rev-parse --verify HEAD) &&
413 git verify-commit $HASH8
416 test_expect_success GPG '--graft with a signed commit' '
417 git cat-file commit $HASH8 >orig &&
418 git replace --graft $HASH8 &&
419 git cat-file commit $HASH8 >repl &&
420 commit_has_parents $HASH8 &&
421 test_must_fail git verify-commit $HASH8 &&
422 sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
424 sed -e "/^$/q" repl >actual &&
425 test_cmp expected actual &&
426 git replace -d $HASH8
429 test_expect_success GPG 'set up a merge commit with a mergetag' '
430 git reset --hard HEAD &&
431 git checkout -b test_branch HEAD~2 &&
432 echo "line 1 from test branch" >>hello &&
433 echo "line 2 from test branch" >>hello &&
436 git commit -m "hello: 2 more lines from a test branch" &&
437 HASH9=$(git rev-parse --verify HEAD) &&
438 git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
439 git checkout master &&
440 git merge -s ours test_tag &&
441 HASH10=$(git rev-parse --verify HEAD) &&
442 git cat-file commit $HASH10 | grep "^mergetag object"
445 test_expect_success GPG '--graft on a commit with a mergetag' '
446 test_must_fail git replace --graft $HASH10 $HASH8^1 &&
447 git replace --graft $HASH10 $HASH8^1 $HASH9 &&
448 git replace -d $HASH10
451 test_expect_success '--convert-graft-file' '
452 git checkout -b with-graft-file &&
454 git reset --hard root2^ &&
456 test_commit after-root1 &&
458 git merge -m merge-root2 root2 &&
460 : add and convert graft file &&
461 printf "%s\n%s %s\n\n# comment\n%s\n" \
462 $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
464 git status 2>stderr &&
465 test_i18ngrep "hint:.*grafts is deprecated" stderr &&
466 git replace --convert-graft-file 2>stderr &&
467 test_i18ngrep ! "hint:.*grafts is deprecated" stderr &&
468 test_path_is_missing .git/info/grafts &&
470 : verify that the history is now "grafted" &&
471 git rev-list HEAD >out &&
472 test_line_count = 4 out &&
474 : create invalid graft file and verify that it is not deleted &&
475 test_when_finished "rm -f .git/info/grafts" &&
476 echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
477 test_must_fail git replace --convert-graft-file 2>err &&
478 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
479 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts