summaryrefslogtreecommitdiff
path: root/t/t6050-replace.sh
blob: 0a53462a614f66c29ec861aa30521f3ed9a30707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/bin/sh
#
# Copyright (c) 2008 Christian Couder
#
test_description='Tests replace refs functionality'

GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME

. ./test-lib.sh
. "$TEST_DIRECTORY/lib-gpg.sh"

add_and_commit_file ()
{
    _file="$1"
    _msg="$2"

    git add $_file || return $?
    test_tick || return $?
    git commit --quiet -m "$_file: $_msg"
}

commit_buffer_contains_parents ()
{
    git cat-file commit "$1" >payload &&
    sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
    shift &&
    for _parent
    do
	echo "parent $_parent"
    done >expected &&
    test_cmp expected actual
}

commit_peeling_shows_parents ()
{
    _parent_number=1
    _commit="$1"
    shift &&
    for _parent
    do
	_found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
	test "$_found" = "$_parent" || return 1
	_parent_number=$(( $_parent_number + 1 ))
    done &&
    test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
    test_i18ngrep "Needed a single revision" err
}

commit_has_parents ()
{
    commit_buffer_contains_parents "$@" &&
    commit_peeling_shows_parents "$@"
}

HASH1=
HASH2=
HASH3=
HASH4=
HASH5=
HASH6=
HASH7=

test_expect_success 'set up buggy branch' '
     echo "line 1" >>hello &&
     echo "line 2" >>hello &&
     echo "line 3" >>hello &&
     echo "line 4" >>hello &&
     add_and_commit_file hello "4 lines" &&
     HASH1=$(git rev-parse --verify HEAD) &&
     echo "line BUG" >>hello &&
     echo "line 6" >>hello &&
     echo "line 7" >>hello &&
     echo "line 8" >>hello &&
     add_and_commit_file hello "4 more lines with a BUG" &&
     HASH2=$(git rev-parse --verify HEAD) &&
     echo "line 9" >>hello &&
     echo "line 10" >>hello &&
     add_and_commit_file hello "2 more lines" &&
     HASH3=$(git rev-parse --verify HEAD) &&
     echo "line 11" >>hello &&
     add_and_commit_file hello "1 more line" &&
     HASH4=$(git rev-parse --verify HEAD) &&
     sed -e "s/BUG/5/" hello >hello.new &&
     mv hello.new hello &&
     add_and_commit_file hello "BUG fixed" &&
     HASH5=$(git rev-parse --verify HEAD) &&
     echo "line 12" >>hello &&
     echo "line 13" >>hello &&
     add_and_commit_file hello "2 more lines" &&
     HASH6=$(git rev-parse --verify HEAD) &&
     echo "line 14" >>hello &&
     echo "line 15" >>hello &&
     echo "line 16" >>hello &&
     add_and_commit_file hello "again 3 more lines" &&
     HASH7=$(git rev-parse --verify HEAD)
'

test_expect_success 'replace the author' '
     git cat-file commit $HASH2 | grep "author A U Thor" &&
     R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
     git cat-file commit $R | grep "author O Thor" &&
     git update-ref refs/replace/$HASH2 $R &&
     git show HEAD~5 | grep "O Thor" &&
     git show $HASH2 | grep "O Thor"
'

test_expect_success 'test --no-replace-objects option' '
     git cat-file commit $HASH2 | grep "author O Thor" &&
     git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
     git show $HASH2 | grep "O Thor" &&
     git --no-replace-objects show $HASH2 | grep "A U Thor"
'

test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
     GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
     GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
'

test_expect_success 'test core.usereplacerefs config option' '
	test_config core.usereplacerefs false &&
	git cat-file commit $HASH2 | grep "author A U Thor" &&
	git show $HASH2 | grep "A U Thor"
'

cat >tag.sig <<EOF
object $HASH2
type commit
tag mytag
tagger T A Gger <> 0 +0000

EOF

test_expect_success 'tag replaced commit' '
     git mktag <tag.sig >.git/refs/tags/mytag 2>message
'

test_expect_success '"git fsck" works' '
     git fsck master >fsck_master.out &&
     test_i18ngrep "dangling commit $R" fsck_master.out &&
     test_i18ngrep "dangling tag $(git show-ref -s refs/tags/mytag)" fsck_master.out &&
     test -z "$(git fsck)"
