@matheusbsbs / matheus-bagliano / commits / a03b55530a
Fork 0

merge: teach --autostash option

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>

Denton Liu committed Apr 7, 2020 at 10:28 UTC a03b55530ab844974b7058042a015fcfcd9e7a53
12 files changed +214 -6
Documentation/config/merge.txt
+10
@@ -70,6 +70,16 @@ merge.stat::
70 Whether to print the diffstat between ORIG_HEAD and the merge result
71 at the end of the merge. True by default.
72
73 +merge.autoStash::
74 + When set to true, automatically create a temporary stash entry
75 + before the operation begins, and apply it after the operation
76 + ends. This means that you can run merge on a dirty worktree.
77 + However, use with care: the final stash application after a
78 + successful merge might result in non-trivial conflicts.
79 + This option can be overridden by the `--no-autostash` and
80 + `--autostash` options of linkgit:git-merge[1].
81 + Defaults to false.
82 +
83 merge.tool::
84 Controls which merge tool is used by linkgit:git-mergetool[1].
85 The list below shows the valid built-in values.
Documentation/git-merge.txt
+8 -3
@@ -94,7 +94,8 @@ will be appended to the specified message.
94
95 --abort::
96 Abort the current conflict resolution process, and
97 - try to reconstruct the pre-merge state.
97 + try to reconstruct the pre-merge state. If an autostash entry is
98 + present, apply it to the worktree.
99 +
100 If there were uncommitted worktree changes present when the merge
101 started, 'git merge --abort' will in some cases be unable to
@@ -102,11 +103,15 @@ reconstruct these changes. It is therefore recommended to always
103 commit or stash your changes before running 'git merge'.
104 +
105 'git merge --abort' is equivalent to 'git reset --merge' when
105 -`MERGE_HEAD` is present.
106 +`MERGE_HEAD` is present unless `MERGE_AUTOSTASH` is also present in
107 +which case 'git merge --abort' applies the stash entry to the worktree
108 +whereas 'git reset --merge' will save the stashed changes in the stash
109 +reflog.
110
111 --quit::
112 Forget about the current merge in progress. Leave the index
109 - and the working tree as-is.
113 + and the working tree as-is. If `MERGE_AUTOSTASH` is present, the
114 + stash entry will be saved to the stash reflog.
115
116 --continue::
117 After a 'git merge' stops due to conflicts you can conclude the
Documentation/merge-options.txt
+8
@@ -155,6 +155,14 @@ ifndef::git-pull[]
155 Note that not all merge strategies may support progress
156 reporting.
157
158 +--autostash::
159 +--no-autostash::
160 + Automatically create a temporary stash entry before the operation
161 + begins, and apply it after the operation ends. This means
162 + that you can run the operation on a dirty worktree. However, use
163 + with care: the final stash application after a successful
164 + merge might result in non-trivial conflicts.
165 +
166 endif::git-pull[]
167
168 --allow-unrelated-histories::
branch.c
+1
@@ -344,6 +344,7 @@ void remove_merge_branch_state(struct repository *r)
344 unlink(git_path_merge_rr(r));
345 unlink(git_path_merge_msg(r));
346 unlink(git_path_merge_mode(r));
347 + save_autostash(git_path_merge_autostash(r));
348 }
349
350 void remove_branch_state(struct repository *r, int verbose)
builtin/commit.c
+2
@@ -1713,6 +1713,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1713 &oid, flags);
1714 }
1715
1716 + apply_autostash(git_path_merge_autostash(the_repository));
1717 +
1718 UNLEAK(err);
1719 UNLEAK(sb);
1720 return 0;
builtin/merge.c
+25
@@ -82,6 +82,7 @@ static int show_progress = -1;
82 static int default_to_upstream = 1;
83 static int signoff;
84 static const char *sign_commit;
85 +static int autostash;
86 static int no_verify;
87
88 static struct strategy all_strategy[] = {
@@ -286,6 +287,7 @@ static struct option builtin_merge_options[] = {
287 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
288 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
289 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
290 + OPT_AUTOSTASH(&autostash),
291 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
292 OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
293 OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
@@ -475,6 +477,7 @@ static void finish(struct commit *head_commit,
477 /* Run a post-merge hook */
478 run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
479
480 + apply_autostash(git_path_merge_autostash(the_repository));
481 strbuf_release(&reflog_message);
482 }
483
@@ -634,6 +637,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
637 return 0;
638 } else if (!strcmp(k, "gpg.mintrustlevel")) {
639 check_trust_level = 0;
640 + } else if (!strcmp(k, "merge.autostash")) {
641 + autostash = git_config_bool(k, v);
642 + return 0;
643 }
644
645 status = fmt_merge_msg_config(k, v, cb);
@@ -1281,6 +1287,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1287 if (abort_current_merge) {
1288 int nargc = 2;
1289 const char *nargv[] = {"reset", "--merge", NULL};
1290 + struct strbuf stash_oid = STRBUF_INIT;
1291
1292 if (orig_argc != 2)
1293 usage_msg_opt(_("--abort expects no arguments"),
@@ -1289,8 +1296,17 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1296 if (!file_exists(git_path_merge_head(the_repository)))
1297 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1298
1299 + if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository),
1300 + READ_ONELINER_SKIP_IF_EMPTY))
1301 + unlink(git_path_merge_autostash(the_repository));
1302 +
1303 /* Invoke 'git reset --merge' */
1304 ret = cmd_reset(nargc, nargv, prefix);
1305 +
1306 + if (stash_oid.len)
1307 + apply_autostash_oid(stash_oid.buf);
1308 +
1309 + strbuf_release(&stash_oid);
1310 goto done;
1311 }
1312
@@ -1513,6 +1529,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1529 goto done;
1530 }
1531
1532 + if (autostash)
1533 + create_autostash(the_repository,
1534 + git_path_merge_autostash(the_repository),
1535 + "merge");
1536 if (checkout_fast_forward(the_repository,
1537 &head_commit->object.oid,
1538 &commit->object.oid,
@@ -1579,6 +1599,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1599 if (fast_forward == FF_ONLY)
1600 die(_("Not possible to fast-forward, aborting."));
1601
1602 + if (autostash)
1603 + create_autostash(the_repository,
1604 + git_path_merge_autostash(the_repository),
1605 + "merge");
1606 +
1607 /* We are going to make a new commit. */
1608 git_committer_info(IDENT_STRICT);
1609
builtin/rebase.c
+1 -2
@@ -1376,8 +1376,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1376 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1377 N_("GPG-sign commits"),
1378 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1379 - OPT_BOOL(0, "autostash", &options.autostash,
1380 - N_("automatically stash/stash pop before and after")),
1379 + OPT_AUTOSTASH(&options.autostash),
1380 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1381 N_("add exec lines after each commit of the "
1382 "editable list")),
parse-options.h
+1
@@ -336,5 +336,6 @@ int parse_opt_passthru_argv(const struct option *, const char *, int);
336 #define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
337 #define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file"))
338 #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"))
339 +#define OPT_AUTOSTASH(v) OPT_BOOL(0, "autostash", v, N_("automatically stash/stash pop before and after"))
340
341 #endif
path.c
+1
@@ -1535,5 +1535,6 @@ REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
1535 REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
1536 REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
1537 REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
1538 +REPO_GIT_PATH_FUNC(merge_autostash, "MERGE_AUTOSTASH")
1539 REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
1540 REPO_GIT_PATH_FUNC(shallow, "shallow")
path.h
+3 -1
@@ -177,11 +177,12 @@ struct path_cache {
177 const char *merge_rr;
178 const char *merge_mode;
179 const char *merge_head;
180 + const char *merge_autostash;
181 const char *fetch_head;
182 const char *shallow;
183 };
184
184 -#define PATH_CACHE_INIT { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
185 +#define PATH_CACHE_INIT { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
186
187 const char *git_path_cherry_pick_head(struct repository *r);
188 const char *git_path_revert_head(struct repository *r);
@@ -190,6 +191,7 @@ const char *git_path_merge_msg(struct repository *r);
191 const char *git_path_merge_rr(struct repository *r);
192 const char *git_path_merge_mode(struct repository *r);
193 const char *git_path_merge_head(struct repository *r);
194 +const char *git_path_merge_autostash(struct repository *r);
195 const char *git_path_fetch_head(struct repository *r);
196 const char *git_path_shallow(struct repository *r);
197
t/t3033-merge-toplevel.sh
+22
@@ -142,6 +142,17 @@ test_expect_success 'refuse two-project merge by default' '
142 test_must_fail git merge five
143 '
144
145 +test_expect_success 'refuse two-project merge by default, quit before --autostash happens' '
146 + t3033_reset &&
147 + git reset --hard four &&
148 + echo change >>one.t &&
149 + git diff >expect &&
150 + test_must_fail git merge --autostash five 2>err &&
151 + test_i18ngrep ! "stash" err &&
152 + git diff >actual &&
153 + test_cmp expect actual
154 +'
155 +
156 test_expect_success 'two-project merge with --allow-unrelated-histories' '
157 t3033_reset &&
158 git reset --hard four &&
@@ -149,4 +160,15 @@ test_expect_success 'two-project merge with --allow-unrelated-histories' '
160 git diff --exit-code five
161 '
162
163 +test_expect_success 'two-project merge with --allow-unrelated-histories with --autostash' '
164 + t3033_reset &&
165 + git reset --hard four &&
166 + echo change >>one.t &&
167 + git diff one.t >expect &&
168 + git merge --allow-unrelated-histories --autostash five 2>err &&
169 + test_i18ngrep "Applied autostash." err &&
170 + git diff one.t >actual &&
171 + test_cmp expect actual
172 +'
173 +
174 test_done
t/t7600-merge.sh
+132
@@ -30,13 +30,17 @@ Testing basic merge operations/option parsing.
30 . "$TEST_DIRECTORY"/lib-gpg.sh
31
32 test_write_lines 1 2 3 4 5 6 7 8 9 >file
33 +cp file file.orig
34 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
35 +test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
36 test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
37 test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
38 test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
39 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
40 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
41 +test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
42 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
43 +test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
44 test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
45
46 create_merge_msgs () {
@@ -675,6 +679,134 @@ test_expect_success 'refresh the index before merging' '
679 git merge c3
680 '
681
682 +test_expect_success 'merge with --autostash' '
683 + git reset --hard c1 &&
684 + git merge-file file file.orig file.9 &&
685 + git merge --autostash c2 2>err &&
686 + test_i18ngrep "Applied autostash." err &&
687 + git show HEAD:file >merge-result &&
688 + test_cmp result.1-5 merge-result &&
689 + test_cmp result.1-5-9 file
690 +'
691 +
692 +test_expect_success 'merge with merge.autoStash' '
693 + test_config merge.autoStash true &&
694 + git reset --hard c1 &&
695 + git merge-file file file.orig file.9 &&
696 + git merge c2 2>err &&
697 + test_i18ngrep "Applied autostash." err &&
698 + git show HEAD:file >merge-result &&
699 + test_cmp result.1-5 merge-result &&
700 + test_cmp result.1-5-9 file
701 +'
702 +
703 +test_expect_success 'fast-forward merge with --autostash' '
704 + git reset --hard c0 &&
705 + git merge-file file file.orig file.5 &&
706 + git merge --autostash c1 2>err &&
707 + test_i18ngrep "Applied autostash." err &&
708 + test_cmp result.1-5 file
709 +'
710 +
711 +test_expect_success 'octopus merge with --autostash' '
712 + git reset --hard c1 &&
713 + git merge-file file file.orig file.3 &&
714 + git merge --autostash c2 c3 2>err &&
715 + test_i18ngrep "Applied autostash." err &&
716 + git show HEAD:file >merge-result &&
717 + test_cmp result.1-5-9 merge-result &&
718 + test_cmp result.1-3-5-9 file
719 +'
720 +
721 +test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
722 + git reset --hard c3 &&
723 + cp file.1 file &&
724 + test_must_fail git merge --autostash c7 &&
725 + git merge --abort 2>err &&
726 + test_i18ngrep "Applied autostash." err &&
727 + test_cmp file.1 file
728 +'
729 +
730 +test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
731 + git reset --hard c1 &&
732 + git merge-file file file.orig file.9 &&
733 + git diff >expect &&
734 + git merge --no-commit --autostash c2 &&
735 + git stash show -p MERGE_AUTOSTASH >actual &&
736 + test_cmp expect actual &&
737 + git commit 2>err &&
738 + test_i18ngrep "Applied autostash." err &&
739 + git show HEAD:file >merge-result &&
740 + test_cmp result.1-5 merge-result &&
741 + test_cmp result.1-5-9 file
742 +'
743 +
744 +test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
745 + git reset --hard c1 &&
746 + git merge-file file file.orig file.9 &&
747 + git diff >expect &&
748 + git merge --no-commit --autostash c2 &&
749 + git stash show -p MERGE_AUTOSTASH >actual &&
750 + test_cmp expect actual &&
751 + git merge --continue 2>err &&
752 + test_i18ngrep "Applied autostash." err &&
753 + git show HEAD:file >merge-result &&
754 + test_cmp result.1-5 merge-result &&
755 + test_cmp result.1-5-9 file
756 +'
757 +
758 +test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
759 + git reset --hard c1 &&
760 + git merge-file file file.orig file.9 &&
761 + git diff >expect &&
762 + git merge --no-commit --autostash c2 &&
763 + git stash show -p MERGE_AUTOSTASH >actual &&
764 + test_cmp expect actual &&
765 + git merge --abort 2>err &&
766 + test_i18ngrep "Applied autostash." err &&
767 + git diff >actual &&
768 + test_cmp expect actual
769 +'
770 +
771 +test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
772 + git reset --hard c1 &&
773 + git merge-file file file.orig file.9 &&
774 + git diff >expect &&
775 + git merge --no-commit --autostash c2 &&
776 + git stash show -p MERGE_AUTOSTASH >actual &&
777 + test_cmp expect actual &&
778 + git reset --hard 2>err &&
779 + test_i18ngrep "Autostash exists; creating a new stash entry." err &&
780 + git diff --exit-code
781 +'
782 +
783 +test_expect_success 'quit merge with --no-commit and --autostash' '
784 + git reset --hard c1 &&
785 + git merge-file file file.orig file.9 &&
786 + git diff >expect &&
787 + git merge --no-commit --autostash c2 &&
788 + git stash show -p MERGE_AUTOSTASH >actual &&
789 + test_cmp expect actual &&
790 + git diff HEAD >expect &&
791 + git merge --quit 2>err &&
792 + test_i18ngrep "Autostash exists; creating a new stash entry." err &&
793 + git diff HEAD >actual &&
794 + test_cmp expect actual
795 +'
796 +
797 +test_expect_success 'merge with conflicted --autostash changes' '
798 + git reset --hard c1 &&
799 + git merge-file file file.orig file.9y &&
800 + git diff >expect &&
801 + test_when_finished "test_might_fail git stash drop" &&
802 + git merge --autostash c3 2>err &&
803 + test_i18ngrep "Applying autostash resulted in conflicts." err &&
804 + git show HEAD:file >merge-result &&
805 + test_cmp result.1-9 merge-result &&
806 + git stash show -p >actual &&
807 + test_cmp expect actual
808 +'
809 +
810 cat >expected.branch <<\EOF
811 Merge branch 'c5-branch' (early part)
812 EOF