Skip to content

Commit 0a5325f

Browse files
committed
Merge branch 'nd/merge-quit' into jch
"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 af5e169 + 324d237 commit 0a5325f

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

Documentation/git-merge.txt

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

108+
--quit::
109+
Forget about the current merge in progress. Leave the index
110+
and the working tree as-is.
111+
108112
--continue::
109113
After a 'git merge' stops due to conflicts you can conclude the
110114
merge by running 'git merge --continue' (see "HOW TO RESOLVE

branch.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,21 @@ void create_branch(struct repository *r,
338338
free(real_ref);
339339
}
340340

341-
void remove_branch_state(struct repository *r, int verbose)
341+
void remove_merge_branch_state(struct repository *r)
342342
{
343-
sequencer_post_commit_cleanup(r, verbose);
344-
if (!unlink(git_path_merge_head(r)) && verbose)
345-
warning(_("cancelling a merge in progress"));
343+
unlink(git_path_merge_head(r));
346344
unlink(git_path_merge_rr(r));
347345
unlink(git_path_merge_msg(r));
348346
unlink(git_path_merge_mode(r));
347+
}
348+
349+
void remove_branch_state(struct repository *r, int verbose)
350+
{
351+
sequencer_post_commit_cleanup(r, verbose);
352+
unlink(git_path_cherry_pick_head(r));
353+
unlink(git_path_revert_head(r));
349354
unlink(git_path_squash_msg(r));
355+
remove_merge_branch_state(r);
350356
}
351357

352358
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,18 @@ 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 reset --hard side &&
872+
test_must_fail git -c rerere.enabled=true merge master &&
873+
test_path_is_file .git/MERGE_HEAD &&
874+
test_path_is_file .git/MERGE_MODE &&
875+
test_path_is_file .git/MERGE_MSG &&
876+
test_path_is_file .git/MERGE_RR &&
877+
git merge --quit &&
878+
test_path_is_missing .git/MERGE_HEAD &&
879+
test_path_is_missing .git/MERGE_MODE &&
880+
test_path_is_missing .git/MERGE_MSG &&
881+
test_path_is_missing .git/MERGE_RR
882+
'
883+
870884
test_done

0 commit comments

Comments
 (0)