Skip to content

Commit

Permalink
merge: teach --autostash option
Browse files Browse the repository at this point in the history
In rebase, one can pass the `--autostash` option to cause the worktree
to be automatically stashed before continuing with the rebase. This
option is missing in merge, however.

Implement the `--autostash` option and corresponding `merge.autoStash`
option in merge which stashes before merging and then pops after.

This option is useful when a developer has some local changes on a topic
branch but they realize that their work depends on another branch.
Previously, they had to run something like

	git fetch ...
	git stash push
	git merge FETCH_HEAD
	git stash pop

but now, that is reduced to

	git fetch ...
	git merge --autostash FETCH_HEAD

When an autostash is generated, it is automatically reapplied to the
worktree only in three explicit situations:

	1. An incomplete merge is commit using `git commit`.
	2. A merge completes successfully.
	3. A merge is aborted using `git merge --abort`.

In all other situations where the merge state is removed using
remove_merge_branch_state() such as aborting a merge via
`git reset --hard`, the autostash is saved into the stash reflog
instead keeping the worktree clean.

Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Suggested-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Denton-L authored and gitster committed Apr 10, 2020
1 parent 804fe31 commit a03b555
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Documentation/config/merge.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ merge.stat::
Whether to print the diffstat between ORIG_HEAD and the merge result
at the end of the merge. True by default.

merge.autoStash::
When set to true, automatically create a temporary stash entry
before the operation begins, and apply it after the operation
ends. This means that you can run merge on a dirty worktree.
However, use with care: the final stash application after a
successful merge might result in non-trivial conflicts.
This option can be overridden by the `--no-autostash` and
`--autostash` options of linkgit:git-merge[1].
Defaults to false.

merge.tool::
Controls which merge tool is used by linkgit:git-mergetool[1].
The list below shows the valid built-in values.
Expand Down
11 changes: 8 additions & 3 deletions Documentation/git-merge.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,24 @@ will be appended to the specified message.

--abort::
Abort the current conflict resolution process, and
try to reconstruct the pre-merge state.
try to reconstruct the pre-merge state. If an autostash entry is
present, apply it to the worktree.
+
If there were uncommitted worktree changes present when the merge
started, 'git merge --abort' will in some cases be unable to
reconstruct these changes. It is therefore recommended to always
commit or stash your changes before running 'git merge'.
+
'git merge --abort' is equivalent to 'git reset --merge' when
`MERGE_HEAD` is present.
`MERGE_HEAD` is present unless `MERGE_AUTOSTASH` is also present in
which case 'git merge --abort' applies the stash entry to the worktree
whereas 'git reset --merge' will save the stashed changes in the stash
reflog.

--quit::
Forget about the current merge in progress. Leave the index
and the working tree as-is.
and the working tree as-is. If `MERGE_AUTOSTASH` is present, the
stash entry will be saved to the stash reflog.

--continue::
After a 'git merge' stops due to conflicts you can conclude the
Expand Down
8 changes: 8 additions & 0 deletions Documentation/merge-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ ifndef::git-pull[]
Note that not all merge strategies may support progress
reporting.

--autostash::
--no-autostash::
Automatically create a temporary stash entry before the operation
begins, and apply it after the operation ends. This means
that you can run the operation on a dirty worktree. However, use
with care: the final stash application after a successful
merge might result in non-trivial conflicts.

endif::git-pull[]

--allow-unrelated-histories::
Expand Down
1 change: 1 addition & 0 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ void remove_merge_branch_state(struct repository *r)
unlink(git_path_merge_rr(r));
unlink(git_path_merge_msg(r));
unlink(git_path_merge_mode(r));
save_autostash(git_path_merge_autostash(r));
}

void remove_branch_state(struct repository *r, int verbose)
Expand Down
2 changes: 2 additions & 0 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
&oid, flags);
}

apply_autostash(git_path_merge_autostash(the_repository));

UNLEAK(err);
UNLEAK(sb);
return 0;
Expand Down
25 changes: 25 additions & 0 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static int show_progress = -1;
static int default_to_upstream = 1;
static int signoff;
static const char *sign_commit;
static int autostash;
static int no_verify;

