Skip to content

Commit c4a38d1

Browse files
committed
Merge branch 'nd/merge-quit'
"git merge" learned "--quit" option that cleans up the in-progress merge while leaving the working tree and the index still in a mess. * nd/merge-quit: merge: add --quit merge: remove drop_save() in favor of remove_merge_branch_state()
2 parents 89d1b57 + f3f8311 commit c4a38d1

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

Documentation/git-merge.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ commit or stash your changes before running 'git merge'.
100100
'git merge --abort' is equivalent to 'git reset --merge' when
101101
`MERGE_HEAD` is present.
102102

103+
--quit::
104+
Forget about the current merge in progress. Leave the index
105+
and the working tree as-is.
106+
103107
--continue::
104108
After a 'git merge' stops due to conflicts you can conclude the
105109
merge by running 'git merge --continue' (see "HOW TO RESOLVE

branch.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,19 @@ void create_branch(struct repository *r,
338338
free(real_ref);
339339
}
340340

341-
void remove_branch_state(struct repository *r)
341+
void remove_merge_branch_state(struct repository *r)
342342
{
343-
sequencer_post_commit_cleanup(r);
344343
unlink(git_path_merge_head(r));
345344
unlink(git_path_merge_rr(r));
346345
unlink(git_path_merge_msg(r));
347346
unlink(git_path_merge_mode(r));
347+
}
348+
349+
void remove_branch_state(struct repository *r)
350+
{
351+
sequencer_post_commit_cleanup(r);
348352
unlink(git_path_squash_msg(r));
353+
remove_merge_branch_state(r);
349354
}
350355

351356
void die_if_checked_out(const char *branch, int ignore_current_worktree)

branch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ int validate_branchname(const char *name, struct strbuf *ref);
6060
*/
6161
int validate_new_branchname(const char *name, struct strbuf *ref, int force);
6262

63+
/*
64+
* Remove information about the merge state on the current
65+
* branch. (E.g., MERGE_HEAD)
66+
*/
67+
void remove_merge_branch_state(struct repository *r);
68+
6369
/*
6470
* Remove information about the state of working on the current
6571
* branch. (E.g., MERGE_HEAD)

builtin/merge.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "packfile.h"
3838
#include "tag.h"
3939
#include "alias.h"
40+
#include "branch.h"
4041
#include "commit-reach.h"
4142
#include "wt-status.h"
4243

@@ -73,6 +74,7 @@ static int option_renormalize;
7374
static int verbosity;
7475
static int allow_rerere_auto;
7576
static int abort_current_merge;
77+
static int quit_current_merge;
7678
static int continue_current_merge;
7779
static int allow_unrelated_histories;
7880
static int show_progress = -1;
@@ -274,6 +276,8 @@ static struct option builtin_merge_options[] = {
274276
OPT__VERBOSITY(&verbosity),
275277
OPT_BOOL(0, "abort", &abort_current_merge,
276278
N_("abort the current in-progress merge")),
279+
OPT_BOOL(0, "quit", &quit_current_merge,
280+
N_("--abort but leave index and working tree alone")),
277281
OPT_BOOL(0, "continue", &continue_current_merge,
278282
N_("continue the current in-progress merge")),
279283
OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
@@ -287,14 +291,6 @@ static struct option builtin_merge_options[] = {
287291
OPT_END()
288292
};
289293

290-
/* Cleans up metadata that is uninteresting after a succeeded merge. */
291-
static void drop_save(void)
292-
{
293-
unlink(git_path_merge_head(the_repository));
294-
unlink(git_path_merge_msg(the_repository));
295-
unlink(git_path_merge_mode(the_repository));
296-
}
297-
298294
static int save_state(struct object_id *stash)
299295
{
300296
int len;
@@ -388,7 +384,7 @@ static void finish_up_to_date(const char *msg)
388384
{
389385
if (verbosity >= 0)
390386
printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
391-
drop_save();
387+
remove_merge_branch_state(the_repository);
392388
}
393389

394390
static void squash_message(struct commit *commit, struct commit_list *remoteheads)
@@ -881,7 +877,7 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
881877
&result_commit, NULL, sign_commit))
882878
die(_("failed to write commit object"));
883879
finish(head, remoteheads, &result_commit, "In-index merge");
884-
drop_save();
880+
remove_merge_branch_state(the_repository);
885881
return 0;
886882
}
887883

@@ -907,7 +903,7 @@ static int finish_automerge(struct commit *head,
907903
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
908904
finish(head, remoteheads, &result_commit, buf.buf);
909905
strbuf_release(&buf);
910-
drop_save();
906+
remove_merge_branch_state(the_repository);
911907
return 0;
912908
}
913909

@@ -1289,6 +1285,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12891285
goto done;
12901286
}
12911287

1288+
if (quit_current_merge) {
1289+
if (orig_argc != 2)
1290+
usage_msg_opt(_("--quit expects no arguments"),
1291+
builtin_merge_usage,
1292+
builtin_merge_options);
1293+
1294+
remove_merge_branch_state(the_repository);
1295+
goto done;
1296+
}
1297+
12921298
if (continue_current_merge) {
12931299
int nargc = 1;
12941300
const char *nargv[] = {"commit", NULL};
@@ -1495,7 +1501,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14951501
}
14961502

14971503
finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
1498-
drop_save();
1504+
remove_merge_branch_state(the_repository);
14991505
goto done;
15001506
} else if (!remoteheads->next && common->next)
15011507
;

t/t7600-merge.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,30 @@ test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue'
867867
verify_parents $c0 $c1
868868
'
869869

870+
test_expect_success 'merge --quit' '
871+
git init merge-quit &&
872+
(
873+
cd merge-quit &&
874+
test_commit base &&
875+
echo one >>base.t &&
876+
git commit -am one &&
877+
git branch one &&
878+
git checkout base &&
879+
echo two >>base.t &&
880+
git commit -am two &&
881+
test_must_fail git -c rerere.enabled=true merge one &&
882+
test_path_is_file .git/MERGE_HEAD &&
883+
test_path_is_file .git/MERGE_MODE &&
884+
test_path_is_file .git/MERGE_MSG &&
885+
git rerere status >rerere.before &&
886+
git merge --quit &&
887+
test_path_is_missing .git/MERGE_HEAD &&
888+
test_path_is_missing .git/MERGE_MODE &&
889+
test_path_is_missing .git/MERGE_MSG &&
890+
git rerere status >rerere.after &&
891+
test_must_be_empty rerere.after &&
892+
! test_cmp rerere.after rerere.before
893+
)
894+
'
895+
870896
test_done

0 commit comments

Comments
 (0)