Skip to content

Commit f00abe6

Browse files
eantoranzgitster
authored andcommitted
builtin/merge.c - cleanup of code in for-cycle that tests strategies
The cmd_merge() function has a loop that tries different merge strategies in turn, and stops when a strategy gets a clean merge, while keeping the "best" conflicted merge so far. Make the loop easier to follow by moving the code around, ensuring that there is only one "break" in the loop where an automerge succeeds. Also group the actions that are performed after an automerge succeeds together to a single location, outside and after the loop. Signed-off-by: Edmundo Carmona Antoranz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8dca754 commit f00abe6

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

builtin/merge.c

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ static int finish_automerge(struct commit *head,
892892
struct strbuf buf = STRBUF_INIT;
893893
struct object_id result_commit;
894894

895+
write_tree_trivial(result_tree);
895896
free_commit_list(common);
896897
parents = remoteheads;
897898
if (!head_subsumed || fast_forward == FF_NO)
@@ -1586,8 +1587,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15861587
save_state(&stash))
15871588
oidclr(&stash);
15881589

1589-
for (i = 0; i < use_strategies_nr; i++) {
1590-
int ret;
1590+
for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
1591+
int ret, cnt;
15911592
if (i) {
15921593
printf(_("Rewinding the tree to pristine...\n"));
15931594
restore_state(&head_commit->object.oid, &stash);
@@ -1604,40 +1605,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
16041605
ret = try_merge_strategy(use_strategies[i]->name,
16051606
common, remoteheads,
16061607
head_commit);
1607-
if (!option_commit && !ret) {
1608-
merge_was_ok = 1;
1609-
/*
1610-
* This is necessary here just to avoid writing
1611-
* the tree, but later we will *not* exit with
1612-
* status code 1 because merge_was_ok is set.
1613-
*/
1614-
ret = 1;
1615-
}
1616-
1617-
if (ret) {
1618-
/*
1619-
* The backend exits with 1 when conflicts are
1620-
* left to be resolved, with 2 when it does not
1621-
* handle the given merge at all.
1622-
*/
1623-
if (ret == 1) {
1624-
int cnt = evaluate_result();
1625-
1626-
if (best_cnt <= 0 || cnt <= best_cnt) {
1627-
best_strategy = use_strategies[i]->name;
1628-
best_cnt = cnt;
1608+
/*
1609+
* The backend exits with 1 when conflicts are
1610+
* left to be resolved, with 2 when it does not
1611+
* handle the given merge at all.
1612+
*/
1613+
if (ret < 2) {
1614+
if (!ret) {
1615+
if (option_commit) {
1616+
/* Automerge succeeded. */
1617+
automerge_was_ok = 1;
1618+
break;
16291619
}
1620+
merge_was_ok = 1;
1621+
}
1622+
cnt = evaluate_result();
1623+
if (best_cnt <= 0 || cnt <= best_cnt) {
1624+
best_strategy = use_strategies[i]->name;
1625+
best_cnt = cnt;
16301626
}
1631-
if (merge_was_ok)
1632-
break;
1633-
else
1634-
continue;
16351627
}
1636-
1637-
/* Automerge succeeded. */
1638-
write_tree_trivial(&result_tree);
1639-
automerge_was_ok = 1;
1640-
break;
16411628
}
16421629

16431630
/*

0 commit comments

Comments
 (0)