'

test_expect_success 'repack, clone and fetch work' '
     git repack -a -d &&
     git clone --no-hardlinks . clone_dir &&
     (
	  cd clone_dir &&
	  git show HEAD~5 | grep "A U Thor" &&
	  git show $HASH2 | grep "A U Thor" &&
	  git cat-file commit $R &&
	  git repack -a -d &&
	  test_must_fail git cat-file commit $R &&
	  git fetch ../ "refs/replace/*:refs/replace/*" &&
	  git show HEAD~5 | grep "O Thor" &&
	  git show $HASH2 | grep "O Thor" &&
	  git cat-file commit $R
     )
'

test_expect_success '"git replace" listing and deleting' '
     test "$HASH2" = "$(git replace -l)" &&
     test "$HASH2" = "$(git replace)" &&
     aa=${HASH2%??????????????????????????????????????} &&
     test "$HASH2" = "$(git replace --list "$aa*")" &&
     test_must_fail git replace -d $R &&
     test_must_fail git replace --delete &&
     test_must_fail git replace -l -d $HASH2 &&
     git replace -d $HASH2 &&
     git show $HASH2 | grep "A U Thor" &&
     test -z "$(git replace -l)"
'

test_expect_success '"git replace" replacing' '
     git replace $HASH2 $R &&
     git show $HASH2 | grep "O Thor" &&
     test_must_fail git replace $HASH2 $R &&
     git replace -f $HASH2 $R &&
     test_must_fail git replace -f &&
     test "$HASH2" = "$(git replace)"
'

test_expect_success '"git replace" resolves sha1' '
     SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
     git replace -d $SHORTHASH2 &&
     git replace $SHORTHASH2 $R &&
     git show $HASH2 | grep "O Thor" &&
     test_must_fail git replace $HASH2 $R &&
     git replace -f $HASH2 $R &&
     test_must_fail git replace --force &&
     test "$HASH2" = "$(git replace)"
'

# This creates a side branch where the bug in H2
# does not appear because P2 is created by applying
# H2 and squashing H5 into it.
# P3, P4 and P6 are created by cherry-picking H3, H4
# and H6 respectively.
#
# At this point, we should have the following:
#
#    P2--P3--P4--P6
#   /
# H1-H2-H3-H4-H5-H6-H7
#
# Then we replace H6 with P6.
#
test_expect_success 'create parallel branch without the bug' '
     git replace -d $HASH2 &&
     git show $HASH2 | grep "A U Thor" &&
     git checkout $HASH1 &&
     git cherry-pick $HASH2 &&
     git show $HASH5 | git apply &&
     git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
     PARA2=$(git rev-parse --verify HEAD) &&
     git cherry-pick $HASH3 &&
     PARA3=$(git rev-parse --verify HEAD) &&
     git cherry-pick $HASH4 &&
     PARA4=$(git rev-parse --verify HEAD) &&
     git cherry-pick $HASH6 &&
     PARA6=$(git rev-parse --verify HEAD) &&
     git replace $HASH6 $PARA6 &&
     git checkout master &&
     cur=$(git rev-parse --verify HEAD) &&
     test "$cur" = "$HASH7" &&
     git log --pretty=oneline | grep $PARA2 &&
     git remote add cloned ./clone_dir
'

test_expect_success 'push to cloned repo' '
     git push cloned $HASH6^:refs/heads/parallel &&
     (
	  cd clone_dir &&
	  git checkout parallel &&
	  git log --pretty=oneline | grep $PARA2
     )
'

test_expect_success 'push branch with replacement' '
     git cat-file commit $PARA3 | grep "author A U Thor" &&
     S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
     git cat-file commit $S | grep "author O Thor" &&
     git replace $PARA3 $S &&
     git show $HASH6~2 | grep "O Thor" &&
     git show $PARA3 | grep "O Thor" &&
     git push cloned $HASH6^:refs/heads/parallel2 &&
     (
	  cd clone_dir &&
	  git checkout parallel2 &&
	  git log --pretty=oneline | grep $PARA3 &&
	  git show $PARA3 | grep "A U Thor"
     )
'

