Compare
05cf4ceb5bc28580ef30cb45c8bbd8b447431cf4..6257588252ec0196c3d4567bec2921ed4e68d43e
commit: refuse to amend during conflict resolution
Running `git commit --amend` during conflict resolution is an ugly
foot-gun. For many years, we have rejected amending during conflict
resolution in the middle of
- a merge
- a cherry-pick
However, this was never extended to other operations that can also
produce conflicts:
- an `am` operation
- a revert
- a rebase
Extend it to handle these other cases now.
Extending to `am`, revert, and the apply backend of rebase are fairly
straightforward. However, with the merge backend of rebase we have to
be more careful, since it powers interactive rebases and
- the interactive machinery internally uses `git commit --amend` for
`squash` and `reword` directives
- users are expected to `git commit --amend` after hitting an `edit`
or `break` directive
So, we need to be careful with rebase to only reject amending when doing
conflict resolution.
A few files under the rebase-merge/ directory provide us the necessary
information:
- stopped-sha is written only when the rebase stops and hands control
back to the user, so its presence marks a genuine stop -- as opposed
to the sequencer's own internal `git commit --amend` while applying
a squash, fixup, or reword, during which no stopped-sha exists.
- amend is written only when the rebase stops with HEAD already
pointing at the commit the user is meant to amend: a clean `edit`,
or a fast-forward `reword`. Its absence at a stop therefore means
the commit did not apply, so HEAD is the previously-applied commit
rather than the one being rebased -- exactly the case we refuse.
So for the merge backend we die when stopped-sha exists and amend does
not. This covers a plain conflicted pick as well as a conflicted `edit`
(both leave HEAD on the previously-applied commit), while still allowing
a clean `edit` or `reword` stop and a `break` stop (no stopped-sha).
stopped-sha is unlinked at the start of the resume loop, so a resumed
squash's internal amend is unaffected.
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
11 files changed,
+258
−85
- builtin/checkout.c
- builtin/commit.c
- builtin/worktree.c
- checkout.c
- checkout.h
- sequencer.c
- sequencer.h
- t/t3404-rebase-interactive.sh
- t/t3507-cherry-pick-conflict.sh
- t/t4151-am-abort.sh
- wt-status.h
modified builtin/checkout.c
| @@ -1340,51 +1340,13 @@ enum checkout_command { | |||
| 1340 | 1340 | CHECKOUT_RESTORE = 3, | |
| 1341 | 1341 | }; | |
| 1342 | 1342 | ||
| 1343 | - | static void advise_disambiguating_remotes(enum checkout_command which_command, | |
| 1344 | - | const char *branch, | |
| 1345 | - | const struct string_list *matched_remote_names) | |
| 1346 | - | { | |
| 1347 | - | const char *cmdname; | |
| 1348 | - | struct string_list_item *item; | |
| 1349 | - | ||
| 1350 | - | switch (which_command) { | |
| 1351 | - | case CHECKOUT_CHECKOUT: | |
| 1352 | - | cmdname = "checkout"; | |
| 1353 | - | break; | |
| 1354 | - | case CHECKOUT_SWITCH: | |
| 1355 | - | cmdname = "switch"; | |
| 1356 | - | break; | |
| 1357 | - | default: | |
| 1358 | - | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1359 | - | which_command); | |
| 1360 | - | break; | |
| 1361 | - | } | |
| 1362 | - | ||
| 1363 | - | advise(_("Branch name '%s' appears in multiple remotes:"), branch); | |
| 1364 | - | for_each_string_list_item(item, matched_remote_names) { | |
| 1365 | - | advise(_(" %s"), item->string); | |
| 1366 | - | } | |
| 1367 | - | advise(_("If you meant to check out a remote tracking branch on <remote>,\n" | |
| 1368 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1369 | - | "\n" | |
| 1370 | - | " git %s --track <remote>/%s\n" | |
| 1371 | - | "\n" | |
| 1372 | - | "If you'd like to always have checkouts of an ambiguous name prefer\n" | |
| 1373 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1374 | - | "checkout.defaultRemote=origin in your config."), | |
| 1375 | - | cmdname, branch); | |
| 1376 | - | } | |
| 1377 | - | ||
| 1378 | 1343 | static char *parse_remote_branch(const char *arg, | |
| 1379 | 1344 | struct object_id *rev, | |
| 1380 | 1345 | int could_be_checkout_paths, | |
| 1381 | 1346 | enum checkout_command which_command) | |
| 1382 | 1347 | { | |
| 1383 | 1348 | int num_matches = 0; | |
| 1384 | - | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 1385 | - | ||
| 1386 | - | char *remote = unique_tracking_name(arg, rev, &num_matches, | |
| 1387 | - | &matched_remote_names); | |
| 1349 | + | char *remote = unique_tracking_name(arg, rev, &num_matches); | |
| 1388 | 1350 | ||
| 1389 | 1351 | if (remote && could_be_checkout_paths) { | |
| 1390 | 1352 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
| @@ -1393,15 +1355,37 @@ static char *parse_remote_branch(const char *arg, | |||
| 1393 | 1355 | } | |
| 1394 | 1356 | ||
| 1395 | 1357 | if (!remote && num_matches > 1) { | |
| 1396 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1397 | - | advise_disambiguating_remotes(which_command, arg, | |
| 1398 | - | &matched_remote_names); | |
| 1399 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1400 | - | arg, num_matches); | |
| 1358 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1359 | + | const char *cmdname; | |
| 1360 | + | ||
| 1361 | + | switch (which_command) { | |
| 1362 | + | case CHECKOUT_CHECKOUT: | |
| 1363 | + | cmdname = "checkout"; | |
| 1364 | + | break; | |
| 1365 | + | case CHECKOUT_SWITCH: | |
| 1366 | + | cmdname = "switch"; | |
| 1367 | + | break; | |
| 1368 | + | default: | |
| 1369 | + | BUG("command <%d> should not reach parse_remote_branch", | |
| 1370 | + | which_command); | |
| 1371 | + | break; | |
| 1372 | + | } | |
| 1373 | + | ||
| 1374 | + | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1375 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1376 | + | "\n" | |
| 1377 | + | " git %s --track origin/<name>\n" | |
| 1378 | + | "\n" | |
| 1379 | + | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1380 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1381 | + | "checkout.defaultRemote=origin in your config."), | |
| 1382 | + | cmdname); | |
| 1383 | + | } | |
| 1384 | + | ||
| 1385 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1386 | + | arg, num_matches); | |
| 1401 | 1387 | } | |
| 1402 | 1388 | ||
| 1403 | - | string_list_clear(&matched_remote_names, 0); | |
| 1404 | - | ||
| 1405 | 1389 | return remote; | |
| 1406 | 1390 | } | |
| 1407 | 1391 | ||
modified builtin/commit.c
| @@ -520,8 +520,6 @@ static const char *prepare_index(const char **argv, const char *prefix, | |||
| 520 | 520 | die(_("cannot do a partial commit during a merge.")); | |
| 521 | 521 | else if (is_from_cherry_pick(whence)) | |
| 522 | 522 | die(_("cannot do a partial commit during a cherry-pick.")); | |
| 523 | - | else if (is_from_rebase(whence)) | |
| 524 | - | die(_("cannot do a partial commit during a rebase.")); | |
| 525 | 523 | } | |
| 526 | 524 | ||
| 527 | 525 | if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec)) | |
| @@ -893,7 +891,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, | |||
| 893 | 891 | */ | |
| 894 | 892 | else if (whence == FROM_MERGE) | |
| 895 | 893 | hook_arg1 = "merge"; | |
| 896 | - | else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) { | |
| 894 | + | else if (is_from_cherry_pick(whence) || is_from_rebase_now_empty(whence)) { | |
| 897 | 895 | hook_arg1 = "commit"; | |
| 898 | 896 | hook_arg2 = "CHERRY_PICK_HEAD"; | |
| 899 | 897 | } | |
| @@ -1086,7 +1084,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, | |||
| 1086 | 1084 | if (amend) | |
| 1087 | 1085 | fputs(_(empty_amend_advice), stderr); | |
| 1088 | 1086 | else if (is_from_cherry_pick(whence) || | |
| 1089 | - | whence == FROM_REBASE_PICK) { | |
| 1087 | + | is_from_rebase_now_empty(whence)) { | |
| 1090 | 1088 | fputs(_(empty_cherry_pick_advice), stderr); | |
| 1091 | 1089 | if (whence == FROM_CHERRY_PICK_SINGLE) | |
| 1092 | 1090 | fputs(_(empty_cherry_pick_advice_single), stderr); | |
| @@ -1326,15 +1324,30 @@ static int parse_and_validate_options(int argc, const char *argv[], | |||
| 1326 | 1324 | use_editor = 0; | |
| 1327 | 1325 | ||
| 1328 | 1326 | /* Sanity check options */ | |
| 1329 | - | if (amend && !current_head) | |
| 1330 | - | die(_("You have nothing to amend.")); | |
| 1331 | - | if (amend && whence != FROM_COMMIT) { | |
| 1332 | - | if (whence == FROM_MERGE) | |
| 1327 | + | if (amend) { | |
| 1328 | + | if (!current_head) | |
| 1329 | + | die(_("You have nothing to amend.")); | |
| 1330 | + | /* | |
| 1331 | + | * Refuse to amend in the middle of any operation that is | |
| 1332 | + | * meant to record its result as a new commit on top of HEAD | |
| 1333 | + | * rather than by rewriting HEAD. | |
| 1334 | + | */ | |
| 1335 | + | switch (sequencer_ongoing_operation(s->repo, whence)) { | |
| 1336 | + | case ONGOING_NONE: | |
| 1337 | + | break; | |
| 1338 | + | case ONGOING_MERGE: | |
| 1333 | 1339 | die(_("You are in the middle of a merge -- cannot amend.")); | |
| 1334 | - | else if (is_from_cherry_pick(whence)) | |
| 1340 | + | case ONGOING_CHERRY_PICK: | |
| 1335 | 1341 | die(_("You are in the middle of a cherry-pick -- cannot amend.")); | |
| 1336 | - | else if (whence == FROM_REBASE_PICK) | |
| 1337 | - | die(_("You are in the middle of a rebase -- cannot amend.")); | |
| 1342 | + | case ONGOING_REBASE_NOW_EMPTY: | |
| 1343 | + | die(_("The now-empty commit has been dropped -- cannot amend.")); | |
| 1344 | + | case ONGOING_REVERT: | |
| 1345 | + | die(_("You are in the middle of a revert -- cannot amend.")); | |
| 1346 | + | case ONGOING_AM: | |
| 1347 | + | die(_("You are in the middle of an am session -- cannot amend.")); | |
| 1348 | + | case ONGOING_REBASE_CONFLICT: | |
| 1349 | + | die(_("You are resolving conflicts during a rebase -- cannot amend.")); | |
| 1350 | + | } | |
| 1338 | 1351 | } | |
| 1339 | 1352 | if (fixup_message && squash_message) | |
| 1340 | 1353 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup"); | |
| @@ -1353,7 +1366,7 @@ static int parse_and_validate_options(int argc, const char *argv[], | |||
| 1353 | 1366 | if (amend && !use_message && !fixup_message) | |
| 1354 | 1367 | use_message = "HEAD"; | |
| 1355 | 1368 | if (!use_message && !is_from_cherry_pick(whence) && | |
| 1356 | - | !is_from_rebase(whence) && renew_authorship) | |
| 1369 | + | !is_from_rebase_now_empty(whence) && renew_authorship) | |
| 1357 | 1370 | die(_("--reset-author can be used only with -C, -c or --amend.")); | |
| 1358 | 1371 | if (use_message) { | |
| 1359 | 1372 | use_message_buffer = read_commit_message(use_message); | |
| @@ -1362,7 +1375,7 @@ static int parse_and_validate_options(int argc, const char *argv[], | |||
| 1362 | 1375 | author_message_buffer = use_message_buffer; | |
| 1363 | 1376 | } | |
| 1364 | 1377 | } | |
| 1365 | - | if ((is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) && | |
| 1378 | + | if ((is_from_cherry_pick(whence) || is_from_rebase_now_empty(whence)) && | |
| 1366 | 1379 | !renew_authorship) { | |
| 1367 | 1380 | author_message = "CHERRY_PICK_HEAD"; | |
| 1368 | 1381 | author_message_buffer = read_commit_message(author_message); | |
| @@ -1887,7 +1900,7 @@ int cmd_commit(int argc, | |||
| 1887 | 1900 | if (!reflog_msg) | |
| 1888 | 1901 | reflog_msg = is_from_cherry_pick(whence) | |
| 1889 | 1902 | ? "commit (cherry-pick)" | |
| 1890 | - | : is_from_rebase(whence) | |
| 1903 | + | : is_from_rebase_now_empty(whence) | |
| 1891 | 1904 | ? "commit (rebase)" | |
| 1892 | 1905 | : "commit"; | |
| 1893 | 1906 | commit_list_insert(current_head, &parents); | |
modified builtin/worktree.c
| @@ -781,7 +781,7 @@ static char *dwim_branch(const char *path, char **new_branch) | |||
| 781 | 781 | *new_branch = branchname; | |
| 782 | 782 | if (guess_remote) { | |
| 783 | 783 | struct object_id oid; | |
| 784 | - | char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL); | |
| 784 | + | char *remote = unique_tracking_name(*new_branch, &oid, NULL); | |
| 785 | 785 | return remote; | |
| 786 | 786 | } | |
| 787 | 787 | return NULL; | |
| @@ -903,7 +903,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 903 | 903 | ||
| 904 | 904 | commit = lookup_commit_reference_by_name(branch); | |
| 905 | 905 | if (!commit) { | |
| 906 | - | remote = unique_tracking_name(branch, &oid, NULL, NULL); | |
| 906 | + | remote = unique_tracking_name(branch, &oid, NULL); | |
| 907 | 907 | if (remote) { | |
| 908 | 908 | new_branch = branch; | |
| 909 | 909 | branch = new_branch_to_free = remote; | |
modified checkout.c
| @@ -8,7 +8,6 @@ | |||
| 8 | 8 | #include "checkout.h" | |
| 9 | 9 | #include "config.h" | |
| 10 | 10 | #include "strbuf.h" | |
| 11 | - | #include "string-list.h" | |
| 12 | 11 | ||
| 13 | 12 | struct tracking_name_data { | |
| 14 | 13 | /* const */ char *src_ref; | |
| @@ -18,7 +17,6 @@ struct tracking_name_data { | |||
| 18 | 17 | const char *default_remote; | |
| 19 | 18 | char *default_dst_ref; | |
| 20 | 19 | struct object_id *default_dst_oid; | |
| 21 | - | struct string_list *remote_names; | |
| 22 | 20 | }; | |
| 23 | 21 | ||
| 24 | 22 | #define TRACKING_NAME_DATA_INIT { 0 } | |
| @@ -41,8 +39,6 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 41 | 39 | oidcpy(dst, cb->dst_oid); | |
| 42 | 40 | cb->default_dst_oid = dst; | |
| 43 | 41 | } | |
| 44 | - | if (cb->remote_names) | |
| 45 | - | string_list_append(cb->remote_names, remote->name); | |
| 46 | 42 | if (cb->dst_ref) { | |
| 47 | 43 | free(query.dst); | |
| 48 | 44 | return 0; | |
| @@ -52,19 +48,14 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 52 | 48 | } | |
| 53 | 49 | ||
| 54 | 50 | char *unique_tracking_name(const char *name, struct object_id *oid, | |
| 55 | - | int *dwim_remotes_matched, | |
| 56 | - | struct string_list *dwim_remote_names) | |
| 51 | + | int *dwim_remotes_matched) | |
| 57 | 52 | { | |
| 58 | 53 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; | |
| 59 | 54 | const char *default_remote = NULL; | |
| 60 | - | ||
| 61 | - | if (!repo_config_get_string_tmp(the_repository, | |
| 62 | - | "checkout.defaultremote", | |
| 63 | - | &default_remote)) | |
| 55 | + | if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote)) | |
| 64 | 56 | cb_data.default_remote = default_remote; | |
| 65 | 57 | cb_data.src_ref = xstrfmt("refs/heads/%s", name); | |
| 66 | 58 | cb_data.dst_oid = oid; | |
| 67 | - | cb_data.remote_names = dwim_remote_names; | |
| 68 | 59 | for_each_remote(check_tracking_name, &cb_data); | |
| 69 | 60 | if (dwim_remotes_matched) | |
| 70 | 61 | *dwim_remotes_matched = cb_data.num_matches; | |
modified checkout.h
| @@ -3,8 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | 4 | #include "hash.h" | |
| 5 | 5 | ||
| 6 | - | struct string_list; | |
| 7 | - | ||
| 8 | 6 | /* | |
| 9 | 7 | * Check if the branch name uniquely matches a branch name on a remote | |
| 10 | 8 | * tracking branch. Return the name of the remote if such a branch | |
| @@ -12,7 +10,6 @@ struct string_list; | |||
| 12 | 10 | */ | |
| 13 | 11 | char *unique_tracking_name(const char *name, | |
| 14 | 12 | struct object_id *oid, | |
| 15 | - | int *dwim_remotes_matched, | |
| 16 | - | struct string_list *dwim_remote_names); | |
| 13 | + | int *dwim_remotes_matched); | |
| 17 | 14 | ||
| 18 | 15 | #endif /* CHECKOUT_H */ | |
modified sequencer.c
| @@ -142,6 +142,13 @@ static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |||
| 142 | 142 | * command is processed, this file is deleted. | |
| 143 | 143 | */ | |
| 144 | 144 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") | |
| 145 | + | /* | |
| 146 | + | * The apply ("am") backend keeps its state in the rebase-apply directory; | |
| 147 | + | * the "applying" file within it marks a plain `git am` (as opposed to an | |
| 148 | + | * apply-based rebase). | |
| 149 | + | */ | |
| 150 | + | static GIT_PATH_FUNC(apply_dir, "rebase-apply") | |
| 151 | + | static GIT_PATH_FUNC(apply_path_applying, "rebase-apply/applying") | |
| 145 | 152 | /* | |
| 146 | 153 | * When we stop at a given patch via the "edit" command, this file contains | |
| 147 | 154 | * the commit object name of the corresponding patch. | |
| @@ -6855,7 +6862,7 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) | |||
| 6855 | 6862 | !repo_get_oid(r, "REBASE_HEAD", &rebase_head) && | |
| 6856 | 6863 | !repo_get_oid(r, "CHERRY_PICK_HEAD", &cherry_pick_head) && | |
| 6857 | 6864 | oideq(&rebase_head, &cherry_pick_head)) | |
| 6858 | - | *whence = FROM_REBASE_PICK; | |
| 6865 | + | *whence = FROM_REBASE_NOW_EMPTY; | |
| 6859 | 6866 | else | |
| 6860 | 6867 | *whence = FROM_CHERRY_PICK_SINGLE; | |
| 6861 | 6868 | ||
| @@ -6865,6 +6872,56 @@ int sequencer_determine_whence(struct repository *r, enum commit_whence *whence) | |||
| 6865 | 6872 | return 0; | |
| 6866 | 6873 | } | |
| 6867 | 6874 | ||
| 6875 | + | enum ongoing_operation sequencer_ongoing_operation(struct repository *r, | |
| 6876 | + | enum commit_whence whence) | |
| 6877 | + | { | |
| 6878 | + | /* | |
| 6879 | + | * The merge, cherry-pick, and (empty) rebase-pick stops are already | |
| 6880 | + | * distinguished by 'whence'. | |
| 6881 | + | */ | |
| 6882 | + | switch (whence) { | |
| 6883 | + | case FROM_MERGE: | |
| 6884 | + | return ONGOING_MERGE; | |
| 6885 | + | case FROM_CHERRY_PICK_SINGLE: | |
| 6886 | + | case FROM_CHERRY_PICK_MULTI: | |
| 6887 | + | return ONGOING_CHERRY_PICK; | |
| 6888 | + | case FROM_REBASE_NOW_EMPTY: | |
| 6889 | + | return ONGOING_REBASE_NOW_EMPTY; | |
| 6890 | + | case FROM_COMMIT: | |
| 6891 | + | break; | |
| 6892 | + | } | |
| 6893 | + | ||
| 6894 | + | /* | |
| 6895 | + | * 'whence' is FROM_COMMIT, but we may still be in the middle of an | |
| 6896 | + | * operation that records its result on top of HEAD; detect those | |
| 6897 | + | * from their on-disk state. | |
| 6898 | + | */ | |
| 6899 | + | ||
| 6900 | + | /* In the middle of a revert? */ | |
| 6901 | + | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) | |
| 6902 | + | return ONGOING_REVERT; | |
| 6903 | + | ||
| 6904 | + | /* In the middle of an `am`? */ | |
| 6905 | + | if (file_exists(apply_path_applying())) | |
| 6906 | + | return ONGOING_AM; | |
| 6907 | + | ||
| 6908 | + | /* | |
| 6909 | + | * In the middle of a rebase that stopped for conflict resolution? | |
| 6910 | + | * The apply backend only ever stops for conflicts, so the presence | |
| 6911 | + | * of its state directory is enough. The merge backend writes | |
| 6912 | + | * stopped-sha whenever it hands control back to the user, but omits | |
| 6913 | + | * `amend` unless it stopped with HEAD already pointing at the commit | |
| 6914 | + | * to be amended (a clean edit/reword stop); its absence therefore | |
| 6915 | + | * marks a conflicted stop. | |
| 6916 | + | */ | |
| 6917 | + | if (file_exists(apply_dir()) || | |
| 6918 | + | (file_exists(rebase_path_stopped_sha()) && | |
| 6919 | + | !file_exists(rebase_path_amend()))) | |
| 6920 | + | return ONGOING_REBASE_CONFLICT; | |
| 6921 | + | ||
| 6922 | + | return ONGOING_NONE; | |
| 6923 | + | } | |
| 6924 | + | ||
| 6868 | 6925 | int sequencer_get_update_refs_state(const char *wt_dir, | |
| 6869 | 6926 | struct string_list *refs) | |
| 6870 | 6927 | { | |
modified sequencer.h
| @@ -269,6 +269,29 @@ int sequencer_get_last_command(struct repository* r, | |||
| 269 | 269 | enum replay_action *action); | |
| 270 | 270 | int sequencer_determine_whence(struct repository *r, enum commit_whence *whence); | |
| 271 | 271 | ||
| 272 | + | /* | |
| 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. | |
| 276 | + | */ | |
| 277 | + | enum ongoing_operation { | |
| 278 | + | ONGOING_NONE = 0, | |
| 279 | + | ONGOING_MERGE, | |
| 280 | + | ONGOING_CHERRY_PICK, | |
| 281 | + | ONGOING_REBASE_NOW_EMPTY, | |
| 282 | + | ONGOING_REVERT, | |
| 283 | + | ONGOING_AM, | |
| 284 | + | ONGOING_REBASE_CONFLICT | |
| 285 | + | }; | |
| 286 | + | ||
| 287 | + | /* | |
| 288 | + | * Return which in-progress operation, if any, is underway; see enum | |
| 289 | + | * ongoing_operation. 'whence' is the origin already computed for the | |
| 290 | + | * pending commit. | |
| 291 | + | */ | |
| 292 | + | enum ongoing_operation sequencer_ongoing_operation(struct repository *r, | |
| 293 | + | enum commit_whence whence); | |
| 294 | + | ||
| 272 | 295 | /** | |
| 273 | 296 | * Append the set of ref-OID pairs that are currently stored for the 'git | |
| 274 | 297 | * rebase --update-refs' feature if such a rebase is currently happening. | |
modified t/t3404-rebase-interactive.sh
| @@ -1829,7 +1829,7 @@ test_expect_success 'post-commit hook is called' ' | |||
| 1829 | 1829 | test_cmp expect actual | |
| 1830 | 1830 | ' | |
| 1831 | 1831 | ||
| 1832 | - | test_expect_success 'correct error message for partial commit after empty pick' ' | |
| 1832 | + | test_expect_success 'partial commit is allowed when a rebase pick becomes empty' ' | |
| 1833 | 1833 | test_when_finished "git rebase --abort" && | |
| 1834 | 1834 | ( | |
| 1835 | 1835 | set_fake_editor && | |
| @@ -1838,8 +1838,7 @@ test_expect_success 'correct error message for partial commit after empty pick' | |||
| 1838 | 1838 | test_must_fail git rebase -i A D | |
| 1839 | 1839 | ) && | |
| 1840 | 1840 | echo x >file1 && | |
| 1841 | - | test_must_fail git commit file1 2>err && | |
| 1842 | - | test_grep "cannot do a partial commit during a rebase." err | |
| 1841 | + | git commit file1 | |
| 1843 | 1842 | ' | |
| 1844 | 1843 | ||
| 1845 | 1844 | test_expect_success 'correct error message for commit --amend after empty pick' ' | |
| @@ -1852,7 +1851,94 @@ test_expect_success 'correct error message for commit --amend after empty pick' | |||
| 1852 | 1851 | ) && | |
| 1853 | 1852 | echo x>file1 && | |
| 1854 | 1853 | test_must_fail git commit -a --amend 2>err && | |
| 1855 | - | test_grep "middle of a rebase -- cannot amend." err | |
| 1854 | + | test_grep "now-empty commit has been dropped -- cannot amend." err | |
| 1855 | + | ' | |
| 1856 | + | ||
| 1857 | + | test_expect_success 'commit --amend is refused at a rebase conflict stop' ' | |
| 1858 | + | test_when_finished "git rebase --abort" && | |
| 1859 | + | git checkout --detach conflict-branch && | |
| 1860 | + | ( | |
| 1861 | + | set_fake_editor && | |
| 1862 | + | FAKE_LINES="1 3" && | |
| 1863 | + | export FAKE_LINES && | |
| 1864 | + | test_must_fail git rebase -i A | |
| 1865 | + | ) && | |
| 1866 | + | test_path_is_file .git/rebase-merge/patch && | |
| 1867 | + | test_path_is_missing .git/rebase-merge/amend && | |
| 1868 | + | echo resolved >conflict && | |
| 1869 | + | git add conflict && | |
| 1870 | + | test_must_fail git commit --amend --no-edit 2>err && | |
| 1871 | + | test_grep "You are resolving conflicts during a rebase -- cannot amend" err | |
| 1872 | + | ' | |
| 1873 | + | ||
| 1874 | + | test_expect_success 'commit --amend is refused when an "edit" pick conflicts' ' | |
| 1875 | + | test_when_finished "git rebase --abort" && | |
| 1876 | + | git checkout --detach conflict-branch && | |
| 1877 | + | ( | |
| 1878 | + | set_fake_editor && | |
| 1879 | + | FAKE_LINES="1 edit 3" && | |
| 1880 | + | export FAKE_LINES && | |
| 1881 | + | test_must_fail git rebase -i A | |
| 1882 | + | ) && | |
| 1883 | + | test_path_is_file .git/rebase-merge/patch && | |
| 1884 | + | test_path_is_missing .git/rebase-merge/amend && | |
| 1885 | + | echo resolved >conflict && | |
| 1886 | + | git add conflict && | |
| 1887 | + | test_must_fail git commit --amend --no-edit 2>err && | |
| 1888 | + | test_grep "You are resolving conflicts during a rebase -- cannot amend" err | |
| 1889 | + | ' | |
| 1890 | + | ||
| 1891 | + | test_expect_success 'commit --amend is allowed at a rebase edit stop' ' | |
| 1892 | + | test_when_finished "git rebase --abort" && | |
| 1893 | + | git checkout --detach no-conflict-branch && | |
| 1894 | + | ( | |
| 1895 | + | set_fake_editor && | |
| 1896 | + | FAKE_LINES="edit 1 2 3 4" && | |
| 1897 | + | export FAKE_LINES && | |
| 1898 | + | git rebase -i A | |
| 1899 | + | ) && | |
| 1900 | + | test_path_is_file .git/rebase-merge/amend && | |
| 1901 | + | echo tweak >fileJ && | |
| 1902 | + | git add fileJ && | |
| 1903 | + | git commit --amend --no-edit | |
| 1904 | + | ' | |
| 1905 | + | ||
| 1906 | + | test_expect_success 'commit --amend is allowed at a rebase break stop' ' | |
| 1907 | + | test_when_finished "git rebase --abort" && | |
| 1908 | + | git checkout --detach no-conflict-branch && | |
| 1909 | + | ( | |
| 1910 | + | set_fake_editor && | |
| 1911 | + | FAKE_LINES="break 1 2 3 4" && | |
| 1912 | + | export FAKE_LINES && | |
| 1913 | + | git rebase -i A | |
| 1914 | + | ) && | |
| 1915 | + | test_must_fail git rev-parse --verify REBASE_HEAD && | |
| 1916 | + | echo tweak >fileJ && | |
| 1917 | + | git add fileJ && | |
| 1918 | + | git commit --amend --no-edit | |
| 1919 | + | ' | |
| 1920 | + | ||
| 1921 | + | test_expect_success 'commit --amend is refused at an apply-backend conflict stop' ' | |
| 1922 | + | test_when_finished "rm -rf apply-backend" && | |
| 1923 | + | test_create_repo apply-backend && | |
| 1924 | + | ( | |
| 1925 | + | cd apply-backend && | |
| 1926 | + | test_commit base file && | |
| 1927 | + | git branch -M mainline && | |
| 1928 | + | test_commit upstream file upstream && | |
| 1929 | + | git checkout -b side mainline~1 && | |
| 1930 | + | test_commit conflicting file side && | |
| 1931 | + | test_commit unrelated other && | |
| 1932 | + | test_must_fail git rebase --apply mainline && | |
| 1933 | + | # the apply backend only ever stops for conflicts, and | |
| 1934 | + | # leaves HEAD on the previously-applied commit | |
| 1935 | + | test_path_is_dir .git/rebase-apply && | |
| 1936 | + | test_path_is_missing .git/rebase-apply/applying && | |
| 1937 | + | echo resolved >file && | |
| 1938 | + | git add file && | |
| 1939 | + | test_must_fail git commit --amend --no-edit 2>err && | |
| 1940 | + | test_grep "You are resolving conflicts during a rebase -- cannot amend" err | |
| 1941 | + | ) | |
| 1856 | 1942 | ' | |
| 1857 | 1943 | ||
| 1858 | 1944 | test_expect_success 'todo has correct onto hash' ' | |
modified t/t3507-cherry-pick-conflict.sh
| @@ -364,6 +364,17 @@ test_expect_success 'failed revert sets REVERT_HEAD' ' | |||
| 364 | 364 | test_cmp_rev picked REVERT_HEAD | |
| 365 | 365 | ' | |
| 366 | 366 | ||
| 367 | + | test_expect_success 'commit --amend of revert fails' ' | |
| 368 | + | pristine_detach initial && | |
| 369 | + | ||
| 370 | + | test_must_fail git revert picked && | |
| 371 | + | echo resolved >foo && | |
| 372 | + | git add foo && | |
| 373 | + | test_must_fail git commit --amend 2>err && | |
| 374 | + | ||
| 375 | + | test_grep "in the middle of a revert -- cannot amend." err | |
| 376 | + | ' | |
| 377 | + | ||
| 367 | 378 | test_expect_success 'successful revert does not set REVERT_HEAD' ' | |
| 368 | 379 | pristine_detach base && | |
| 369 | 380 | git revert base && | |
modified t/t4151-am-abort.sh
| @@ -63,6 +63,17 @@ do | |||
| 63 | 63 | ||
| 64 | 64 | done | |
| 65 | 65 | ||
| 66 | + | test_expect_success 'commit --amend during a failed am fails' ' | |
| 67 | + | git reset --hard initial && | |
| 68 | + | cp file-2-expect file-2 && | |
| 69 | + | test_must_fail git am 000[1245]-*.patch && | |
| 70 | + | echo resolved >file-1 && | |
| 71 | + | git add file-1 && | |
| 72 | + | test_must_fail git commit --amend 2>err && | |
| 73 | + | test_grep "in the middle of an am session -- cannot amend." err && | |
| 74 | + | git am --abort | |
| 75 | + | ' | |
| 76 | + | ||
| 66 | 77 | test_expect_success 'am -3 --skip removes otherfile-4' ' | |
| 67 | 78 | git reset --hard initial && | |
| 68 | 79 | test_must_fail git am -3 0003-*.patch && | |
modified wt-status.h
| @@ -41,7 +41,7 @@ enum commit_whence { | |||
| 41 | 41 | FROM_MERGE, /* commit came from merge */ | |
| 42 | 42 | FROM_CHERRY_PICK_SINGLE, /* commit came from cherry-pick */ | |
| 43 | 43 | FROM_CHERRY_PICK_MULTI, /* commit came from a sequence of cherry-picks */ | |
| 44 | - | FROM_REBASE_PICK /* commit came from a pick/reword/edit */ | |
| 44 | + | FROM_REBASE_NOW_EMPTY /* rebase applied a pick that became empty */ | |
| 45 | 45 | }; | |
| 46 | 46 | ||
| 47 | 47 | static inline int is_from_cherry_pick(enum commit_whence whence) | |
| @@ -50,9 +50,9 @@ static inline int is_from_cherry_pick(enum commit_whence whence) | |||
| 50 | 50 | whence == FROM_CHERRY_PICK_MULTI; | |
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | - | static inline int is_from_rebase(enum commit_whence whence) | |
| 53 | + | static inline int is_from_rebase_now_empty(enum commit_whence whence) | |
| 54 | 54 | { | |
| 55 | - | return whence == FROM_REBASE_PICK; | |
| 55 | + | return whence == FROM_REBASE_NOW_EMPTY; | |
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | 58 | struct wt_status_change_data { | |