diff options
| author | Junio C Hamano <gitster@pobox.com> | 2026-05-17 22:58:30 +0900 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2026-05-17 22:58:31 +0900 |
| commit | d17a7b8191d59e387e728ff60c2e226fa6c940bc (patch) | |
| tree | 6eb5135c6c3fc032ba0b5d81b397bd17d3eb9cd2 | |
| parent | 068c10c7413fee5a69db1a46fb32f335675b25ca (diff) | |
| parent | c07039ebc4bbf2eb6c852fb1280891a448d1bf48 (diff) | |
| download | git-d17a7b8191d59e387e728ff60c2e226fa6c940bc.tar.gz git-d17a7b8191d59e387e728ff60c2e226fa6c940bc.zip | |
Merge branch 'hn/git-checkout-m-with-stash'
"git checkout -m another-branch" was invented to deal with local
changes to paths that are different between the current and the new
branch, but it gave only one chance to resolve conflicts. The command
was taught to create a stash to save the local changes.
* hn/git-checkout-m-with-stash:
checkout -m: autostash when switching branches
checkout: rollback lock on early returns in merge_working_tree
sequencer: teach autostash apply to take optional conflict marker labels
sequencer: allow create_autostash to run silently
stash: add --label-ours, --label-theirs, --label-base for apply
| -rw-r--r-- | Documentation/git-checkout.adoc | 55 | ||||
| -rw-r--r-- | Documentation/git-stash.adoc | 11 | ||||
| -rw-r--r-- | Documentation/git-switch.adoc | 36 | ||||
| -rw-r--r-- | builtin/checkout.c | 166 | ||||
| -rw-r--r-- | builtin/commit.c | 3 | ||||
| -rw-r--r-- | builtin/merge.c | 15 | ||||
| -rw-r--r-- | builtin/stash.c | 28 | ||||
| -rw-r--r-- | sequencer.c | 69 | ||||
| -rw-r--r-- | sequencer.h | 7 | ||||
| -rwxr-xr-x | t/t3420-rebase-autostash.sh | 16 | ||||
| -rwxr-xr-x | t/t3903-stash.sh | 24 | ||||
| -rwxr-xr-x | t/t7201-co.sh | 71 | ||||
| -rwxr-xr-x | t/t7600-merge.sh | 3 | ||||
| -rw-r--r-- | xdiff-interface.c | 12 | ||||
| -rw-r--r-- | xdiff-interface.h | 1 | ||||
| -rw-r--r-- | xdiff/xmerge.c | 6 |
16 files changed, 343 insertions, 180 deletions
diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..a8b3b8c2e2 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -251,20 +251,19 @@ working tree, by copying them from elsewhere, extracting a tarball, etc. are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. - However, with this option, a three-way merge between the current - branch, your working tree contents, and the new branch - is done, and you will be on the new branch. -+ -When a merge conflict happens, the index entries for conflicting -paths are left unmerged, and you need to resolve the conflicts -and mark the resolved paths with `git add` (or `git rm` if the merge -should result in deletion of the path). + With this option, the conflicting local changes are + automatically stashed before the switch and reapplied + afterwards. If the local changes do not overlap with the + differences between branches, the switch proceeds without + stashing. If reapplying the stash results in conflicts, the + entry is saved to the stash list. Resolve the conflicts + and run `git stash drop` when done, or clear the working + tree (e.g. with `git reset --hard`) before running `git stash + pop` later to re-apply your changes. + When checking out paths from the index, this option lets you recreate the conflicted merge in the specified paths. This option cannot be used when checking out paths from a tree-ish. -+ -When switching branches with `--merge`, staged changes may be lost. `--conflict=<style>`:: The same as `--merge` option above, but changes the way the @@ -578,38 +577,36 @@ $ git checkout mytopic error: You have local changes to 'frotz'; not switching branches. ------------ -You can give the `-m` flag to the command, which would try a -three-way merge: +You can give the `-m` flag to the command, which will carry your local +changes to the new branch: ------------ $ git checkout -m mytopic -Auto-merging frotz +Applied autostash. +Switched to branch 'mytopic' +The following paths have local changes: +M frotz ------------ -After this three-way merge, the local modifications are _not_ +After the switch, the local modifications are reapplied and are _not_ registered in your index file, so `git diff` would show you what changes you made since the tip of the new branch. === 3. Merge conflict -When a merge conflict happens during switching branches with -the `-m` option, you would see something like this: +When the `--merge` (`-m`) option is given and the local changes +overlap with the changes in the branch we're switching to, the +changes are stashed and reapplied after the switch. If this +process results in conflicts, the stash entry is saved and a +message is printed: ------------ $ git checkout -m mytopic -Auto-merging frotz -ERROR: Merge conflict in frotz -fatal: merge program failed ------------- - -At this point, `git diff` shows the changes cleanly merged as in -the previous example, as well as the changes in the conflicted -files. Edit and resolve the conflict and mark it resolved with -`git add` as usual: - ------------- -$ edit frotz -$ git add frotz +Your local changes are stashed, however applying them +resulted in conflicts. You can either resolve the conflicts +and then discard the stash with "git stash drop", or, if you +do not want to resolve them now, run "git reset --hard" and +apply the local changes later by running "git stash pop". ------------ CONFIGURATION diff --git a/Documentation/git-stash.adoc b/Documentation/git-stash.adoc index b05c990ecd..50bb89f483 100644 --- a/Documentation/git-stash.adoc +++ b/Documentation/git-stash.adoc @@ -12,7 +12,7 @@ git stash list [<log-options>] git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>] git stash drop [-q | --quiet] [<stash>] git stash pop [--index] [-q | --quiet] [<stash>] -git stash apply [--index] [-q | --quiet] [<stash>] +git stash apply [--index] [-q | --quiet] [--label-ours=<label>] [--label-theirs=<label>] [--label-base=<label>] [<stash>] git stash branch <branchname> [<stash>] git stash [push] [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet] [-u | --include-untracked] [-a | --all] [(-m | --message) <message>] @@ -195,6 +195,15 @@ the index's ones. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally). +`--label-ours=<label>`:: +`--label-theirs=<label>`:: +`--label-base=<label>`:: + These options are only valid for the `apply` command. ++ +Use the given labels in conflict markers instead of the default +"Updated upstream", "Stashed changes", and "Stash base". +`--label-base` only has an effect with merge.conflictStyle=diff3. + `-k`:: `--keep-index`:: `--no-keep-index`:: diff --git a/Documentation/git-switch.adoc b/Documentation/git-switch.adoc index 87707e9265..d6c4f229a5 100644 --- a/Documentation/git-switch.adoc +++ b/Documentation/git-switch.adoc @@ -123,18 +123,19 @@ variable. `-m`:: `--merge`:: - If you have local modifications to one or more files that are - different between the current branch and the branch to which - you are switching, the command refuses to switch branches in - order to preserve your modifications in context. However, - with this option, a three-way merge between the current - branch, your working tree contents, and the new branch is - done, and you will be on the new branch. -+ -When a merge conflict happens, the index entries for conflicting -paths are left unmerged, and you need to resolve the conflicts -and mark the resolved paths with `git add` (or `git rm` if the merge -should result in deletion of the path). + If you have local modifications to one or more files that + are different between the current branch and the branch to + which you are switching, the command normally refuses to + switch branches in order to preserve your modifications in + context. However, with this option, the conflicting local + changes are automatically stashed before the switch and + reapplied afterwards. If the local changes do not overlap + with the differences between branches, the switch proceeds + without stashing. If reapplying the stash results in + conflicts, the entry is saved to the stash list. Resolve + the conflicts and run `git stash drop` when done, or clear + the working tree (e.g. with `git reset --hard`) before + running `git stash pop` later to re-apply your changes. `--conflict=<style>`:: The same as `--merge` option above, but changes the way the @@ -217,15 +218,18 @@ $ git switch mytopic error: You have local changes to 'frotz'; not switching branches. ------------ -You can give the `-m` flag to the command, which would try a three-way -merge: +You can give the `-m` flag to the command, which will carry your local +changes to the new branch: ------------ $ git switch -m mytopic -Auto-merging frotz +Applied autostash. +Switched to branch 'mytopic' +The following paths have local changes: +M frotz ------------ -After this three-way merge, the local modifications are _not_ +After the switch, the local modifications are reapplied and are _not_ registered in your index file, so `git diff` would show you what changes you made since the tip of the new branch. diff --git a/builtin/checkout.c b/builtin/checkout.c index ac0186a33e..1345e8574a 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -17,7 +17,6 @@ #include "merge-ll.h" #include "lockfile.h" #include "mem-pool.h" -#include "merge-ort-wrappers.h" #include "object-file.h" #include "object-name.h" #include "odb.h" @@ -30,6 +29,7 @@ #include "repo-settings.h" #include "resolve-undo.h" #include "revision.h" +#include "sequencer.h" #include "setup.h" #include "strvec.h" #include "submodule.h" @@ -100,6 +100,8 @@ struct checkout_opts { .auto_advance = 1, \ } +#define MERGE_WORKING_TREE_UNPACK_FAILED (-2) + struct branch_info { char *name; /* The short name used */ char *path; /* The full name of a real branch */ @@ -760,9 +762,9 @@ static void setup_branch_path(struct branch_info *branch) branch->path = strbuf_detach(&buf, NULL); } -static void init_topts(struct unpack_trees_options *topts, int merge, +static void init_topts(struct unpack_trees_options *topts, int show_progress, int overwrite_ignore, - struct commit *old_commit) + bool quiet) { memset(topts, 0, sizeof(*topts)); topts->head_idx = -1; @@ -774,7 +776,7 @@ static void init_topts(struct unpack_trees_options *topts, int merge, topts->initial_checkout = is_index_unborn(the_repository->index); topts->update = 1; topts->merge = 1; - topts->quiet = merge && old_commit; + topts->quiet = quiet; topts->verbose_update = show_progress; topts->fn = twoway_merge; topts->preserve_ignored = !overwrite_ignore; @@ -783,6 +785,7 @@ static void init_topts(struct unpack_trees_options *topts, int merge, static int merge_working_tree(const struct checkout_opts *opts, struct branch_info *old_branch_info, struct branch_info *new_branch_info, + bool quiet, int *writeout_error) { int ret; @@ -790,8 +793,10 @@ static int merge_working_tree(const struct checkout_opts *opts, struct tree *new_tree; repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); - if (repo_read_index_preload(the_repository, NULL, 0) < 0) + if (repo_read_index_preload(the_repository, NULL, 0) < 0) { + rollback_lock_file(&lock_file); return error(_("index file corrupt")); + } resolve_undo_clear_index(the_repository->index); if (opts->new_orphan_branch && opts->orphan_from_empty_tree) { @@ -804,14 +809,18 @@ static int merge_working_tree(const struct checkout_opts *opts, } else { new_tree = repo_get_commit_tree(the_repository, new_branch_info->commit); - if (!new_tree) + if (!new_tree) { + rollback_lock_file(&lock_file); return error(_("unable to read tree (%s)"), oid_to_hex(&new_branch_info->commit->object.oid)); + } } if (opts->discard_changes) { ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info); - if (ret) + if (ret) { + rollback_lock_file(&lock_file); return ret; + } } else { struct tree_desc trees[2]; struct tree *tree; @@ -821,13 +830,14 @@ static int merge_working_tree(const struct checkout_opts *opts, refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); if (unmerged_index(the_repository->index)) { + rollback_lock_file(&lock_file); error(_("you need to resolve your current index first")); return 1; } /* 2-way merge to the new branch */ - init_topts(&topts, opts->merge, opts->show_progress, - opts->overwrite_ignore, old_branch_info->commit); + init_topts(&topts, opts->show_progress, + opts->overwrite_ignore, quiet); init_checkout_metadata(&topts.meta, new_branch_info->refname, new_branch_info->commit ? &new_branch_info->commit->object.oid : @@ -853,82 +863,8 @@ static int merge_working_tree(const struct checkout_opts *opts, ret = unpack_trees(2, trees, &topts); clear_unpack_trees_porcelain(&topts); if (ret == -1) { - /* - * Unpack couldn't do a trivial merge; either - * give up or do a real merge, depending on - * whether the merge flag was used. - */ - struct tree *work; - struct tree *old_tree; - struct merge_options o; - struct strbuf sb = STRBUF_INIT; - struct strbuf old_commit_shortname = STRBUF_INIT; - - if (!opts->merge) - return 1; - - /* - * Without old_branch_info->commit, the below is the same as - * the two-tree unpack we already tried and failed. - */ - if (!old_branch_info->commit) - return 1; - old_tree = repo_get_commit_tree(the_repository, - old_branch_info->commit); - - if (repo_index_has_changes(the_repository, old_tree, &sb)) - die(_("cannot continue with staged changes in " - "the following files:\n%s"), sb.buf); - strbuf_release(&sb); - - /* Do more real merge */ - - /* - * We update the index fully, then write the - * tree from the index, then merge the new - * branch with the current tree, with the old - * branch as the base. Then we reset the index - * (but not the working tree) to the new - * branch, leaving the working tree as the - * merged version, but skipping unmerged - * entries in the index. - */ - - add_files_to_cache(the_repository, NULL, NULL, NULL, 0, - 0, 0); - init_ui_merge_options(&o, the_repository); - o.verbosity = 0; - work = write_in_core_index_as_tree(the_repository, - the_repository->index); - - ret = reset_tree(new_tree, - opts, 1, - writeout_error, new_branch_info); - if (ret) - return ret; - o.ancestor = old_branch_info->name; - if (!old_branch_info->name) { - strbuf_add_unique_abbrev(&old_commit_shortname, - &old_branch_info->commit->object.oid, - DEFAULT_ABBREV); - o.ancestor = old_commit_shortname.buf; - } - o.branch1 = new_branch_info->name; - o.branch2 = "local"; - o.conflict_style = opts->conflict_style; - ret = merge_ort_nonrecursive(&o, - new_tree, - work, - old_tree); - if (ret < 0) - die(NULL); - ret = reset_tree(new_tree, - opts, 0, - writeout_error, new_branch_info); - strbuf_release(&o.obuf); - strbuf_release(&old_commit_shortname); - if (ret) - return ret; + rollback_lock_file(&lock_file); + return MERGE_WORKING_TREE_UNPACK_FAILED; } } @@ -1173,6 +1109,10 @@ static int switch_branches(const struct checkout_opts *opts, struct object_id rev; int flag, writeout_error = 0; int do_merge = 1; + int created_autostash = 0; + struct strbuf old_commit_shortname = STRBUF_INIT; + struct strbuf autostash_msg = STRBUF_INIT; + const char *stash_label_base = NULL; trace2_cmd_mode("branch"); @@ -1210,11 +1150,49 @@ static int switch_branches(const struct checkout_opts *opts, do_merge = 0; } + if (old_branch_info.name) { + stash_label_base = old_branch_info.name; + } else if (old_branch_info.commit) { + strbuf_add_unique_abbrev(&old_commit_shortname, + &old_branch_info.commit->object.oid, + DEFAULT_ABBREV); + stash_label_base = old_commit_shortname.buf; + } + if (do_merge) { - ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); + ret = merge_working_tree(opts, &old_branch_info, new_branch_info, + opts->merge, &writeout_error); + if (ret == MERGE_WORKING_TREE_UNPACK_FAILED && opts->merge) { + strbuf_addf(&autostash_msg, + "autostash while switching to '%s'", + new_branch_info->name); + create_autostash_ref(the_repository, + "CHECKOUT_AUTOSTASH_HEAD", + autostash_msg.buf, true); + created_autostash = 1; + ret = merge_working_tree(opts, &old_branch_info, new_branch_info, + false, &writeout_error); + } + if (created_autostash) { + if (opts->conflict_style >= 0) { + struct strbuf cfg = STRBUF_INIT; + strbuf_addf(&cfg, "merge.conflictStyle=%s", + conflict_style_name(opts->conflict_style)); + git_config_push_parameter(cfg.buf); + strbuf_release(&cfg); + } + apply_autostash_ref(the_repository, + "CHECKOUT_AUTOSTASH_HEAD", + new_branch_info->name, + "local", + stash_label_base, + autostash_msg.buf); + } if (ret) { branch_info_release(&old_branch_info); - return ret; + strbuf_release(&old_commit_shortname); + strbuf_release(&autostash_msg); + return ret < 0 ? 1 : ret; } } @@ -1223,8 +1201,22 @@ static int switch_branches(const struct checkout_opts *opts, update_refs_for_switch(opts, &old_branch_info, new_branch_info); + if (created_autostash) { + discard_index(the_repository->index); + if (repo_read_index(the_repository) < 0) + die(_("index file corrupt")); + + if (!opts->quiet && new_branch_info->commit) { + printf(_("The following paths have local changes:\n")); + show_local_changes(&new_branch_info->commit->object, + &opts->diff_options); + } + } + ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1); branch_info_release(&old_branch_info); + strbuf_release(&old_commit_shortname); + strbuf_release(&autostash_msg); return ret || writeout_error; } diff --git a/builtin/commit.c b/builtin/commit.c index a3e52ac9ca..28f6174503 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1979,7 +1979,8 @@ int cmd_commit(int argc, &oid, flags); } - apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, NULL, NULL, NULL); cleanup: free_commit_extra_headers(extra); diff --git a/builtin/merge.c b/builtin/merge.c index 2cbce56f8d..aacf8c524e 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -537,7 +537,8 @@ static void finish(struct commit *head_commit, run_hooks_l(the_repository, "post-merge", squash ? "1" : "0", NULL); if (new_head) - apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, NULL, NULL, NULL); strbuf_release(&reflog_message); } @@ -1672,12 +1673,14 @@ int cmd_merge(int argc, } if (autostash) - create_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + create_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, false); if (checkout_fast_forward(the_repository, &head_commit->object.oid, &commit->object.oid, overwrite_ignore)) { - apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, NULL, NULL, NULL); ret = 1; goto done; } @@ -1764,7 +1767,8 @@ int cmd_merge(int argc, die_ff_impossible(); if (autostash) - create_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + create_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, false); /* We are going to make a new commit. */ git_committer_info(IDENT_STRICT); @@ -1849,7 +1853,8 @@ int cmd_merge(int argc, else fprintf(stderr, _("Merge with strategy %s failed.\n"), use_strategies[0]->name); - apply_autostash_ref(the_repository, "MERGE_AUTOSTASH"); + apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", + NULL, NULL, NULL, NULL); ret = 2; goto done; } else if (best_strategy == wt_strategy) diff --git a/builtin/stash.c b/builtin/stash.c index 0d27b2fb1f..32dbc97b47 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -44,7 +44,7 @@ #define BUILTIN_STASH_POP_USAGE \ N_("git stash pop [--index] [-q | --quiet] [<stash>]") #define BUILTIN_STASH_APPLY_USAGE \ - N_("git stash apply [--index] [-q | --quiet] [<stash>]") + N_("git stash apply [--index] [-q | --quiet] [--label-ours=<label>] [--label-theirs=<label>] [--label-base=<label>] [<stash>]") #define BUILTIN_STASH_BRANCH_USAGE \ N_("git stash branch <branchname> [<stash>]") #define BUILTIN_STASH_STORE_USAGE \ @@ -591,7 +591,9 @@ static void unstage_changes_unless_new(struct object_id *orig_tree) } static int do_apply_stash(const char *prefix, struct stash_info *info, - int index, int quiet) + int index, int quiet, + const char *label_ours, const char *label_theirs, + const char *label_base) { int clean, ret; int has_index = index; @@ -643,9 +645,9 @@ static int do_apply_stash(const char *prefix, struct stash_info *info, init_ui_merge_options(&o, the_repository); - o.branch1 = "Updated upstream"; - o.branch2 = "Stashed changes"; - o.ancestor = "Stash base"; + o.branch1 = label_ours ? label_ours : "Updated upstream"; + o.branch2 = label_theirs ? label_theirs : "Stashed changes"; + o.ancestor = label_base ? label_base : "Stash base"; if (oideq(&info->b_tree, &c_tree)) o.branch1 = "Version stash was based on"; @@ -723,11 +725,18 @@ static int apply_stash(int argc, const char **argv, const char *prefix, int ret = -1; int quiet = 0; int index = use_index; + const char *label_ours = NULL, *label_theirs = NULL, *label_base = NULL; struct stash_info info = STASH_INFO_INIT; struct option options[] = { OPT__QUIET(&quiet, N_("be quiet, only report errors")), OPT_BOOL(0, "index", &index, N_("attempt to recreate the index")), + OPT_STRING(0, "label-ours", &label_ours, N_("label"), + N_("label for the upstream side in conflict markers")), + OPT_STRING(0, "label-theirs", &label_theirs, N_("label"), + N_("label for the stashed side in conflict markers")), + OPT_STRING(0, "label-base", &label_base, N_("label"), + N_("label for the base in diff3 conflict markers")), OPT_END() }; @@ -737,7 +746,8 @@ static int apply_stash(int argc, const char **argv, const char *prefix, if (get_stash_info(&info, argc, argv)) goto cleanup; - ret = do_apply_stash(prefix, &info, index, quiet); + ret = do_apply_stash(prefix, &info, index, quiet, + label_ours, label_theirs, label_base); cleanup: free_stash_info(&info); return ret; @@ -836,7 +846,8 @@ static int pop_stash(int argc, const char **argv, const char *prefix, if (get_stash_info_assert(&info, argc, argv)) goto cleanup; - if ((ret = do_apply_stash(prefix, &info, index, quiet))) + if ((ret = do_apply_stash(prefix, &info, index, quiet, + NULL, NULL, NULL))) printf_ln(_("The stash entry is kept in case " "you need it again.")); else @@ -877,7 +888,8 @@ static int branch_stash(int argc, const char **argv, const char *prefix, strvec_push(&cp.args, oid_to_hex(&info.b_commit)); ret = run_command(&cp); if (!ret) - ret = do_apply_stash(prefix, &info, 1, 0); + ret = do_apply_stash(prefix, &info, 1, 0, + NULL, NULL, NULL); if (!ret && info.is_stash_ref) ret = do_drop_stash(&info, 0); diff --git a/sequencer.c b/sequencer.c index b7d8dca47f..746f85a442 100644 --- a/sequencer.c +++ b/sequencer.c @@ -4657,7 +4657,9 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset) static void create_autostash_internal(struct repository *r, const char *path, - const char *refname) + const char *refname, + const char *message, + bool silent) { struct strbuf buf = STRBUF_INIT; struct lock_file lock_file = LOCK_INIT; @@ -4679,7 +4681,8 @@ static void create_autostash_internal(struct repository *r, struct object_id oid; strvec_pushl(&stash.args, - "stash", "create", "autostash", NULL); + "stash", "create", + message ? message : "autostash", NULL); stash.git_cmd = 1; stash.no_stdin = 1; strbuf_reset(&buf); @@ -4702,7 +4705,8 @@ static void create_autostash_internal(struct repository *r, &oid, null_oid(the_hash_algo), 0, UPDATE_REFS_DIE_ON_ERR); } - printf(_("Created autostash: %s\n"), buf.buf); + if (!silent) + printf(_("Created autostash: %s\n"), buf.buf); if (reset_head(r, &ropts) < 0) die(_("could not reset --hard")); discard_index(r->index); @@ -4714,15 +4718,19 @@ static void create_autostash_internal(struct repository *r, void create_autostash(struct repository *r, const char *path) { - create_autostash_internal(r, path, NULL); + create_autostash_internal(r, path, NULL, NULL, false); } -void create_autostash_ref(struct repository *r, const char *refname) +void create_autostash_ref(struct repository *r, const char *refname, + const char *message, bool silent) { - create_autostash_internal(r, NULL, refname); + create_autostash_internal(r, NULL, refname, message, silent); } -static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) +static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply, + const char *label_ours, const char *label_theirs, + const char *label_base, + const char *stash_msg) { struct child_process child = CHILD_PROCESS_INIT; int ret = 0; @@ -4733,6 +4741,12 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) child.no_stderr = 1; strvec_push(&child.args, "stash"); strvec_push(&child.args, "apply"); + if (label_ours) + strvec_pushf(&child.args, "--label-ours=%s", label_ours); + if (label_theirs) + strvec_pushf(&child.args, "--label-theirs=%s", label_theirs); + if (label_base) + strvec_pushf(&child.args, "--label-base=%s", label_base); strvec_push(&child.args, stash_oid); ret = run_command(&child); } @@ -4746,20 +4760,24 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply) strvec_push(&store.args, "stash"); strvec_push(&store.args, "store"); strvec_push(&store.args, "-m"); - strvec_push(&store.args, "autostash"); + strvec_push(&store.args, stash_msg ? stash_msg : "autostash"); strvec_push(&store.args, "-q"); strvec_push(&store.args, stash_oid); if (run_command(&store)) ret = error(_("cannot store %s"), stash_oid); + else if (attempt_apply) + fprintf(stderr, + _("Your local changes are stashed, however applying them\n" + "resulted in conflicts. You can either resolve the conflicts\n" + "and then discard the stash with \"git stash drop\", or, if you\n" + "do not want to resolve them now, run \"git reset --hard\" and\n" + "apply the local changes later by running \"git stash pop\".\n")); else fprintf(stderr, - _("%s\n" + _("Autostash exists; creating a new stash entry.\n" "Your changes are safe in the stash.\n" "You can run \"git stash pop\" or" - " \"git stash drop\" at any time.\n"), - attempt_apply ? - _("Applying autostash resulted in conflicts.") : - _("Autostash exists; creating a new stash entry.")); + " \"git stash drop\" at any time.\n")); } return ret; @@ -4777,7 +4795,8 @@ static int apply_save_autostash(const char *path, int attempt_apply) } strbuf_trim(&stash_oid); - ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply); + ret = apply_save_autostash_oid(stash_oid.buf, attempt_apply, + NULL, NULL, NULL, NULL); unlink(path); strbuf_release(&stash_oid); @@ -4796,11 +4815,14 @@ int apply_autostash(const char *path) int apply_autostash_oid(const char *stash_oid) { - return apply_save_autostash_oid(stash_oid, 1); + return apply_save_autostash_oid(stash_oid, 1, NULL, NULL, NULL, NULL); } static int apply_save_autostash_ref(struct repository *r, const char *refname, - int attempt_apply) + int attempt_apply, + const char *label_ours, const char *label_theirs, + const char *label_base, + const char *stash_msg) { struct object_id stash_oid; char stash_oid_hex[GIT_MAX_HEXSZ + 1]; @@ -4816,7 +4838,9 @@ static int apply_save_autostash_ref(struct repository *r, const char *refname, return error(_("autostash reference is a symref")); oid_to_hex_r(stash_oid_hex, &stash_oid); - ret = apply_save_autostash_oid(stash_oid_hex, attempt_apply); + ret = apply_save_autostash_oid(stash_oid_hex, attempt_apply, + label_ours, label_theirs, label_base, + stash_msg); refs_delete_ref(get_main_ref_store(r), "", refname, &stash_oid, REF_NO_DEREF); @@ -4826,12 +4850,17 @@ static int apply_save_autostash_ref(struct repository *r, const char *refname, int save_autostash_ref(struct repository *r, const char *refname) { - return apply_save_autostash_ref(r, refname, 0); + return apply_save_autostash_ref(r, refname, 0, + NULL, NULL, NULL, NULL); } -int apply_autostash_ref(struct repository *r, const char *refname) +int apply_autostash_ref(struct repository *r, const char *refname, + const char *label_ours, const char *label_theirs, + const char *label_base, const char *stash_msg) { - return apply_save_autostash_ref(r, refname, 1); + return apply_save_autostash_ref(r, refname, 1, + label_ours, label_theirs, label_base, + stash_msg); } static int checkout_onto(struct repository *r, struct replay_opts *opts, diff --git a/sequencer.h b/sequencer.h index a6fa670c7c..3164bd437d 100644 --- a/sequencer.h +++ b/sequencer.h @@ -229,12 +229,15 @@ void commit_post_rewrite(struct repository *r, const struct object_id *new_head); void create_autostash(struct repository *r, const char *path); -void create_autostash_ref(struct repository *r, const char *refname); +void create_autostash_ref(struct repository *r, const char *refname, + const char *message, bool silent); int save_autostash(const char *path); int save_autostash_ref(struct repository *r, const char *refname); int apply_autostash(const char *path); int apply_autostash_oid(const char *stash_oid); -int apply_autostash_ref(struct repository *r, const char *refname); +int apply_autostash_ref(struct repository *r, const char *refname, + const char *label_ours, const char *label_theirs, + const char *label_base, const char *stash_msg); #define SUMMARY_INITIAL_COMMIT (1 << 0) #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1) diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh index ad3ba6a984..f0bbc476ff 100755 --- a/t/t3420-rebase-autostash.sh +++ b/t/t3420-rebase-autostash.sh @@ -61,18 +61,22 @@ create_expected_failure_apply () { First, rewinding head to replay your work on top of it... Applying: second commit Applying: third commit - Applying autostash resulted in conflicts. - Your changes are safe in the stash. - You can run "git stash pop" or "git stash drop" at any time. + Your local changes are stashed, however applying them + resulted in conflicts. You can either resolve the conflicts + and then discard the stash with "git stash drop", or, if you + do not want to resolve them now, run "git reset --hard" and + apply the local changes later by running "git stash pop". EOF } create_expected_failure_merge () { cat >expected <<-EOF $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) - Applying autostash resulted in conflicts. - Your changes are safe in the stash. - You can run "git stash pop" or "git stash drop" at any time. + Your local changes are stashed, however applying them + resulted in conflicts. You can either resolve the conflicts + and then discard the stash with "git stash drop", or, if you + do not want to resolve them now, run "git reset --hard" and + apply the local changes later by running "git stash pop". Successfully rebased and updated refs/heads/rebased-feature-branch. EOF } diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 70879941c2..bdaad22e1f 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -56,6 +56,7 @@ setup_stash() { git add other-file && test_tick && git commit -m initial && + git tag initial && echo 2 >file && git add file && echo 3 >file && @@ -1790,4 +1791,27 @@ test_expect_success 'stash.index=false overridden by --index' ' test_cmp expect file ' +test_expect_success 'apply with custom conflict labels' ' + git reset --hard initial && + test_commit label-base conflict-file base-content && + echo stashed >conflict-file && + git stash push -m "stashed" && + test_commit label-upstream conflict-file upstream-content && + test_must_fail git -c merge.conflictStyle=diff3 stash apply --label-ours=UP --label-theirs=STASH && + test_grep "^<<<<<<< UP" conflict-file && + test_grep "^||||||| Stash base" conflict-file && + test_grep "^>>>>>>> STASH" conflict-file +' + +test_expect_success 'apply with empty conflict labels' ' + git reset --hard initial && + test_commit empty-label-base conflict-file base-content && + echo stashed >conflict-file && + git stash push -m "stashed" && + test_commit empty-label-upstream conflict-file upstream-content && + test_must_fail git stash apply --label-ours= --label-theirs= && + test_grep "^<<<<<<<$" conflict-file && + test_grep "^>>>>>>>$" conflict-file +' + test_done diff --git a/t/t7201-co.sh b/t/t7201-co.sh index 9bcf7c0b40..7613b1d2a4 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -102,7 +102,10 @@ test_expect_success 'checkout -m with dirty tree' ' test "$(git symbolic-ref HEAD)" = "refs/heads/side" && - printf "M\t%s\n" one >expect.messages && + cat >expect.messages <<-\EOF && + The following paths have local changes: + M one + EOF test_cmp expect.messages messages && fill "M one" "A three" "D two" >expect.main && @@ -210,6 +213,72 @@ test_expect_success 'checkout --merge --conflict=diff3 <branch>' ' test_cmp expect two ' +test_expect_success 'checkout -m with mixed staged and unstaged changes' ' + git checkout -f main && + git clean -f && + + fill 0 x y z >same && + git add same && + fill 1 2 3 4 5 6 7 >one && + git checkout -m side >actual 2>&1 && + test_grep "Applied autostash" actual && + fill 0 x y z >expect && + test_cmp expect same && + fill 1 2 3 4 5 6 7 >expect && + test_cmp expect one +' + +test_expect_success 'checkout -m creates a recoverable stash on conflict' ' + git checkout -f main && + git clean -f && + + fill 1 2 3 4 5 >one && + test_must_fail git checkout side 2>stderr && + test_grep "Your local changes" stderr && + git checkout -m side >actual 2>&1 && + test_grep "resulted in conflicts" actual && + test_grep "git stash drop" actual && + test_grep "git stash pop" actual && + test_grep "The following paths have local changes" actual && + git log -p -1 --format="%gs%n%B" -g --diff-merges=1 refs/stash >actual && + sed /^index/d actual >actual.trimmed && + cat >expect <<-EOF && + autostash while switching to ${SQ}side${SQ} + On main: autostash while switching to ${SQ}side${SQ} + + diff --git a/one b/one + --- a/one + +++ b/one + @@ -3,6 +3,3 @@ + 3 + 4 + 5 + -6 + -7 + -8 + EOF + test_cmp expect actual.trimmed && + git stash drop && + git reset --hard +' + +test_expect_success 'checkout -m which would overwrite untracked file' ' + git checkout -f --detach main && + test_commit another-file && + git checkout HEAD^ && + >another-file.t && + fill 1 2 3 4 5 >one && + test_must_fail git checkout -m @{-1} 2>err && + q_to_tab >expect <<-\EOF && + error: The following untracked working tree files would be overwritten by checkout: + Qanother-file.t + Please move or remove them before you switch branches. + Aborting + Applied autostash. + EOF + test_cmp expect err +' + test_expect_success 'switch to another branch while carrying a deletion' ' git checkout -f main && git reset --hard && diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh index 9838094b66..f877d9a433 100755 --- a/t/t7600-merge.sh +++ b/t/t7600-merge.sh @@ -914,7 +914,8 @@ test_expect_success 'merge with conflicted --autostash changes' ' git diff >expect && test_when_finished "test_might_fail git stash drop" && git merge --autostash c3 2>err && - test_grep "Applying autostash resulted in conflicts." err && + test_grep "applying them" err && + test_grep "resulted in conflicts" err && git show HEAD:file >merge-result && test_cmp result.1-9 merge-result && git stash show -p >actual && diff --git a/xdiff-interface.c b/xdiff-interface.c index f043330f2a..5ee2b96d0a 100644 --- a/xdiff-interface.c +++ b/xdiff-interface.c @@ -325,6 +325,18 @@ int parse_conflict_style_name(const char *value) return -1; } +const char *conflict_style_name(int style) +{ + switch (style) { + case XDL_MERGE_DIFF3: + return "diff3"; + case XDL_MERGE_ZEALOUS_DIFF3: + return "zdiff3"; + default: + return "merge"; + } +} + int git_xmerge_style = -1; int git_xmerge_config(const char *var, const char *value, diff --git a/xdiff-interface.h b/xdiff-interface.h index fbc4ceec40..ce54e1c0e0 100644 --- a/xdiff-interface.h +++ b/xdiff-interface.h @@ -55,6 +55,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags); void xdiff_clear_find_func(xdemitconf_t *xecfg); struct config_context; int parse_conflict_style_name(const char *value); +const char *conflict_style_name(int style); int git_xmerge_config(const char *var, const char *value, const struct config_context *ctx, void *cb); extern int git_xmerge_style; diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c index 29dad98c49..659ad4ec97 100644 --- a/xdiff/xmerge.c +++ b/xdiff/xmerge.c @@ -199,9 +199,9 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, int size, int i, int style, xdmerge_t *m, char *dest, int marker_size) { - int marker1_size = (name1 ? strlen(name1) + 1 : 0); - int marker2_size = (name2 ? strlen(name2) + 1 : 0); - int marker3_size = (name3 ? strlen(name3) + 1 : 0); + int marker1_size = (name1 && *name1 ? strlen(name1) + 1 : 0); + int marker2_size = (name2 && *name2 ? strlen(name2) + 1 : 0); + int marker3_size = (name3 && *name3 ? strlen(name3) + 1 : 0); int needs_cr = is_cr_needed(xe1, xe2, m); if (marker_size <= 0) |
