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 <eantoranz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Edmundo Carmona Antoranz committed Jul 8, 2019 at 21:15 UTC f00abe6ae05ac67aa3a22469a1d8f84defd1df6c
1 file changed +20 -33
builtin/merge.c
+20 -33
@@ -892,6 +892,7 @@ static int finish_automerge(struct commit *head,
892 struct strbuf buf = STRBUF_INIT;
893 struct object_id result_commit;
894
895 + write_tree_trivial(result_tree);
896 free_commit_list(common);
897 parents = remoteheads;
898 if (!head_subsumed || fast_forward == FF_NO)
@@ -1586,8 +1587,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1587 save_state(&stash))
1588 oidclr(&stash);
1589
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;
1592 if (i) {
1593 printf(_("Rewinding the tree to pristine...\n"));
1594 restore_state(&head_commit->object.oid, &stash);
@@ -1604,40 +1605,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1605 ret = try_merge_strategy(use_strategies[i]->name,
1606 common, remoteheads,
1607 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;
1619 }
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;
1626 }
1631 - if (merge_was_ok)
1632 - break;
1633 - else
1634 - continue;
1627 }
1636 -
1637 - /* Automerge succeeded. */
1638 - write_tree_trivial(&result_tree);
1639 - automerge_was_ok = 1;
1640 - break;
1628 }
1629
1630 /*