]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6050-replace.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t6050-replace.sh
CommitLineData
a3e82672
CC
1#!/bin/sh
2#
3# Copyright (c) 2008 Christian Couder
4#
5test_description='Tests replace refs functionality'
6
1550bb6e 7GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
8export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
a3e82672 10. ./test-lib.sh
60e2f5a5 11. "$TEST_DIRECTORY/lib-gpg.sh"
a3e82672 12
adf8e542 13add_and_commit_file ()
a3e82672
CC
14{
15 _file="$1"
16 _msg="$2"
17
18 git add $_file || return $?
19 test_tick || return $?
20 git commit --quiet -m "$_file: $_msg"
21}
22
adf8e542
CC
23commit_buffer_contains_parents ()
24{
25 git cat-file commit "$1" >payload &&
26 sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
27 shift &&
28 for _parent
29 do
30 echo "parent $_parent"
31 done >expected &&
32 test_cmp expected actual
33}
34
35commit_peeling_shows_parents ()
36{
37 _parent_number=1
38 _commit="$1"
39 shift &&
40 for _parent
41 do
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 &&
58761701 46 test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
6789275d 47 test_grep "Needed a single revision" err
adf8e542
CC
48}
49
50commit_has_parents ()
51{
52 commit_buffer_contains_parents "$@" &&
53 commit_peeling_shows_parents "$@"
54}
55
a3e82672
CC
56HASH1=
57HASH2=
58HASH3=
59HASH4=
60HASH5=
61HASH6=
62HASH7=
63
64test_expect_success 'set up buggy branch' '
32942346
JC
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)
a3e82672
CC
97'
98
99test_expect_success 'replace the author' '
32942346
JC
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"
a3e82672
CC
106'
107
b0fa7ab5 108test_expect_success 'test --no-replace-objects option' '
32942346
JC
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"
b0fa7ab5
CC
113'
114
6476b38b 115test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
32942346
JC
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"
6476b38b
CC
118'
119
da4398d6
JK
120test_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"
124'
125
cc400f50
CC
126cat >tag.sig <<EOF
127object $HASH2
128type commit
129tag mytag
130tagger T A Gger <> 0 +0000
131
132EOF
133
134test_expect_success 'tag replaced commit' '
32942346 135 git update-ref refs/tags/mytag $(git mktag <tag.sig)
cc400f50
CC
136'
137
dae556bd 138test_expect_success '"git fsck" works' '
32942346 139 git fsck main >fsck_main.out &&
6789275d
JH
140 test_grep "dangling commit $R" fsck_main.out &&
141 test_grep "dangling tag $(git show-ref -s refs/tags/mytag)" fsck_main.out &&
32942346 142 test -z "$(git fsck)"
dae556bd
CC
143'
144
145test_expect_success 'repack, clone and fetch work' '
32942346
JC
146 git repack -a -d &&
147 git clone --no-hardlinks . clone_dir &&
148 (
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
159 )
dae556bd
CC
160'
161
54b0c1e0 162test_expect_success '"git replace" listing and deleting' '
32942346
JC
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)"
54b0c1e0
CC
173'
174
bebdd271 175test_expect_success '"git replace" replacing' '
32942346
JC
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)"
bebdd271
CC
182'
183
9dfc3684 184test_expect_success '"git replace" resolves sha1' '
32942346
JC
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)"
9dfc3684
MG
193'
194
4e65b538
CC
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.
200#
201# At this point, we should have the following:
202#
203# P2--P3--P4--P6
204# /
205# H1-H2-H3-H4-H5-H6-H7
206#
207# Then we replace H6 with P6.
208#
209test_expect_success 'create parallel branch without the bug' '
32942346
JC
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
4e65b538
CC
229'
230
231test_expect_success 'push to cloned repo' '
32942346
JC
232 git push cloned $HASH6^:refs/heads/parallel &&
233 (
234 cd clone_dir &&
235 git checkout parallel &&
236 git log --pretty=oneline | grep $PARA2
237 )
4e65b538
CC
238'
239
240test_expect_success 'push branch with replacement' '
32942346
JC
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 &&
248 (
249 cd clone_dir &&
250 git checkout parallel2 &&
251 git log --pretty=oneline | grep $PARA3 &&
252 git show $PARA3 | grep "A U Thor"
253 )
4e65b538
CC
254'
255
256test_expect_success 'fetch branch with replacement' '
32942346
JC
257 git branch tofetch $HASH6 &&
258 (
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
270 )
4e65b538
CC
271'
272
6476b38b 273test_expect_success 'bisect and replacements' '
32942346
JC
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
6476b38b
CC
283'
284
6e2a09d2
NE
285test_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
289'
290
abb25ac3
JH
291test_expect_success 'not just commits' '
292 echo replaced >file &&
293 git add file &&
294 REPLACED=$(git rev-parse :file) &&
295 mv file file.replaced &&
296
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 &&
302
303 git checkout file &&
304 test_cmp file.replaced file
305'
306
3e625c8f
CC
307test_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
312'
313
314test_expect_success '-f option bypasses the type check' '
315 git replace -f mytag $HASH1 &&
b1ecd8cf 316 git replace --force HEAD^{tree} HEAD~1 &&
3e625c8f
CC
317 git replace -f HEAD^ $BLOB
318'
319
1f7117ef 320test_expect_success 'git cat-file --batch works on replace objects' '
303c5d65
CC
321 git replace | grep $PARA3 &&
322 echo $PARA3 | git cat-file --batch
323'
324
bbbb4afc
CC
325test_expect_success 'test --format bogus' '
326 test_must_fail git replace --format bogus >/dev/null 2>&1
327'
328
329test_expect_success 'test --format short' '
330 git replace --format=short >actual &&
331 git replace >expected &&
332 test_cmp expected actual
333'
334
335test_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) &&
339 {
340 echo "$H1 -> $BLOB" &&
341 echo "$BLOB -> $REPLACED" &&
342 echo "$HT -> $H1" &&
343 echo "$PARA3 -> $S" &&
344 echo "$MYTAG -> $HASH1"
345 } | sort >expected &&
1c2828c1 346 git replace -l --format medium | sort >actual &&
bbbb4afc
CC
347 test_cmp expected actual
348'
349
663a8566 350test_expect_success 'test --format long' '
bbbb4afc
CC
351 {
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 &&
1c2828c1 358 git replace --format=long | sort >actual &&
bbbb4afc
CC
359 test_cmp expected actual
360'
361
36fc7d8a
SG
362test_expect_success 'setup fake editors' '
363 write_script fakeeditor <<-\EOF &&
85f98fc0
CC
364 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
365 mv "$1.new" "$1"
366 EOF
36fc7d8a
SG
367 write_script failingfakeeditor <<-\EOF
368 ./fakeeditor "$@"
369 false
370 EOF
85f98fc0
CC
371'
372
373test_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"
382'
383
384test_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" &&
36fc7d8a 387 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
85f98fc0
CC
388 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
389 git replace -l | grep "$PARA3" &&
390 git cat-file commit "$PARA3" | grep "A fake Thor"
391'
392
11aec955
CC
393test_expect_success 'replace ref cleanup' '
394 test -n "$(git replace)" &&
395 git replace -d $(git replace) &&
396 test -z "$(git replace)"
397'
398
adf8e542 399test_expect_success '--graft with and without already replaced object' '
502d87b9
CC
400 git log --oneline >log &&
401 test_line_count = 7 log &&
adf8e542 402 git replace --graft $HASH5 &&
502d87b9
CC
403 git log --oneline >log &&
404 test_line_count = 3 log &&
adf8e542
CC
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
410'
411
f8e44a81
CC
412test_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
421'
422
ee521ec4
CC
423test_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
432'
433
60e2f5a5
CC
434test_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
442'
443
444test_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
455'
456
3fa10259
CC
457test_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 &&
1550bb6e 467 git checkout main &&
3fa10259
CC
468 git merge -s ours test_tag &&
469 HASH10=$(git rev-parse --verify HEAD) &&
470 git cat-file commit $HASH10 | grep "^mergetag object"
471'
472
473test_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
477'
478
0115e030
JS
479test_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 &&
487
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 &&
8821e90a 492 git status 2>stderr &&
6789275d 493 test_grep "hint:.*grafts is deprecated" stderr &&
8821e90a 494 git replace --convert-graft-file 2>stderr &&
6789275d 495 test_grep ! "hint:.*grafts is deprecated" stderr &&
0115e030
JS
496 test_path_is_missing .git/info/grafts &&
497
498 : verify that the history is now "grafted" &&
499 git rev-list HEAD >out &&
500 test_line_count = 4 out &&
501
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 &&
6789275d
JH
506 test_grep "$EMPTY_BLOB $EMPTY_TREE" err &&
507 test_grep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
0115e030
JS
508'
509
a3e82672 510test_done