Use the final_oid_fn to finalize hashing of object IDs
[alt-git.git] / t / t6050-replace.sh
blobe33d512ec11e844147380cabbbada45e31e4d65b
1 #!/bin/sh
3 # Copyright (c) 2008 Christian Couder
5 test_description='Tests replace refs functionality'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY/lib-gpg.sh"
13 add_and_commit_file ()
15 _file="$1"
16 _msg="$2"
18 git add $_file || return $?
19 test_tick || return $?
20 git commit --quiet -m "$_file: $_msg"
23 commit_buffer_contains_parents ()
25 git cat-file commit "$1" >payload &&
26 sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
27 shift &&
28 for _parent
30 echo "parent $_parent"
31 done >expected &&
32 test_cmp expected actual
35 commit_peeling_shows_parents ()
37 _parent_number=1
38 _commit="$1"
39 shift &&
40 for _parent
42 _found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
43 test "$_found" = "$_parent" || return 1
44 _parent_number=$(( $_parent_number + 1 ))
45 done &&
46 test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
47 test_i18ngrep "Needed a single revision" err
50 commit_has_parents ()
52 commit_buffer_contains_parents "$@" &&
53 commit_peeling_shows_parents "$@"
56 HASH1=
57 HASH2=
58 HASH3=
59 HASH4=
60 HASH5=
61 HASH6=
62 HASH7=
64 test_expect_success 'set up buggy branch' '
65 echo "line 1" >>hello &&
66 echo "line 2" >>hello &&
67 echo "line 3" >>hello &&
68 echo "line 4" >>hello &&
69 add_and_commit_file hello "4 lines" &&
70 HASH1=$(git rev-parse --verify HEAD) &&
71 echo "line BUG" >>hello &&
72 echo "line 6" >>hello &&
73 echo "line 7" >>hello &&
74 echo "line 8" >>hello &&
75 add_and_commit_file hello "4 more lines with a BUG" &&
76 HASH2=$(git rev-parse --verify HEAD) &&
77 echo "line 9" >>hello &&
78 echo "line 10" >>hello &&
79 add_and_commit_file hello "2 more lines" &&
80 HASH3=$(git rev-parse --verify HEAD) &&
81 echo "line 11" >>hello &&
82 add_and_commit_file hello "1 more line" &&
83 HASH4=$(git rev-parse --verify HEAD) &&
84 sed -e "s/BUG/5/" hello >hello.new &&
85 mv hello.new hello &&
86 add_and_commit_file hello "BUG fixed" &&
87 HASH5=$(git rev-parse --verify HEAD) &&
88 echo "line 12" >>hello &&
89 echo "line 13" >>hello &&
90 add_and_commit_file hello "2 more lines" &&
91 HASH6=$(git rev-parse --verify HEAD) &&
92 echo "line 14" >>hello &&
93 echo "line 15" >>hello &&
94 echo "line 16" >>hello &&
95 add_and_commit_file hello "again 3 more lines" &&
96 HASH7=$(git rev-parse --verify HEAD)
99 test_expect_success 'replace the author' '
100 git cat-file commit $HASH2 | grep "author A U Thor" &&
101 R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
102 git cat-file commit $R | grep "author O Thor" &&
103 git update-ref refs/replace/$HASH2 $R &&
104 git show HEAD~5 | grep "O Thor" &&
105 git show $HASH2 | grep "O Thor"
108 test_expect_success 'test --no-replace-objects option' '
109 git cat-file commit $HASH2 | grep "author O Thor" &&
110 git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
111 git show $HASH2 | grep "O Thor" &&
112 git --no-replace-objects show $HASH2 | grep "A U Thor"
115 test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
116 GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
117 GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
120 test_expect_success 'test core.usereplacerefs config option' '
121 test_config core.usereplacerefs false &&
122 git cat-file commit $HASH2 | grep "author A U Thor" &&
123 git show $HASH2 | grep "A U Thor"
126 cat >tag.sig <<EOF
127 object $HASH2
128 type commit
129 tag mytag
130 tagger T A Gger <> 0 +0000
134 test_expect_success 'tag replaced commit' '
135 git mktag <tag.sig >.git/refs/tags/mytag
138 test_expect_success '"git fsck" works' '
139 git fsck main >fsck_main.out &&
140 test_i18ngrep "dangling commit $R" fsck_main.out &&
141 test_i18ngrep "dangling tag $(git show-ref -s refs/tags/mytag)" fsck_main.out &&
142 test -z "$(git fsck)"
145 test_expect_success 'repack, clone and fetch work' '
146 git repack -a -d &&
147 git clone --no-hardlinks . clone_dir &&
149 cd clone_dir &&
150 git show HEAD~5 | grep "A U Thor" &&
151 git show $HASH2 | grep "A U Thor" &&
152 git cat-file commit $R &&
153 git repack -a -d &&
154 test_must_fail git cat-file commit $R &&
155 git fetch ../ "refs/replace/*:refs/replace/*" &&
156 git show HEAD~5 | grep "O Thor" &&
157 git show $HASH2 | grep "O Thor" &&
158 git cat-file commit $R
162 test_expect_success '"git replace" listing and deleting' '
163 test "$HASH2" = "$(git replace -l)" &&
164 test "$HASH2" = "$(git replace)" &&
165 aa=${HASH2%??????????????????????????????????????} &&
166 test "$HASH2" = "$(git replace --list "$aa*")" &&
167 test_must_fail git replace -d $R &&
168 test_must_fail git replace --delete &&
169 test_must_fail git replace -l -d $HASH2 &&
170 git replace -d $HASH2 &&
171 git show $HASH2 | grep "A U Thor" &&
172 test -z "$(git replace -l)"
175 test_expect_success '"git replace" replacing' '
176 git replace $HASH2 $R &&
177 git show $HASH2 | grep "O Thor" &&
178 test_must_fail git replace $HASH2 $R &&
179 git replace -f $HASH2 $R &&
180 test_must_fail git replace -f &&
181 test "$HASH2" = "$(git replace)"
184 test_expect_success '"git replace" resolves sha1' '
185 SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
186 git replace -d $SHORTHASH2 &&
187 git replace $SHORTHASH2 $R &&
188 git show $HASH2 | grep "O Thor" &&
189 test_must_fail git replace $HASH2 $R &&
190 git replace -f $HASH2 $R &&
191 test_must_fail git replace --force &&
192 test "$HASH2" = "$(git replace)"
195 # This creates a side branch where the bug in H2
196 # does not appear because P2 is created by applying
197 # H2 and squashing H5 into it.
198 # P3, P4 and P6 are created by cherry-picking H3, H4
199 # and H6 respectively.
201 # At this point, we should have the following:
203 # P2--P3--P4--P6
205 # H1-H2-H3-H4-H5-H6-H7
207 # Then we replace H6 with P6.
209 test_expect_success 'create parallel branch without the bug' '
210 git replace -d $HASH2 &&
211 git show $HASH2 | grep "A U Thor" &&
212 git checkout $HASH1 &&
213 git cherry-pick $HASH2 &&
214 git show $HASH5 | git apply &&
215 git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
216 PARA2=$(git rev-parse --verify HEAD) &&
217 git cherry-pick $HASH3 &&
218 PARA3=$(git rev-parse --verify HEAD) &&
219 git cherry-pick $HASH4 &&
220 PARA4=$(git rev-parse --verify HEAD) &&
221 git cherry-pick $HASH6 &&
222 PARA6=$(git rev-parse --verify HEAD) &&
223 git replace $HASH6 $PARA6 &&
224 git checkout main &&
225 cur=$(git rev-parse --verify HEAD) &&
226 test "$cur" = "$HASH7" &&
227 git log --pretty=oneline | grep $PARA2 &&
228 git remote add cloned ./clone_dir
231 test_expect_success 'push to cloned repo' '
232 git push cloned $HASH6^:refs/heads/parallel &&
234 cd clone_dir &&
235 git checkout parallel &&
236 git log --pretty=oneline | grep $PARA2
240 test_expect_success 'push branch with replacement' '
241 git cat-file commit $PARA3 | grep "author A U Thor" &&
242 S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
243 git cat-file commit $S | grep "author O Thor" &&
244 git replace $PARA3 $S &&
245 git show $HASH6~2 | grep "O Thor" &&
246 git show $PARA3 | grep "O Thor" &&
247 git push cloned $HASH6^:refs/heads/parallel2 &&
249 cd clone_dir &&
250 git checkout parallel2 &&
251 git log --pretty=oneline | grep $PARA3 &&
252 git show $PARA3 | grep "A U Thor"
256 test_expect_success 'fetch branch with replacement' '
257 git branch tofetch $HASH6 &&
259 cd clone_dir &&
260 git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
261 git log --pretty=oneline parallel3 >output.txt &&
262 ! grep $PARA3 output.txt &&
263 git show $PARA3 >para3.txt &&
264 grep "A U Thor" para3.txt &&
265 git fetch origin "refs/replace/*:refs/replace/*" &&
266 git log --pretty=oneline parallel3 >output.txt &&
267 grep $PARA3 output.txt &&
268 git show $PARA3 >para3.txt &&
269 grep "O Thor" para3.txt
273 test_expect_success 'bisect and replacements' '
274 git bisect start $HASH7 $HASH1 &&
275 test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
276 git bisect reset &&
277 GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
278 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
279 git bisect reset &&
280 git --no-replace-objects bisect start $HASH7 $HASH1 &&
281 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
282 git bisect reset
285 test_expect_success 'index-pack and replacements' '
286 git --no-replace-objects rev-list --objects HEAD |
287 git --no-replace-objects pack-objects test- &&
288 git index-pack test-*.pack
291 test_expect_success 'not just commits' '
292 echo replaced >file &&
293 git add file &&
294 REPLACED=$(git rev-parse :file) &&
295 mv file file.replaced &&
297 echo original >file &&
298 git add file &&
299 ORIGINAL=$(git rev-parse :file) &&
300 git update-ref refs/replace/$ORIGINAL $REPLACED &&
301 mv file file.original &&
303 git checkout file &&
304 test_cmp file.replaced file
307 test_expect_success 'replaced and replacement objects must be of the same type' '
308 test_must_fail git replace mytag $HASH1 &&
309 test_must_fail git replace HEAD^{tree} HEAD~1 &&
310 BLOB=$(git rev-parse :file) &&
311 test_must_fail git replace HEAD^ $BLOB
314 test_expect_success '-f option bypasses the type check' '
315 git replace -f mytag $HASH1 &&
316 git replace --force HEAD^{tree} HEAD~1 &&
317 git replace -f HEAD^ $BLOB
320 test_expect_success 'git cat-file --batch works on replace objects' '
321 git replace | grep $PARA3 &&
322 echo $PARA3 | git cat-file --batch
325 test_expect_success 'test --format bogus' '
326 test_must_fail git replace --format bogus >/dev/null 2>&1
329 test_expect_success 'test --format short' '
330 git replace --format=short >actual &&
331 git replace >expected &&
332 test_cmp expected actual
335 test_expect_success 'test --format medium' '
336 H1=$(git --no-replace-objects rev-parse HEAD~1) &&
337 HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
338 MYTAG=$(git --no-replace-objects rev-parse mytag) &&
340 echo "$H1 -> $BLOB" &&
341 echo "$BLOB -> $REPLACED" &&
342 echo "$HT -> $H1" &&
343 echo "$PARA3 -> $S" &&
344 echo "$MYTAG -> $HASH1"
345 } | sort >expected &&
346 git replace -l --format medium | sort >actual &&
347 test_cmp expected actual
350 test_expect_success 'test --format long' '
352 echo "$H1 (commit) -> $BLOB (blob)" &&
353 echo "$BLOB (blob) -> $REPLACED (blob)" &&
354 echo "$HT (tree) -> $H1 (commit)" &&
355 echo "$PARA3 (commit) -> $S (commit)" &&
356 echo "$MYTAG (tag) -> $HASH1 (commit)"
357 } | sort >expected &&
358 git replace --format=long | sort >actual &&
359 test_cmp expected actual
362 test_expect_success 'setup fake editors' '
363 write_script fakeeditor <<-\EOF &&
364 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
365 mv "$1.new" "$1"
367 write_script failingfakeeditor <<-\EOF
368 ./fakeeditor "$@"
369 false
373 test_expect_success '--edit with and without already replaced object' '
374 test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
375 GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
376 git replace -l | grep "$PARA3" &&
377 git cat-file commit "$PARA3" | grep "A fake Thor" &&
378 git replace -d "$PARA3" &&
379 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
380 git replace -l | grep "$PARA3" &&
381 git cat-file commit "$PARA3" | grep "A fake Thor"
384 test_expect_success '--edit and change nothing or command failed' '
385 git replace -d "$PARA3" &&
386 test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
387 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
388 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
389 git replace -l | grep "$PARA3" &&
390 git cat-file commit "$PARA3" | grep "A fake Thor"
393 test_expect_success 'replace ref cleanup' '
394 test -n "$(git replace)" &&
395 git replace -d $(git replace) &&
396 test -z "$(git replace)"
399 test_expect_success '--graft with and without already replaced object' '
400 git log --oneline >log &&
401 test_line_count = 7 log &&
402 git replace --graft $HASH5 &&
403 git log --oneline >log &&
404 test_line_count = 3 log &&
405 commit_has_parents $HASH5 &&
406 test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
407 git replace --force -g $HASH5 $HASH4 $HASH3 &&
408 commit_has_parents $HASH5 $HASH4 $HASH3 &&
409 git replace -d $HASH5
412 test_expect_success '--graft using a tag as the new parent' '
413 git tag new_parent $HASH5 &&
414 git replace --graft $HASH7 new_parent &&
415 commit_has_parents $HASH7 $HASH5 &&
416 git replace -d $HASH7 &&
417 git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
418 git replace --graft $HASH7 annotated_new_parent &&
419 commit_has_parents $HASH7 $HASH5 &&
420 git replace -d $HASH7
423 test_expect_success '--graft using a tag as the replaced object' '
424 git tag replaced_object $HASH7 &&
425 git replace --graft replaced_object $HASH5 &&
426 commit_has_parents $HASH7 $HASH5 &&
427 git replace -d $HASH7 &&
428 git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
429 git replace --graft annotated_replaced_object $HASH5 &&
430 commit_has_parents $HASH7 $HASH5 &&
431 git replace -d $HASH7
434 test_expect_success GPG 'set up a signed commit' '
435 echo "line 17" >>hello &&
436 echo "line 18" >>hello &&
437 git add hello &&
438 test_tick &&
439 git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
440 HASH8=$(git rev-parse --verify HEAD) &&
441 git verify-commit $HASH8
444 test_expect_success GPG '--graft with a signed commit' '
445 git cat-file commit $HASH8 >orig &&
446 git replace --graft $HASH8 &&
447 git cat-file commit $HASH8 >repl &&
448 commit_has_parents $HASH8 &&
449 test_must_fail git verify-commit $HASH8 &&
450 sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
451 echo >>expected &&
452 sed -e "/^$/q" repl >actual &&
453 test_cmp expected actual &&
454 git replace -d $HASH8
457 test_expect_success GPG 'set up a merge commit with a mergetag' '
458 git reset --hard HEAD &&
459 git checkout -b test_branch HEAD~2 &&
460 echo "line 1 from test branch" >>hello &&
461 echo "line 2 from test branch" >>hello &&
462 git add hello &&
463 test_tick &&
464 git commit -m "hello: 2 more lines from a test branch" &&
465 HASH9=$(git rev-parse --verify HEAD) &&
466 git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
467 git checkout main &&
468 git merge -s ours test_tag &&
469 HASH10=$(git rev-parse --verify HEAD) &&
470 git cat-file commit $HASH10 | grep "^mergetag object"
473 test_expect_success GPG '--graft on a commit with a mergetag' '
474 test_must_fail git replace --graft $HASH10 $HASH8^1 &&
475 git replace --graft $HASH10 $HASH8^1 $HASH9 &&
476 git replace -d $HASH10
479 test_expect_success '--convert-graft-file' '
480 git checkout -b with-graft-file &&
481 test_commit root2 &&
482 git reset --hard root2^ &&
483 test_commit root1 &&
484 test_commit after-root1 &&
485 test_tick &&
486 git merge -m merge-root2 root2 &&
488 : add and convert graft file &&
489 printf "%s\n%s %s\n\n# comment\n%s\n" \
490 $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
491 >.git/info/grafts &&
492 git status 2>stderr &&
493 test_i18ngrep "hint:.*grafts is deprecated" stderr &&
494 git replace --convert-graft-file 2>stderr &&
495 test_i18ngrep ! "hint:.*grafts is deprecated" stderr &&
496 test_path_is_missing .git/info/grafts &&
498 : verify that the history is now "grafted" &&
499 git rev-list HEAD >out &&
500 test_line_count = 4 out &&
502 : create invalid graft file and verify that it is not deleted &&
503 test_when_finished "rm -f .git/info/grafts" &&
504 echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
505 test_must_fail git replace --convert-graft-file 2>err &&
506 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
507 test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
510 test_done