Commit
cc499d40e5b1523a66271639746a1c05fa7771c9
commit: refuse partial commits during conflict resolution
Similar to the previous commit, just as `git commit --amend` is a foot-gun during conflict resolution, so is a partial commit (`git commit <paths>`). Recording a conflict resolution is about capturing the state of the entire tree on top of HEAD, not a subset of paths. For many years we have rejected partial commits in the middle of - a merge - a cherry-pick but, just like amending, this was never extended to the other operations that can also leave conflicts to resolve: - an `am` operation - a revert - a rebase that stopped for conflict resolution Reuse sequencer_ongoing_operation(), introduced for the analogous `--amend` check, to detect these and refuse the partial commit. A rebase that stopped because a pick became empty is not conflict resolution and, as an earlier patch established, is deliberately left permitted. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren <newren@gmail.com> · 2026-09-01 22:24 UTC · committed by Junio C Hamano
5 files changed,
+78
−7
- builtin/commit.c
- sequencer.h
- t/t3404-rebase-interactive.sh
- t/t3507-cherry-pick-conflict.sh
- t/t4151-am-abort.sh
modified builtin/commit.c
| @@ -515,11 +515,25 @@ static const char *prepare_index(const char **argv, const char *prefix, | |||
| 515 | 515 | */ | |
| 516 | 516 | commit_style = COMMIT_PARTIAL; | |
| 517 | 517 | ||
| 518 | - | if (whence != FROM_COMMIT) { | |
| 519 | - | if (whence == FROM_MERGE) | |
| 520 | - | die(_("cannot do a partial commit during a merge.")); | |
| 521 | - | else if (is_from_cherry_pick(whence)) | |
| 522 | - | die(_("cannot do a partial commit during a cherry-pick.")); | |
| 518 | + | switch (sequencer_ongoing_operation(the_repository, whence)) { | |
| 519 | + | case ONGOING_NONE: | |
| 520 | + | break; | |
| 521 | + | case ONGOING_MERGE: | |
| 522 | + | die(_("cannot do a partial commit during a merge.")); | |
| 523 | + | case ONGOING_CHERRY_PICK: | |
| 524 | + | die(_("cannot do a partial commit during a cherry-pick.")); | |
| 525 | + | case ONGOING_REBASE_NOW_EMPTY: | |
| 526 | + | /* | |
| 527 | + | * A pick that became empty is not a conflict, and creating | |
| 528 | + | * a new commit (partial or not) poses no problem. | |
| 529 | + | */ | |
| 530 | + | break; | |
| 531 | + | case ONGOING_REVERT: | |
| 532 | + | die(_("cannot do a partial commit during a revert.")); | |
| 533 | + | case ONGOING_AM: | |
| 534 | + | die(_("cannot do a partial commit during an am session.")); | |
| 535 | + | case ONGOING_REBASE_CONFLICT: | |
| 536 | + | die(_("cannot do a partial commit while resolving conflicts during a rebase.")); | |
| 523 | 537 | } | |
| 524 | 538 | ||
| 525 | 539 | if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec)) | |
modified sequencer.h
| @@ -271,8 +271,9 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) | |||
| 271 | 271 | ||
| 272 | 272 | /* | |
| 273 | 273 | * An in-progress operation that records its result (often a conflict | |
| 274 | - | * resolution) as a new commit on top of HEAD, during which amending | |
| 275 | - | * HEAD via "git commit --amend" is almost always a mistake. | |
| 274 | + | * resolution) as a new commit on top of HEAD. Some ways of invoking | |
| 275 | + | * "git commit" -- amending HEAD, or a partial commit -- are almost | |
| 276 | + | * always a mistake during such an operation. | |
| 276 | 277 | */ | |
| 277 | 278 | enum ongoing_operation { | |
| 278 | 279 | ONGOING_NONE = 0, | |
modified t/t3404-rebase-interactive.sh
| @@ -1941,6 +1941,40 @@ test_expect_success 'commit --amend is refused at an apply-backend conflict stop | |||
| 1941 | 1941 | ) | |
| 1942 | 1942 | ' | |
| 1943 | 1943 | ||
| 1944 | + | test_expect_success 'partial commit is refused at a rebase conflict stop' ' | |
| 1945 | + | test_when_finished "git rebase --abort" && | |
| 1946 | + | git checkout --detach conflict-branch && | |
| 1947 | + | ( | |
| 1948 | + | set_fake_editor && | |
| 1949 | + | FAKE_LINES="1 3" && | |
| 1950 | + | export FAKE_LINES && | |
| 1951 | + | test_must_fail git rebase -i A | |
| 1952 | + | ) && | |
| 1953 | + | echo resolved >conflict && | |
| 1954 | + | git add conflict && | |
| 1955 | + | test_must_fail git commit conflict 2>err && | |
| 1956 | + | test_grep "cannot do a partial commit while resolving conflicts during a rebase." err | |
| 1957 | + | ' | |
| 1958 | + | ||
| 1959 | + | test_expect_success 'partial commit is refused at an apply-backend conflict stop' ' | |
| 1960 | + | test_when_finished "rm -rf apply-backend" && | |
| 1961 | + | test_create_repo apply-backend && | |
| 1962 | + | ( | |
| 1963 | + | cd apply-backend && | |
| 1964 | + | test_commit base file && | |
| 1965 | + | git branch -M mainline && | |
| 1966 | + | test_commit upstream file upstream && | |
| 1967 | + | git checkout -b side mainline~1 && | |
| 1968 | + | test_commit conflicting file side && | |
| 1969 | + | test_commit unrelated other && | |
| 1970 | + | test_must_fail git rebase --apply mainline && | |
| 1971 | + | echo resolved >file && | |
| 1972 | + | git add file && | |
| 1973 | + | test_must_fail git commit file 2>err && | |
| 1974 | + | test_grep "cannot do a partial commit while resolving conflicts during a rebase." err | |
| 1975 | + | ) | |
| 1976 | + | ' | |
| 1977 | + | ||
| 1944 | 1978 | test_expect_success 'todo has correct onto hash' ' | |
| 1945 | 1979 | GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual && | |
| 1946 | 1980 | onto=$(git rev-parse --short HEAD~4) && | |
modified t/t3507-cherry-pick-conflict.sh
| @@ -375,6 +375,17 @@ test_expect_success 'commit --amend of revert fails' ' | |||
| 375 | 375 | test_grep "in the middle of a revert -- cannot amend." err | |
| 376 | 376 | ' | |
| 377 | 377 | ||
| 378 | + | test_expect_success 'partial commit during a revert fails' ' | |
| 379 | + | pristine_detach initial && | |
| 380 | + | ||
| 381 | + | test_must_fail git revert picked && | |
| 382 | + | echo resolved >foo && | |
| 383 | + | git add foo && | |
| 384 | + | test_must_fail git commit foo 2>err && | |
| 385 | + | ||
| 386 | + | test_grep "cannot do a partial commit during a revert." err | |
| 387 | + | ' | |
| 388 | + | ||
| 378 | 389 | test_expect_success 'successful revert does not set REVERT_HEAD' ' | |
| 379 | 390 | pristine_detach base && | |
| 380 | 391 | git revert base && | |
modified t/t4151-am-abort.sh
| @@ -74,6 +74,17 @@ test_expect_success 'commit --amend during a failed am fails' ' | |||
| 74 | 74 | git am --abort | |
| 75 | 75 | ' | |
| 76 | 76 | ||
| 77 | + | test_expect_success 'partial commit during a failed am fails' ' | |
| 78 | + | git reset --hard initial && | |
| 79 | + | cp file-2-expect file-2 && | |
| 80 | + | test_must_fail git am 000[1245]-*.patch && | |
| 81 | + | echo resolved >file-1 && | |
| 82 | + | git add file-1 && | |
| 83 | + | test_must_fail git commit file-1 2>err && | |
| 84 | + | test_grep "cannot do a partial commit during an am session." err && | |
| 85 | + | git am --abort | |
| 86 | + | ' | |
| 87 | + | ||
| 77 | 88 | test_expect_success 'am -3 --skip removes otherfile-4' ' | |
| 78 | 89 | git reset --hard initial && | |
| 79 | 90 | test_must_fail git am -3 0003-*.patch && | |