git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/4] merge: apply autostash if merge strategy fails

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 23, 2021, 19:22 UTC
Message-ID
<xmqqk0lgn8cl.fsf@gitster.g>
In-Reply-To
<e02b945ce8595a8d7b1b4ad9c382a1beeb040ed4.1627042470.git.gitgitgadget@gmail.com>
"Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> As explained in a03b55530a, and made more abvious by the tests added in
> that commit, the autostash is then applied if the merge succeeds, either
> directly or by committing (after conflict resolution or if '--no-commit'
> was given), or if the merge is aborted with 'git merge --abort'. In some
> other cases, like the user calling 'git reset --merge' or 'git merge
> --quit', the autostash is not applied, but saved in the stash list.
OK.
Show 8 quoted lines
> However, there exists a scenario that creates an autostash but does not
> apply nor save it to the stash list: if the chosen merge strategy
> completely fails to handle the merge, i.e. 'try_merge_strategy' returns
> 2.
>
> Apply the autostash in that case also. An easy way to test that is to
> try to merge more than two commits but explicitely ask for the 'recursive'
> merge strategy.

We know that the recursive that barfs when asked to merge more than one "other" histories will fail before touching the index or the working tree, but I do not think the merge-strategy protocol makes any guarantee as to what half-modified state the strategy would leave the index and the working tree in.

So this change at the first glance looks risky, but at least we have a call to restore_state() before you added an apply_autostash(). The restore_state() call is the same as the one is used between application of a strategy that has failed the same way in the loop, so we should be able to trust it to bring the state back to the pristine (i.e. after autostash removed the local changes).

I think this change makes sense, too.
(I didn't look at 1/4 and 2/4)
Thanks.
Show 37 quoted lines
> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
> ---
>  builtin/merge.c  | 1 +
>  t/t7600-merge.sh | 8 ++++++++
>  2 files changed, 9 insertions(+)
>
> diff --git a/builtin/merge.c b/builtin/merge.c
> index 788a6b0cd55..d44c14a21a3 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -1709,6 +1709,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
>  		else
>  			fprintf(stderr, _("Merge with strategy %s failed.\n"),
>  				use_strategies[0]->name);
> +		apply_autostash(git_path_merge_autostash(the_repository));
>  		ret = 2;
>  		goto done;
>  	} else if (best_strategy == wt_strategy)
> diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
> index 216113d3537..2ef39d3088e 100755
> --- a/t/t7600-merge.sh
> +++ b/t/t7600-merge.sh
> @@ -732,6 +732,14 @@ test_expect_success 'octopus merge with --autostash' '
>  	test_cmp result.1-3-5-9 file
>  '
>  
> +test_expect_success 'failed merge (exit 2) with --autostash' '
> +	git reset --hard c1 &&
> +	git merge-file file file.orig file.5 &&
> +	test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
> +	test_i18ngrep "Applied autostash." err &&
> +	test_cmp result.1-5 file
> +'
> +
>  test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
>  	git reset --hard c3 &&
>  	cp file.1 file &&
Previous: Philippe Blain via GitGitGadgetNext: Felipe Contreras
Message 10 of 11 in “[RFC] merge --autostash: apply autostash in more cases”
  1. 0/4 [RFC] merge --autostash: apply autostash in more casesPhilippe Blain via GitGitGadget, Jul 23, 2021
  2. 1/4 merge: add missing word "strategy" to a messagePhilippe Blain via GitGitGadget, Jul 23, 2021
  3. Felipe ContrerasJul 23, 2021
  4. 3/4 merge: apply autostash if fast-forward failsPhilippe Blain via GitGitGadget, Jul 23, 2021
  5. Felipe ContrerasJul 23, 2021
  6. Junio C HamanoJul 23, 2021
  7. 2/4 Documentation: define 'MERGE_AUTOSTASH'Philippe Blain via GitGitGadget, Jul 23, 2021
  8. Felipe ContrerasJul 23, 2021
  9. 4/4 merge: apply autostash if merge strategy failsPhilippe Blain via GitGitGadget, Jul 23, 2021
  10. Junio C HamanoJul 23, 2021
  11. Felipe ContrerasJul 23, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.