Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Christian Couder
4 #
5 test_description='Tests replace refs functionality'
6
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY/lib-gpg.sh"
12
13 add_and_commit_file ()
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
23 commit_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
35 commit_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 &&
46 test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
47 test_grep "Needed a single revision" err
48 }
49
50 commit_has_parents ()
51 {
52 commit_buffer_contains_parents "$@" &&
53 commit_peeling_shows_parents "$@"
54 }
55
56 HASH1=
57 HASH2=
58 HASH3=
59 HASH4=
60 HASH5=
61 HASH6=
62 HASH7=
63
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)
97 '
98
99 test_expect_success 'replace the author' '
100 git cat-file commit $HASH2 >actual &&
101 test_grep "author A U Thor" actual &&
102 R=$(sed -e "s/A U/O/" actual | git hash-object -t commit --stdin -w) &&
103 git cat-file commit $R >actual &&
104 test_grep "author O Thor" actual &&
105 git update-ref refs/replace/$HASH2 $R &&
106 git show HEAD~5 >actual &&
107 test_grep "O Thor" actual &&
108 git show $HASH2 >actual &&
109 test_grep "O Thor" actual
110 '
111
112 test_expect_success 'test --no-replace-objects option' '
113 git cat-file commit $HASH2 >actual &&
114 test_grep "author O Thor" actual &&
115 git --no-replace-objects cat-file commit $HASH2 >actual &&
116 test_grep "author A U Thor" actual &&
117 git show $HASH2 >actual &&
118 test_grep "O Thor" actual &&
119 git --no-replace-objects show $HASH2 >actual &&
120 test_grep "A U Thor" actual
121 '
122
123 test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
124 GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 >actual &&
125 test_grep "author A U Thor" actual &&
126 GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 >actual &&
127 test_grep "A U Thor" actual
128 '
129
130 test_expect_success 'test core.usereplacerefs config option' '
131 test_config core.usereplacerefs false &&
132 git cat-file commit $HASH2 >actual &&
133 test_grep "author A U Thor" actual &&
134 git show $HASH2 >actual &&
135 test_grep "A U Thor" actual
136 '
137
138 cat >tag.sig <<EOF
139 object $HASH2
140 type commit
141 tag mytag
142 tagger T A Gger <> 0 +0000
143
144 EOF
145
146 test_expect_success 'tag replaced commit' '
147 git update-ref refs/tags/mytag $(git mktag <tag.sig)
148 '
149
150 test_expect_success '"git fsck" works' '
151 git fsck main >fsck_main.out &&
152 test_grep "dangling commit $R" fsck_main.out &&
153 test_grep "dangling tag $(git show-ref -s refs/tags/mytag)" fsck_main.out &&
154 test -z "$(git fsck)"
155 '
156
157 test_expect_success 'repack, clone and fetch work' '
158 git repack -a -d &&
159 git clone --no-hardlinks . clone_dir &&
160 (
161 cd clone_dir &&
162 git show HEAD~5 >actual &&
163 test_grep "A U Thor" actual &&
164 git show $HASH2 >actual &&
165 test_grep "A U Thor" actual &&
166 git cat-file commit $R &&
167 git repack -a -d &&
168 test_must_fail git cat-file commit $R &&
169 git fetch ../ "refs/replace/*:refs/replace/*" &&
170 git show HEAD~5 >actual &&
171 test_grep "O Thor" actual &&
172 git show $HASH2 >actual &&
173 test_grep "O Thor" actual &&
174 git cat-file commit $R
175 )
176 '
177
178 test_expect_success '"git replace" listing and deleting' '
179 test "$HASH2" = "$(git replace -l)" &&
180 test "$HASH2" = "$(git replace)" &&
181 aa=${HASH2%??????????????????????????????????????} &&
182 test "$HASH2" = "$(git replace --list "$aa*")" &&
183 test_must_fail git replace -d $R &&
184 test_must_fail git replace --delete &&
185 test_must_fail git replace -l -d $HASH2 &&
186 git replace -d $HASH2 &&
187 git show $HASH2 >actual &&
188 test_grep "A U Thor" actual &&
189 test -z "$(git replace -l)"
190 '
191
192 test_expect_success '"git replace" replacing' '
193 git replace $HASH2 $R &&
194 git show $HASH2 >actual &&
195 test_grep "O Thor" actual &&
196 test_must_fail git replace $HASH2 $R &&
197 git replace -f $HASH2 $R &&
198 test_must_fail git replace -f &&
199 test "$HASH2" = "$(git replace)"
200 '
201
202 test_expect_success '"git replace" resolves sha1' '
203 SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
204 git replace -d $SHORTHASH2 &&
205 git replace $SHORTHASH2 $R &&
206 git show $HASH2 >actual &&
207 test_grep "O Thor" actual &&
208 test_must_fail git replace $HASH2 $R &&
209 git replace -f $HASH2 $R &&
210 test_must_fail git replace --force &&
211 test "$HASH2" = "$(git replace)"
212 '
213
214 # This creates a side branch where the bug in H2
215 # does not appear because P2 is created by applying
216 # H2 and squashing H5 into it.
217 # P3, P4 and P6 are created by cherry-picking H3, H4
218 # and H6 respectively.
219 #
220 # At this point, we should have the following:
221 #
222 # P2--P3--P4--P6
223 # /
224 # H1-H2-H3-H4-H5-H6-H7
225 #
226 # Then we replace H6 with P6.
227 #
228 test_expect_success 'create parallel branch without the bug' '
229 git replace -d $HASH2 &&
230 git show $HASH2 >actual &&
231 test_grep "A U Thor" actual &&
232 git checkout $HASH1 &&
233 git cherry-pick $HASH2 &&
234 git show $HASH5 >actual &&
235 git apply actual &&
236 git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
237 PARA2=$(git rev-parse --verify HEAD) &&
238 git cherry-pick $HASH3 &&
239 PARA3=$(git rev-parse --verify HEAD) &&
240 git cherry-pick $HASH4 &&
241 PARA4=$(git rev-parse --verify HEAD) &&
242 git cherry-pick $HASH6 &&
243 PARA6=$(git rev-parse --verify HEAD) &&
244 git replace $HASH6 $PARA6 &&
245 git checkout main &&
246 cur=$(git rev-parse --verify HEAD) &&
247 test "$cur" = "$HASH7" &&
248 git log --pretty=oneline >actual &&
249 test_grep $PARA2 actual &&
250 git remote add cloned ./clone_dir
251 '
252
253 test_expect_success 'push to cloned repo' '
254 git push cloned $HASH6^:refs/heads/parallel &&
255 (
256 cd clone_dir &&
257 git checkout parallel &&
258 git log --pretty=oneline >actual &&
259 test_grep $PARA2 actual
260 )
261 '
262
263 test_expect_success 'push branch with replacement' '
264 git cat-file commit $PARA3 >actual &&
265 test_grep "author A U Thor" actual &&
266 S=$(sed -e "s/A U/O/" actual | git hash-object -t commit --stdin -w) &&
267 git cat-file commit $S >actual &&
268 test_grep "author O Thor" actual &&
269 git replace $PARA3 $S &&
270 git show $HASH6~2 >actual &&
271 test_grep "O Thor" actual &&
272 git show $PARA3 >actual &&
273 test_grep "O Thor" actual &&
274 git push cloned $HASH6^:refs/heads/parallel2 &&
275 (
276 cd clone_dir &&
277 git checkout parallel2 &&
278 git log --pretty=oneline >actual &&
279 test_grep $PARA3 actual &&
280 git show $PARA3 >actual &&
281 test_grep "A U Thor" actual
282 )
283 '
284
285 test_expect_success 'fetch branch with replacement' '
286 git branch tofetch $HASH6 &&
287 (
288 cd clone_dir &&
289 git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
290 git log --pretty=oneline parallel3 >output.txt &&
291 test_grep ! $PARA3 output.txt &&
292 git show $PARA3 >para3.txt &&
293 test_grep "A U Thor" para3.txt &&
294 git fetch origin "refs/replace/*:refs/replace/*" &&
295 git log --pretty=oneline parallel3 >output.txt &&
296 test_grep $PARA3 output.txt &&
297 git show $PARA3 >para3.txt &&
298 test_grep "O Thor" para3.txt
299 )
300 '
301
302 test_expect_success 'bisect and replacements' '
303 git bisect start $HASH7 $HASH1 &&
304 test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
305 git bisect reset &&
306 GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
307 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
308 git bisect reset &&
309 git --no-replace-objects bisect start $HASH7 $HASH1 &&
310 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
311 git bisect reset
312 '
313
314 test_expect_success 'index-pack and replacements' '
315 git --no-replace-objects rev-list --objects HEAD >actual &&
316 git --no-replace-objects pack-objects test- <actual &&
317 git index-pack test-*.pack
318 '
319
320 test_expect_success 'not just commits' '
321 echo replaced >file &&
322 git add file &&
323 REPLACED=$(git rev-parse :file) &&
324 mv file file.replaced &&
325
326 echo original >file &&
327 git add file &&
328 ORIGINAL=$(git rev-parse :file) &&
329 git update-ref refs/replace/$ORIGINAL $REPLACED &&
330 mv file file.original &&
331
332 git checkout file &&
333 test_cmp file.replaced file
334 '
335
336 test_expect_success 'replaced and replacement objects must be of the same type' '
337 test_must_fail git replace mytag $HASH1 &&
338 test_must_fail git replace HEAD^{tree} HEAD~1 &&
339 BLOB=$(git rev-parse :file) &&
340 test_must_fail git replace HEAD^ $BLOB
341 '
342
343 test_expect_success '-f option bypasses the type check' '
344 git replace -f mytag $HASH1 &&
345 git replace --force HEAD^{tree} HEAD~1 &&
346 git replace -f HEAD^ $BLOB
347 '
348
349 test_expect_success 'git cat-file --batch works on replace objects' '
350 git replace >actual &&
351 test_grep $PARA3 actual &&
352 echo $PARA3 | git cat-file --batch
353 '
354
355 test_expect_success 'test --format bogus' '
356 test_must_fail git replace --format bogus >/dev/null 2>&1
357 '
358
359 test_expect_success 'test --format short' '
360 git replace --format=short >actual &&
361 git replace >expected &&
362 test_cmp expected actual
363 '
364
365 test_expect_success 'test --format medium' '
366 H1=$(git --no-replace-objects rev-parse HEAD~1) &&
367 HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
368 MYTAG=$(git --no-replace-objects rev-parse mytag) &&
369 {
370 echo "$H1 -> $BLOB" &&
371 echo "$BLOB -> $REPLACED" &&
372 echo "$HT -> $H1" &&
373 echo "$PARA3 -> $S" &&
374 echo "$MYTAG -> $HASH1"
375 } | sort >expected &&
376 git replace -l --format medium >output &&
377 sort output >actual &&
378 test_cmp expected actual
379 '
380
381 test_expect_success 'test --format long' '
382 {
383 echo "$H1 (commit) -> $BLOB (blob)" &&
384 echo "$BLOB (blob) -> $REPLACED (blob)" &&
385 echo "$HT (tree) -> $H1 (commit)" &&
386 echo "$PARA3 (commit) -> $S (commit)" &&
387 echo "$MYTAG (tag) -> $HASH1 (commit)"
388 } | sort >expected &&
389 git replace --format=long >output &&
390 sort output >actual &&
391 test_cmp expected actual
392 '
393
394 test_expect_success 'setup fake editors' '
395 write_script fakeeditor <<-\EOF &&
396 sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
397 mv "$1.new" "$1"
398 EOF
399 write_script failingfakeeditor <<-\EOF
400 ./fakeeditor "$@"
401 false
402 EOF
403 '
404
405 test_expect_success '--edit with and without already replaced object' '
406 test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
407 GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
408 git replace -l >actual &&
409 test_grep "$PARA3" actual &&
410 git cat-file commit "$PARA3" >actual &&
411 test_grep "A fake Thor" actual &&
412 git replace -d "$PARA3" &&
413 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
414 git replace -l >actual &&
415 test_grep "$PARA3" actual &&
416 git cat-file commit "$PARA3" >actual &&
417 test_grep "A fake Thor" actual
418 '
419
420 test_expect_success '--edit and change nothing or command failed' '
421 git replace -d "$PARA3" &&
422 test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
423 test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
424 GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
425 git replace -l >actual &&
426 test_grep "$PARA3" actual &&
427 git cat-file commit "$PARA3" >actual &&
428 test_grep "A fake Thor" actual
429 '
430
431 test_expect_success 'replace ref cleanup' '
432 test -n "$(git replace)" &&
433 git replace -d $(git replace) &&
434 test -z "$(git replace)"
435 '
436
437 test_expect_success '--graft with and without already replaced object' '
438 git log --oneline >log &&
439 test_line_count = 7 log &&
440 git replace --graft $HASH5 &&
441 git log --oneline >log &&
442 test_line_count = 3 log &&
443 commit_has_parents $HASH5 &&
444 test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
445 git replace --force -g $HASH5 $HASH4 $HASH3 &&
446 commit_has_parents $HASH5 $HASH4 $HASH3 &&
447 git replace -d $HASH5
448 '
449
450 test_expect_success '--graft using a tag as the new parent' '
451 git tag new_parent $HASH5 &&
452 git replace --graft $HASH7 new_parent &&
453 commit_has_parents $HASH7 $HASH5 &&
454 git replace -d $HASH7 &&
455 git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
456 git replace --graft $HASH7 annotated_new_parent &&
457 commit_has_parents $HASH7 $HASH5 &&
458 git replace -d $HASH7
459 '
460
461 test_expect_success '--graft using a tag as the replaced object' '
462 git tag replaced_object $HASH7 &&
463 git replace --graft replaced_object $HASH5 &&
464 commit_has_parents $HASH7 $HASH5 &&
465 git replace -d $HASH7 &&
466 git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
467 git replace --graft annotated_replaced_object $HASH5 &&
468 commit_has_parents $HASH7 $HASH5 &&
469 git replace -d $HASH7
470 '
471
472 test_expect_success GPG 'set up a signed commit' '
473 echo "line 17" >>hello &&
474 echo "line 18" >>hello &&
475 git add hello &&
476 test_tick &&
477 git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
478 HASH8=$(git rev-parse --verify HEAD) &&
479 git verify-commit $HASH8
480 '
481
482 test_expect_success GPG '--graft with a signed commit' '
483 git cat-file commit $HASH8 >orig &&
484 git replace --graft $HASH8 &&
485 git cat-file commit $HASH8 >repl &&
486 commit_has_parents $HASH8 &&
487 test_must_fail git verify-commit $HASH8 &&
488 sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
489 echo >>expected &&
490 sed -e "/^$/q" repl >actual &&
491 test_cmp expected actual &&
492 git replace -d $HASH8
493 '
494
495 test_expect_success GPG 'set up a merge commit with a mergetag' '
496 git reset --hard HEAD &&
497 git checkout -b test_branch HEAD~2 &&
498 echo "line 1 from test branch" >>hello &&
499 echo "line 2 from test branch" >>hello &&
500 git add hello &&
501 test_tick &&
502 git commit -m "hello: 2 more lines from a test branch" &&
503 HASH9=$(git rev-parse --verify HEAD) &&
504 git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
505 git checkout main &&
506 git merge -s ours test_tag &&
507 HASH10=$(git rev-parse --verify HEAD) &&
508 git cat-file commit $HASH10 >actual &&
509 test_grep "^mergetag object" actual
510 '
511
512 test_expect_success GPG '--graft on a commit with a mergetag' '
513 test_must_fail git replace --graft $HASH10 $HASH8^1 &&
514 git replace --graft $HASH10 $HASH8^1 $HASH9 &&
515 git replace -d $HASH10
516 '
517
518 test_expect_success '--convert-graft-file' '
519 git checkout -b with-graft-file &&
520 test_commit root2 &&
521 git reset --hard root2^ &&
522 test_commit root1 &&
523 test_commit after-root1 &&
524 test_tick &&
525 git merge -m merge-root2 root2 &&
526
527 : add and convert graft file &&
528 printf "%s\n%s %s\n\n# comment\n%s\n" \
529 $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
530 >.git/info/grafts &&
531 git status 2>stderr &&
532 test_grep "hint:.*grafts is deprecated" stderr &&
533 git replace --convert-graft-file 2>stderr &&
534 test_grep ! "hint:.*grafts is deprecated" stderr &&
535 test_path_is_missing .git/info/grafts &&
536
537 : verify that the history is now "grafted" &&
538 git rev-list HEAD >out &&
539 test_line_count = 4 out &&
540
541 : create invalid graft file and verify that it is not deleted &&
542 test_when_finished "rm -f .git/info/grafts" &&
543 echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
544 test_must_fail git replace --convert-graft-file 2>err &&
545 test_grep "$EMPTY_BLOB $EMPTY_TREE" err &&
546 test_grep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
547 '
548
549 test_done