test_expect_success 'fetch branch with replacement' '
     git branch tofetch $HASH6 &&
     (
	  cd clone_dir &&
	  git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
	  git log --pretty=oneline parallel3 >output.txt &&
	  ! grep $PARA3 output.txt &&
	  git show $PARA3 >para3.txt &&
	  grep "A U Thor" para3.txt &&
	  git fetch origin "refs/replace/*:refs/replace/*" &&
	  git log --pretty=oneline parallel3 >output.txt &&
	  grep $PARA3 output.txt &&
	  git show $PARA3 >para3.txt &&
	  grep "O Thor" para3.txt
     )
'

test_expect_success 'bisect and replacements' '
     git bisect start $HASH7 $HASH1 &&
     test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
     git bisect reset &&
     GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
     git bisect reset &&
     git --no-replace-objects bisect start $HASH7 $HASH1 &&
     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
     git bisect reset
'

test_expect_success 'index-pack and replacements' '
	git --no-replace-objects rev-list --objects HEAD |
	git --no-replace-objects pack-objects test- &&
	git index-pack test-*.pack
'

test_expect_success 'not just commits' '
	echo replaced >file &&
	git add file &&
	REPLACED=$(git rev-parse :file) &&
	mv file file.replaced &&

	echo original >file &&
	git add file &&
	ORIGINAL=$(git rev-parse :file) &&
	git update-ref refs/replace/$ORIGINAL $REPLACED &&
	mv file file.original &&

	git checkout file &&
	test_cmp file.replaced file
'

test_expect_success 'replaced and replacement objects must be of the same type' '
	test_must_fail git replace mytag $HASH1 &&
	test_must_fail git replace HEAD^{tree} HEAD~1 &&
	BLOB=$(git rev-parse :file) &&
	test_must_fail git replace HEAD^ $BLOB
'

test_expect_success '-f option bypasses the type check' '
	git replace -f mytag $HASH1 &&
	git replace --force HEAD^{tree} HEAD~1 &&
	git replace -f HEAD^ $BLOB
'

test_expect_success 'git cat-file --batch works on replace objects' '
	git replace | grep $PARA3 &&
	echo $PARA3 | git cat-file --batch
'

test_expect_success 'test --format bogus' '
	test_must_fail git replace --format bogus >/dev/null 2>&1
'

test_expect_success 'test --format short' '
	git replace --format=short >actual &&
	git replace >expected &&
	test_cmp expected actual
'

test_expect_success 'test --format medium' '
	H1=$(git --no-replace-objects rev-parse HEAD~1) &&
	HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
	MYTAG=$(git --no-replace-objects rev-parse mytag) &&
	{
		echo "$H1 -> $BLOB" &&
		echo "$BLOB -> $REPLACED" &&
		echo "$HT -> $H1" &&
		echo "$PARA3 -> $S" &&
		echo "$MYTAG -> $HASH1"
	} | sort >expected &&
	git replace -l --format medium | sort >actual &&
	test_cmp expected actual
'

test_expect_success 'test --format long' '
	{
		echo "$H1 (commit) -> $BLOB (blob)" &&
		echo "$BLOB (blob) -> $REPLACED (blob)" &&
		echo "$HT (tree) -> $H1 (commit)" &&
		echo "$PARA3 (commit) -> $S (commit)" &&
		echo "$MYTAG (tag) -> $HASH1 (commit)"
	} | sort >expected &&
	git replace --format=long | sort >actual &&
	test_cmp expected actual
'

test_expect_success 'setup fake editors' '
	write_script fakeeditor <<-\EOF &&
		sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
		mv "$1.new" "$1"
	EOF
	write_script failingfakeeditor <<-\EOF
		./fakeeditor "$@"
		false
	EOF
'

test_expect_success '--edit with and without already replaced object' '
	test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
	GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
	git replace -l | grep "$PARA3" &&
	git cat-file commit "$PARA3" | grep "A fake Thor" &&
	git replace -d "$PARA3" &&
	GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
	git replace -l | grep "$PARA3" &&
	git cat-file commit "$PARA3" | grep "A fake Thor"
'

test_expect_success '--edit and change nothing or command failed' '
	git replace -d "$PARA3" &&
	test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
	test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
	GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
	git replace -l | grep "$PARA3" &&
	git cat-file commit "$PARA3" | grep "A fake Thor"