static struct strategy all_strategy[] = {
Expand Down Expand Up @@ -286,6 +287,7 @@ static struct option builtin_merge_options[] = {
OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
OPT_AUTOSTASH(&autostash),
OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
Expand Down Expand Up @@ -475,6 +477,7 @@ static void finish(struct commit *head_commit,
/* Run a post-merge hook */
run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);

apply_autostash(git_path_merge_autostash(the_repository));
strbuf_release(&reflog_message);
}

Expand Down Expand Up @@ -634,6 +637,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
return 0;
} else if (!strcmp(k, "gpg.mintrustlevel")) {
check_trust_level = 0;
} else if (!strcmp(k, "merge.autostash")) {
autostash = git_config_bool(k, v);
return 0;
}

status = fmt_merge_msg_config(k, v, cb);
Expand Down Expand Up @@ -1281,6 +1287,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
if (abort_current_merge) {
int nargc = 2;
const char *nargv[] = {"reset", "--merge", NULL};
struct strbuf stash_oid = STRBUF_INIT;

if (orig_argc != 2)
usage_msg_opt(_("--abort expects no arguments"),
Expand All @@ -1289,8 +1296,17 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
if (!file_exists(git_path_merge_head(the_repository)))
die(_("There is no merge to abort (MERGE_HEAD missing)."));

if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository),
READ_ONELINER_SKIP_IF_EMPTY))
unlink(git_path_merge_autostash(the_repository));

/* Invoke 'git reset --merge' */
ret = cmd_reset(nargc, nargv, prefix);

if (stash_oid.len)
apply_autostash_oid(stash_oid.buf);

strbuf_release(&stash_oid);
goto done;
}

Expand Down Expand Up @@ -1513,6 +1529,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
goto done;
}

