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