interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3454-history-drop.sh

611 lines · 13290 bytes Raw Blame History
1#!/bin/sh
2
3test_description='tests for git-history drop subcommand'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY/lib-log-graph.sh"
7. "$TEST_DIRECTORY/lib-gpg.sh"
8
9expect_graph () {
10 cat >expect &&
11 lib_test_cmp_graph --format=%s "$@"
12}
13
14expect_log () {
15 git log --format="%s" "$@" >actual &&
16 cat >expect &&
17 test_cmp expect actual
18}
19
20test_drop_gpg_sign () {
21 must_fail= will=will
22 if test "x$1" = "x!"
23 then
24 must_fail=test_must_fail
25 will="will not"
26 shift
27 fi
28 conf=$1
29 shift
30
31 test_expect_success GPG "drop $* with commit.gpgsign=$conf $will sign replayed descendants" "
32 test_when_finished 'rm -rf repo' &&
33 git init repo &&
34 (
35 cd repo &&
36 test_commit first &&
37 test_commit second &&
38 test_commit third &&
39
40 git config commit.gpgsign $conf &&
41 git history drop $* HEAD~ &&
42
43 $must_fail git verify-commit HEAD
44 )
45 "
46}
47
48test_expect_success 'errors on missing commit argument' '
49 test_when_finished "rm -rf repo" &&
50 git init repo &&
51 (
52 cd repo &&
53 test_commit initial &&
54 test_must_fail git history drop 2>err &&
55 test_grep "command expects a single revision" err
56 )
57'
58
59test_expect_success 'errors on too many arguments' '
60 test_when_finished "rm -rf repo" &&
61 git init repo &&
62 (
63 cd repo &&
64 test_commit initial &&
65 test_must_fail git history drop HEAD HEAD 2>err &&
66 test_grep "command expects a single revision" err
67 )
68'
69
70test_expect_success 'errors on unknown revision' '
71 test_when_finished "rm -rf repo" &&
72 git init repo &&
73 (
74 cd repo &&
75 test_commit initial &&
76 test_must_fail git history drop does-not-exist 2>err &&
77 test_grep "commit cannot be found: does-not-exist" err
78 )
79'
80
81test_expect_success 'errors with invalid --empty= value' '
82 test_when_finished "rm -rf repo" &&
83 git init repo &&
84 (
85 cd repo &&
86 test_commit initial &&
87 test_commit second &&
88 test_must_fail git history drop --empty=bogus HEAD 2>err &&
89 test_grep "unrecognized.*--empty.*bogus" err
90 )
91'
92
93test_expect_success 'drops a commit in the middle and replays descendants' '
94 test_when_finished "rm -rf repo" &&
95 git init repo &&
96 (
97 cd repo &&
98 test_commit first &&
99 test_commit second &&
100 test_commit third &&
101
102 git symbolic-ref HEAD >expect &&
103 git history drop HEAD~ &&
104 git symbolic-ref HEAD >actual &&
105 test_cmp expect actual &&
106
107 expect_log <<-\EOF &&
108 third
109 first
110 EOF
111
112 test_must_fail git show HEAD:second.t &&
113 test_path_is_missing second.t &&
114
115 git reflog >reflog &&
116 test_grep "drop: dropping HEAD~" reflog
117 )
118'
119
120test_drop_gpg_sign ! false
121test_drop_gpg_sign true
122test_drop_gpg_sign false --gpg-sign
123test_drop_gpg_sign ! true --no-gpg-sign
124test_drop_gpg_sign ! true --gpg-sign --no-gpg-sign
125test_drop_gpg_sign false --no-gpg-sign --gpg-sign
126
127test_expect_success GPG 'drop has no commit to sign when dropping the tip' '
128 test_when_finished "rm -rf repo" &&
129 git init repo &&
130 (
131 cd repo &&
132 test_commit first &&
133 test_commit second &&
134
135 git history drop --gpg-sign HEAD &&
136
137 test_must_fail git verify-commit HEAD
138 )
139'
140
141test_expect_success 'drops the HEAD commit' '
142 test_when_finished "rm -rf repo" &&
143 git init repo &&
144 (
145 cd repo &&
146 test_commit first &&
147 test_commit second &&
148
149 git history drop HEAD &&
150
151 expect_log <<-\EOF
152 first
153 EOF
154 )
155'
156
157test_expect_success 'drops a commit on detached HEAD' '
158 test_when_finished "rm -rf repo" &&
159 git init repo &&
160 (
161 cd repo &&
162 test_commit first &&
163 test_commit second &&
164 test_commit third &&
165 git checkout --detach HEAD &&
166
167 git history drop HEAD~ &&
168
169 expect_log <<-\EOF
170 third
171 first
172 EOF
173 )
174'
175
176# Note: in this case it would actually be fine to drop the root commit, as we
177# do have a descendant commit, and no reference points to the root commit
178# directly. So this is something that we may relax eventually.
179test_expect_success 'refuses to drop the root commit' '
180 test_when_finished "rm -rf repo" &&
181 git init repo &&
182 (
183 cd repo &&
184 test_commit first &&
185 test_commit second &&
186
187 test_must_fail git history drop HEAD~ 2>err &&
188 test_grep "cannot drop root commit" err
189 )
190'
191
192# In contrast to the above case, we actually don't want to drop the root commit
193# here as that would cause us to end up with an empty commit graph.
194test_expect_success 'refuses to drop the root commit when branch becomes empty' '
195 test_when_finished "rm -rf repo" &&
196 git init repo &&
197 (
198 cd repo &&
199 test_commit first &&
200
201 test_must_fail git history drop HEAD 2>err &&
202 test_grep "cannot drop root commit" err
203 )
204'
205
206test_expect_success 'refuses to drop a merge commit' '
207 test_when_finished "rm -rf repo" &&
208 git init repo &&
209 (
210 cd repo &&
211 test_commit base &&
212 git branch branch &&
213 test_commit ours &&
214 git switch branch &&
215 test_commit theirs &&
216 git switch - &&
217 git merge theirs &&
218
219 test_must_fail git history drop HEAD 2>err &&
220 test_grep "cannot drop merge commit" err
221 )
222'
223
224test_expect_success 'refuses when descendants contain a merge commit' '
225 test_when_finished "rm -rf repo" &&
226 git init repo &&
227 (
228 cd repo &&
229 test_commit base &&
230 test_commit middle &&
231 git branch branch &&
232 test_commit ours &&
233 git switch branch &&
234 test_commit theirs &&
235 git switch - &&
236 git merge theirs &&
237
238 test_must_fail git history drop middle 2>err &&
239 test_grep "replaying merge commits is not supported yet" err
240 )
241'
242
243test_expect_success 'works in a bare repository' '
244 test_when_finished "rm -rf repo repo.git" &&
245
246 git init repo &&
247 test_commit -C repo first &&
248 test_commit -C repo second &&
249 test_commit -C repo third &&
250
251 git clone --bare repo repo.git &&
252 (
253 cd repo.git &&
254
255 git history drop HEAD~ &&
256 expect_log <<-\EOF
257 third
258 first
259 EOF
260 )
261'
262
263test_expect_success 'updates branches on other lines of descent' '
264 test_when_finished "rm -rf repo" &&
265 git init repo &&
266 (
267 cd repo &&
268 test_commit base &&
269 test_commit target &&
270 git branch theirs &&
271 test_commit ours &&
272 git switch theirs &&
273 test_commit theirs &&
274
275 expect_graph --branches <<-\EOF &&
276 * theirs
277 | * ours
278 |/
279 * target
280 * base
281 EOF
282
283 git history drop target &&
284
285 expect_graph --branches <<-\EOF
286 * ours
287 | * theirs
288 |/
289 * base
290 EOF
291 )
292'
293
294test_expect_success 'moves branch pointing at dropped commit to its parent' '
295 test_when_finished "rm -rf repo" &&
296 git init repo --initial-branch=main &&
297 (
298 cd repo &&
299 test_commit first &&
300 test_commit second &&
301 git branch points-at-second &&
302 test_commit third &&
303
304 git rev-parse first >expect &&
305 git history drop second &&
306 git rev-parse points-at-second >actual &&
307 test_cmp expect actual &&
308
309 expect_log --format="%s %D" --branches <<-\EOF
310 third HEAD -> main
311 first tag: first, points-at-second
312 EOF
313 )
314'
315
316test_expect_success '--dry-run prints ref updates without modifying repo' '
317 test_when_finished "rm -rf repo" &&
318 git init repo --initial-branch=main &&
319 (
320 cd repo &&
321 test_commit base &&
322 git branch branch &&
323 test_commit middle &&
324 test_commit ours &&
325 git switch branch &&
326 test_commit theirs &&
327
328 git refs list >refs-expect &&
329 git history drop --dry-run main~ >updates &&
330 git refs list >refs-actual &&
331 test_cmp refs-expect refs-actual &&
332 test_grep "update refs/heads/main" updates &&
333
334 git update-ref --stdin <updates &&
335 expect_log main <<-\EOF
336 ours
337 base
338 EOF
339 )
340'
341
342test_expect_success '--dry-run detects conflicts with modified working tree' '
343 test_when_finished "rm -rf repo" &&
344 git init repo --initial-branch=main &&
345 (
346 cd repo &&
347 test_commit first &&
348 test_commit second modify-me &&
349 echo modified >modify-me &&
350
351 git refs list >refs-expect &&
352 git diff >diff-expect &&
353 test_must_fail git history drop --dry-run HEAD 2>err &&
354 test_grep "dropping this commit would overwrite local changes" err &&
355 git diff >diff-actual &&
356 git refs list >refs-actual &&
357
358 test_cmp diff-expect diff-actual &&
359 test_cmp refs-expect refs-actual
360 )
361'
362
363test_expect_success '--update-refs=head updates only HEAD' '
364 test_when_finished "rm -rf repo" &&
365 git init repo --initial-branch=main &&
366 (
367 cd repo &&
368 test_commit base &&
369 test_commit target &&
370 git branch theirs &&
371 test_commit ours &&
372 git switch theirs &&
373 test_commit theirs &&
374
375 # When told to update HEAD only, the command refuses to
376 # rewrite commits that are not an ancestor of HEAD.
377 test_must_fail git history drop --update-refs=head main 2>err &&
378 test_grep "rewritten commit must be an ancestor of HEAD" err &&
379
380 expect_graph --branches <<-\EOF &&
381 * theirs
382 | * ours
383 |/
384 * target
385 * base
386 EOF
387
388 git switch main &&
389 git history drop --update-refs=head target &&
390
391 expect_graph --branches <<-\EOF
392 * ours
393 | * theirs
394 | * target
395 |/
396 * base
397 EOF
398 )
399'
400
401test_expect_success '--update-refs=head can rewrite detached HEAD' '
402 test_when_finished "rm -rf repo" &&
403 git init repo --initial-branch=main &&
404 (
405 cd repo &&
406 test_commit first &&
407 test_commit second &&
408 test_commit third &&
409 git switch --detach HEAD &&
410
411 git history drop --update-refs=head second &&
412
413 expect_log HEAD <<-\EOF &&
414 third
415 first
416 EOF
417 expect_log main <<-\EOF
418 third
419 second
420 first
421 EOF
422 )
423'
424
425test_expect_success 'conflict with replayed commit aborts cleanly' '
426 test_when_finished "rm -rf repo" &&
427 git init repo &&
428 (
429 cd repo &&
430 test_commit base &&
431 test_commit conflict-a file &&
432 test_commit conflict-b file &&
433
434 git refs list >refs-expect &&
435 test_must_fail git history drop HEAD~ 2>err &&
436 test_grep "failed replaying descendants" err &&
437 git refs list >refs-actual &&
438 test_cmp refs-expect refs-actual
439 )
440'
441
442# Build a history where a descendant of the drop target reverts the change
443# introduced by the drop target. After dropping, the descendant's diff applies
444# against a tree that already lacks the change, so it becomes empty.
445setup_empty_descendant_repo () {
446 git init "$1" &&
447 (
448 cd "$1" &&
449 echo C1 >file &&
450 git add file &&
451 git commit -m "base" &&
452 git tag base &&
453 echo C2 >file &&
454 git add file &&
455 git commit -m "drop-me" &&
456 git tag drop-me &&
457 test_commit middle &&
458 echo C1 >file &&
459 git add file &&
460 git commit -m "revert-drop-me" &&
461 git tag revert-drop-me
462 )
463}
464
465test_expect_success '--empty=drop drops descendants that become empty' '
466 test_when_finished "rm -rf repo" &&
467 setup_empty_descendant_repo repo &&
468 (
469 cd repo &&
470
471 git history drop --empty=drop drop-me &&
472
473 expect_log <<-\EOF
474 middle
475 base
476 EOF
477 )
478'
479
480test_expect_success '--empty=keep keeps descendants that become empty' '
481 test_when_finished "rm -rf repo" &&
482 setup_empty_descendant_repo repo &&
483 (
484 cd repo &&
485
486 git history drop --empty=keep drop-me &&
487
488 expect_log <<-\EOF &&
489 revert-drop-me
490 middle
491 base
492 EOF
493 git diff HEAD~ HEAD >diff &&
494 test_must_be_empty diff
495 )
496'
497
498test_expect_success '--empty=abort errors out when a descendant becomes empty' '
499 test_when_finished "rm -rf repo" &&
500 setup_empty_descendant_repo repo &&
501 (
502 cd repo &&
503
504 test_must_fail git history drop --empty=abort drop-me 2>err &&
505 test_grep "became empty after replay" err
506 )
507'
508
509test_expect_success 'updates index and worktree when HEAD moves' '
510 test_when_finished "rm -rf repo" &&
511 git init repo &&
512 (
513 cd repo &&
514 test_commit first &&
515 test_commit second &&
516 test_commit third &&
517
518 git history drop second &&
519
520 # Worktree should no longer contain second.t.
521 test_path_is_missing second.t &&
522 test_path_is_file first.t &&
523 test_path_is_file third.t &&
524
525 # Index and worktree should both match the new HEAD.
526 git status --porcelain --untracked-files=no >status &&
527 test_must_be_empty status
528 )
529'
530
531test_expect_success 'updates worktree when dropping HEAD itself' '
532 test_when_finished "rm -rf repo" &&
533 git init repo &&
534 (
535 cd repo &&
536 test_commit first &&
537 test_commit second &&
538
539 git history drop HEAD &&
540
541 test_path_is_missing second.t &&
542 test_path_is_file first.t &&
543
544 git status --porcelain --untracked-files=no >status &&
545 test_must_be_empty status
546 )
547'
548
549test_expect_success 'preserves unrelated unstaged modifications' '
550 test_when_finished "rm -rf repo" &&
551 git init repo &&
552 (
553 cd repo &&
554 test_commit first &&
555 echo first-content >unrelated.txt &&
556 git add unrelated.txt &&
557 git commit -m "add unrelated" &&
558 test_commit second &&
559 test_commit third &&
560
561 echo locally-modified >unrelated.txt &&
562
563 git diff >diff-expect &&
564 git history drop second &&
565 git diff >diff-actual &&
566 test_cmp diff-expect diff-actual &&
567 test_path_is_missing second.t
568 )
569'
570
571test_expect_success 'preserves unrelated staged changes' '
572 test_when_finished "rm -rf repo" &&
573 git init repo &&
574 (
575 cd repo &&
576 test_commit first &&
577 echo first-content >unrelated.txt &&
578 git add unrelated.txt &&
579 git commit -m "add unrelated" &&
580 test_commit second &&
581 test_commit third &&
582
583 echo staged-change >unrelated.txt &&
584 git add unrelated.txt &&
585
586 git diff --cached >diff-expect &&
587 git history drop second &&
588 git diff --cached >diff-actual &&
589 test_cmp diff-expect diff-actual &&
590 test_path_is_missing second.t
591 )
592'
593
594test_expect_success 'aborts when local modifications would be overwritten' '
595 test_when_finished "rm -rf repo" &&
596 git init repo &&
597 (
598 cd repo &&
599 test_commit base &&
600 test_commit conflict &&
601
602 echo local-edit >conflict.t &&
603 git diff >diff-expect &&
604 test_must_fail git history drop HEAD 2>err &&
605 test_grep "would overwrite local changes" err &&
606 git diff >diff-actual &&
607 test_cmp diff-expect diff-actual
608 )
609'
610
611test_done