if (autostash)
create_autostash(the_repository,
git_path_merge_autostash(the_repository),
"merge");
if (checkout_fast_forward(the_repository,
&head_commit->object.oid,
&commit->object.oid,
Expand Down Expand Up @@ -1579,6 +1599,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
if (fast_forward == FF_ONLY)
die(_("Not possible to fast-forward, aborting."));

if (autostash)
create_autostash(the_repository,
git_path_merge_autostash(the_repository),
"merge");

/* We are going to make a new commit. */
git_committer_info(IDENT_STRICT);

Expand Down
3 changes: 1 addition & 2 deletions builtin/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
{ OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
N_("GPG-sign commits"),
PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
OPT_BOOL(0, "autostash", &options.autostash,
N_("automatically stash/stash pop before and after")),
OPT_AUTOSTASH(&options.autostash),
OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
N_("add exec lines after each commit of the "
"editable list")),
Expand Down
1 change: 1 addition & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,6 @@ int parse_opt_passthru_argv(const struct option *, const char *, int);
#define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
#define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file"))
#define OPT_PATHSPEC_FILE_NUL(v) OPT_BOOL(0, "pathspec-file-nul", v, N_("with --pathspec-from-file, pathspec elements are separated with NUL character"))
#define OPT_AUTOSTASH(v) OPT_BOOL(0, "autostash", v, N_("automatically stash/stash pop before and after"))

#endif
1 change: 1 addition & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,5 +1535,6 @@ REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
REPO_GIT_PATH_FUNC(merge_autostash, "MERGE_AUTOSTASH")
REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
REPO_GIT_PATH_FUNC(shallow, "shallow")
4 changes: 3 additions & 1 deletion path.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ struct path_cache {
const char *merge_rr;
const char *merge_mode;
const char *merge_head;
const char *merge_autostash;
const char *fetch_head;
const char *shallow;
};

#define PATH_CACHE_INIT { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
#define PATH_CACHE_INIT { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }

const char *git_path_cherry_pick_head(struct repository *r);
const char *git_path_revert_head(struct repository *r);
Expand All @@ -190,6 +191,7 @@ const char *git_path_merge_msg(struct repository *r);
const char *git_path_merge_rr(struct repository *r);
const char *git_path_merge_mode(struct repository *r);
const char *git_path_merge_head(struct repository *r);
const char *git_path_merge_autostash(struct repository *r);
const char *git_path_fetch_head(struct repository *r);
const char *git_path_shallow(struct repository *r);

Expand Down
22 changes: 22 additions & 0 deletions t/t3033-merge-toplevel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,33 @@ test_expect_success 'refuse two-project merge by default' '
test_must_fail git merge five
'

test_expect_success 'refuse two-project merge by default, quit before --autostash happens' '
t3033_reset &&
git reset --hard four &&
echo change >>one.t &&
git diff >expect &&
test_must_fail git merge --autostash five 2>err &&
test_i18ngrep ! "stash" err &&
git diff >actual &&
test_cmp expect actual
'

test_expect_success 'two-project merge with --allow-unrelated-histories' '
t3033_reset &&
git reset --hard four &&
git merge --allow-unrelated-histories five &&
git diff --exit-code five
'

test_expect_success 'two-project merge with --allow-unrelated-histories with --autostash' '
t3033_reset &&
git reset --hard four &&
echo change >>one.t &&
git diff one.t >expect &&
git merge --allow-unrelated-histories --autostash five 2>err &&
test_i18ngrep "Applied autostash." err &&
git diff one.t >actual &&
test_cmp expect actual
'

test_done
132 changes: 132 additions & 0 deletions t/t7600-merge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ Testing basic merge operations/option parsing.
. "$TEST_DIRECTORY"/lib-gpg.sh

test_write_lines 1 2 3 4 5 6 7 8 9 >file
cp file file.orig
test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z

create_merge_msgs () {
Expand Down Expand Up @@ -675,6 +679,134 @@ test_expect_success 'refresh the index before merging' '
git merge c3
'

test_expect_success 'merge with --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git merge --autostash c2 2>err &&
test_i18ngrep "Applied autostash." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-5 merge-result &&
test_cmp result.1-5-9 file
'

test_expect_success 'merge with merge.autoStash' '
test_config merge.autoStash true &&
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git merge c2 2>err &&
test_i18ngrep "Applied autostash." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-5 merge-result &&
test_cmp result.1-5-9 file
'

test_expect_success 'fast-forward merge with --autostash' '
git reset --hard c0 &&
git merge-file file file.orig file.5 &&
git merge --autostash c1 2>err &&
test_i18ngrep "Applied autostash." err &&
test_cmp result.1-5 file
'

test_expect_success 'octopus merge with --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.3 &&
git merge --autostash c2 c3 2>err &&
test_i18ngrep "Applied autostash." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-5-9 merge-result &&
test_cmp result.1-3-5-9 file
'

test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
git reset --hard c3 &&
cp file.1 file &&
test_must_fail git merge --autostash c7 &&
git merge --abort 2>err &&
test_i18ngrep "Applied autostash." err &&
test_cmp file.1 file
'

test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git diff >expect &&
git merge --no-commit --autostash c2 &&
git stash show -p MERGE_AUTOSTASH >actual &&
test_cmp expect actual &&
git commit 2>err &&
test_i18ngrep "Applied autostash." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-5 merge-result &&
test_cmp result.1-5-9 file
'

test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git diff >expect &&
git merge --no-commit --autostash c2 &&
git stash show -p MERGE_AUTOSTASH >actual &&
test_cmp expect actual &&
git merge --continue 2>err &&
test_i18ngrep "Applied autostash." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-5 merge-result &&
test_cmp result.1-5-9 file
'

test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git diff >expect &&
git merge --no-commit --autostash c2 &&
git stash show -p MERGE_AUTOSTASH >actual &&
test_cmp expect actual &&
git merge --abort 2>err &&
test_i18ngrep "Applied autostash." err &&
git diff >actual &&
test_cmp expect actual
'

test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git diff >expect &&
git merge --no-commit --autostash c2 &&
git stash show -p MERGE_AUTOSTASH >actual &&
test_cmp expect actual &&
git reset --hard 2>err &&
test_i18ngrep "Autostash exists; creating a new stash entry." err &&
git diff --exit-code
'

test_expect_success 'quit merge with --no-commit and --autostash' '
git reset --hard c1 &&
git merge-file file file.orig file.9 &&
git diff >expect &&
git merge --no-commit --autostash c2 &&
git stash show -p MERGE_AUTOSTASH >actual &&
test_cmp expect actual &&
git diff HEAD >expect &&
git merge --quit 2>err &&
test_i18ngrep "Autostash exists; creating a new stash entry." err &&
git diff HEAD >actual &&
test_cmp expect actual
'

test_expect_success 'merge with conflicted --autostash changes' '
git reset --hard c1 &&
git merge-file file file.orig file.9y &&
git diff >expect &&
test_when_finished "test_might_fail git stash drop" &&
git merge --autostash c3 2>err &&
test_i18ngrep "Applying autostash resulted in conflicts." err &&
git show HEAD:file >merge-result &&
test_cmp result.1-9 merge-result &&
git stash show -p >actual &&
test_cmp expect actual
'

cat >expected.branch <<\EOF
Merge branch 'c5-branch' (early part)
EOF
Expand Down

0 comments on commit a03b555

Please sign in to comment.