Skip to content

Commit 5b1d30c

Browse files
newrengitster
authored andcommitted
merge: cleanup confusing logic for handling successful merges
builtin/merge.c has a loop over the specified strategies, where if they all fail with conflicts, it picks the one with the least number of conflicts. In the codepath that finds a successful merge, if an automatic commit was wanted, the code breaks out of the above loop, which makes sense. However, if the user requested there be no automatic commit, the loop would continue. That seems weird; --no-commit should not affect the choice of merge strategy, but the code as written makes one think it does. However, since the loop itself embeds "!merge_was_ok" as a condition on continuing to loop, it actually would also exit early if --no-commit was specified, it just exited from a different location. Restructure the code slightly to make it clear that the loop will immediately exit whenever we find a merge strategy that is successful. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 795ea87 commit 5b1d30c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

builtin/merge.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
16921692
if (save_state(&stash))
16931693
oidclr(&stash);
16941694

1695-
for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
1695+
for (i = 0; i < use_strategies_nr; i++) {
16961696
int ret, cnt;
16971697
if (i) {
16981698
printf(_("Rewinding the tree to pristine...\n"));
@@ -1717,12 +1717,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17171717
*/
17181718
if (ret < 2) {
17191719
if (!ret) {
1720-
if (option_commit) {
1721-
/* Automerge succeeded. */
1722-
automerge_was_ok = 1;
1723-
break;
1724-
}
1720+
/*
1721+
* This strategy worked; no point in trying
1722+
* another.
1723+
*/
17251724
merge_was_ok = 1;
1725+
best_strategy = use_strategies[i]->name;
1726+
break;
17261727
}
17271728
cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
17281729
if (best_cnt <= 0 || cnt <= best_cnt) {
@@ -1736,7 +1737,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17361737
* If we have a resulting tree, that means the strategy module
17371738
* auto resolved the merge cleanly.
17381739
*/
1739-
if (automerge_was_ok) {
1740+
if (merge_was_ok && option_commit) {
1741+
automerge_was_ok = 1;
17401742
ret = finish_automerge(head_commit, head_subsumed,
17411743
common, remoteheads,
17421744
&result_tree, wt_strategy);

0 commit comments

Comments
 (0)