'

test_expect_success 'replace ref cleanup' '
	test -n "$(git replace)" &&
	git replace -d $(git replace) &&
	test -z "$(git replace)"
'

test_expect_success '--graft with and without already replaced object' '
	git log --oneline >log &&
	test_line_count = 7 log &&
	git replace --graft $HASH5 &&
	git log --oneline >log &&
	test_line_count = 3 log &&
	commit_has_parents $HASH5 &&
	test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
	git replace --force -g $HASH5 $HASH4 $HASH3 &&
	commit_has_parents $HASH5 $HASH4 $HASH3 &&
	git replace -d $HASH5
'

test_expect_success '--graft using a tag as the new parent' '
	git tag new_parent $HASH5 &&
	git replace --graft $HASH7 new_parent &&
	commit_has_parents $HASH7 $HASH5 &&
	git replace -d $HASH7 &&
	git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
	git replace --graft $HASH7 annotated_new_parent &&
	commit_has_parents $HASH7 $HASH5 &&
	git replace -d $HASH7
'

test_expect_success '--graft using a tag as the replaced object' '
	git tag replaced_object $HASH7 &&
	git replace --graft replaced_object $HASH5 &&
	commit_has_parents $HASH7 $HASH5 &&
	git replace -d $HASH7 &&
	git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
	git replace --graft annotated_replaced_object $HASH5 &&
	commit_has_parents $HASH7 $HASH5 &&
	git replace -d $HASH7
'

test_expect_success GPG 'set up a signed commit' '
	echo "line 17" >>hello &&
	echo "line 18" >>hello &&
	git add hello &&
	test_tick &&
	git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
	HASH8=$(git rev-parse --verify HEAD) &&
	git verify-commit $HASH8
'

test_expect_success GPG '--graft with a signed commit' '
	git cat-file commit $HASH8 >orig &&
	git replace --graft $HASH8 &&
	git cat-file commit $HASH8 >repl &&
	commit_has_parents $HASH8 &&
	test_must_fail git verify-commit $HASH8 &&
	sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
	echo >>expected &&
	sed -e "/^$/q" repl >actual &&
	test_cmp expected actual &&
	git replace -d $HASH8
'

test_expect_success GPG 'set up a merge commit with a mergetag' '
	git reset --hard HEAD &&
	git checkout -b test_branch HEAD~2 &&
	echo "line 1 from test branch" >>hello &&
	echo "line 2 from test branch" >>hello &&
	git add hello &&
	test_tick &&
	git commit -m "hello: 2 more lines from a test branch" &&
	HASH9=$(git rev-parse --verify HEAD) &&
	git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
	git checkout master &&
	git merge -s ours test_tag &&
	HASH10=$(git rev-parse --verify HEAD) &&
	git cat-file commit $HASH10 | grep "^mergetag object"
'

test_expect_success GPG '--graft on a commit with a mergetag' '
	test_must_fail git replace --graft $HASH10 $HASH8^1 &&
	git replace --graft $HASH10 $HASH8^1 $HASH9 &&
	git replace -d $HASH10
'

test_expect_success '--convert-graft-file' '
	git checkout -b with-graft-file &&
	test_commit root2 &&
	git reset --hard root2^ &&
	test_commit root1 &&
	test_commit after-root1 &&
	test_tick &&
	git merge -m merge-root2 root2 &&

	: add and convert graft file &&
	printf "%s\n%s %s\n\n# comment\n%s\n" \
		$(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
		>.git/info/grafts &&
	git status 2>stderr &&
	test_i18ngrep "hint:.*grafts is deprecated" stderr &&
	git replace --convert-graft-file 2>stderr &&
	test_i18ngrep ! "hint:.*grafts is deprecated" stderr &&
	test_path_is_missing .git/info/grafts &&

	: verify that the history is now "grafted" &&
	git rev-list HEAD >out &&
	test_line_count = 4 out &&

	: create invalid graft file and verify that it is not deleted &&
	test_when_finished "rm -f .git/info/grafts" &&
	echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
	test_must_fail git replace --convert-graft-file 2>err &&
	test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
	test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
'

test_done