Subject: [PATCH v2 0/3] Miscellaneous merge fixes

From: "Elijah Newren via GitGitGadget" <[email-masked]>
Date: Tue, 23 Aug 2022 02:42:18 +0000

While working on other things, I noticed a few miscellaneous issues in
builtin/merge.c. Here's a few small fixes to address them.

Elijah Newren (3):
  merge: only apply autostash when appropriate
  merge: cleanup confusing logic for handling successful merges
  merge: small code readability improvement

 builtin/merge.c  | 25 +++++++++++++++----------
 t/t7600-merge.sh |  9 +++++++++
 2 files changed, 24 insertions(+), 10 deletions(-)

base-commit: 9bf691b78cf906751e65d65ba0c6ffdcd9a5a12c
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1331%2Fnewren%2Fmisc-merge-fixes-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1331/newren/misc-merge-fixes-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1331

Range-diff vs v1:

 1:  92840bf6378 ! 1:  610b8d089db merge: only apply autostash when appropriate
     @@ t/t7600-merge.sh: test_expect_success 'merge --squash c3 with c7' '
       	test_cmp expect actual
       '
       
     -+test_expect_success 'merge --squash --autostash conflict does not attempt to play autostash' '
     ++test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
      +	git reset --hard c3 &&
      +	>unrelated &&
      +	git add unrelated &&
 2:  5657a05e763 ! 2:  9817d4b19bc merge: avoid searching for strategies with fewer than 0 conflicts
     @@ Metadata
      Author: Elijah Newren <[email-masked]>
      
       ## Commit message ##
     -    merge: avoid searching for strategies with fewer than 0 conflicts
     +    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.
     +    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 looking for a "better" strategy.  Since it had just
     -    found a strategy with 0 conflicts though, and it is not possible to
     -    have fewer than 0 conflicts, the continuing search is guaranteed to be
     -    futile.
     +    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.
      
     -    While searching additional strategies won't cause problems other than
     -    wasting energy, it is wasteful.  Avoid searching for other strategies
     -    with fewer than 0 conflicts.
     +    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-masked]>
      
       ## builtin/merge.c ##
     +@@ builtin/merge.c: int cmd_merge(int argc, const char **argv, const char *prefix)
     + 	if (save_state(&stash))
     + 		oidclr(&stash);
     + 
     +-	for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
     ++	for (i = 0; i < use_strategies_nr; i++) {
     + 		int ret, cnt;
     + 		if (i) {
     + 			printf(_("Rewinding the tree to pristine...\n"));
      @@ builtin/merge.c: int cmd_merge(int argc, const char **argv, const char *prefix)
       		 */
       		if (ret < 2) {
       			if (!ret) {
      -				if (option_commit) {
     -+				if (option_commit)
     - 					/* Automerge succeeded. */
     - 					automerge_was_ok = 1;
     +-					/* Automerge succeeded. */
     +-					automerge_was_ok = 1;
      -					break;
      -				}
     --				merge_was_ok = 1;
     -+				else
     -+					/* Merge good, but let user commit */
     -+					merge_was_ok = 1;
      +				/*
      +				 * This strategy worked; no point in trying
      +				 * another.
      +				 */
     -+				best_strategy = wt_strategy;
     + 				merge_was_ok = 1;
     ++				best_strategy = use_strategies[i]->name;
      +				break;
       			}
       			cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
       			if (best_cnt <= 0 || cnt <= best_cnt) {
     +@@ builtin/merge.c: int cmd_merge(int argc, const char **argv, const char *prefix)
     + 	 * If we have a resulting tree, that means the strategy module
     + 	 * auto resolved the merge cleanly.
     + 	 */
     +-	if (automerge_was_ok) {
     ++	if (merge_was_ok && option_commit) {
     ++		automerge_was_ok = 1;
     + 		ret = finish_automerge(head_commit, head_subsumed,
     + 				       common, remoteheads,
     + 				       &result_tree, wt_strategy);
 -:  ----------- > 3:  88173eba0b9 merge: small code readability improvement

-- 
gitgitgadget