Subject: [PATCH v4 0/7] Fix merge restore state

From: "Elijah Newren via GitGitGadget" <[email-masked]>
Date: Fri, 22 Jul 2022 05:15:35 +0000

This started as a simple series to fix restore_state() in builtin/merge.c,
fixing an issue reported by ZheNing Hu[3]. It's grown so much it's hard to
call it simple. Anyway...

Changes since v3:

 * Removed some accidental &nbsp; characters from a commit message
 * Made use of the error() function to simplify the first patch

[1]
https://lore.kernel.org/git/[email-masked]/

Elijah Newren (7):
  merge-ort-wrappers: make printed message match the one from recursive
  merge-resolve: abort if index does not match HEAD
  merge: do not abort early if one strategy fails to handle the merge
  merge: fix save_state() to work when there are stat-dirty files
  merge: make restore_state() restore staged state too
  merge: ensure we can actually restore pre-merge state
  merge: do not exit restore_state() prematurely

 builtin/merge.c                          | 59 ++++++++++++++++++------
 git-merge-resolve.sh                     | 10 ++++
 merge-ort-wrappers.c                     |  4 +-
 t/t6402-merge-rename.sh                  |  2 +-
 t/t6424-merge-unrelated-index-changes.sh | 58 +++++++++++++++++++++++
 t/t6439-merge-co-error-msgs.sh           |  1 +
 t/t7607-merge-state.sh                   | 32 +++++++++++++
 7 files changed, 150 insertions(+), 16 deletions(-)
 create mode 100755 t/t7607-merge-state.sh

base-commit: e72d93e88cb20b06e88e6e7d81bd1dc4effe453f
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1231%2Fnewren%2Ffix-merge-restore-state-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1231/newren/fix-merge-restore-state-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1231

Range-diff vs v3:

 1:  e39b2e15ece ! 1:  bd36d16c8d9 merge-ort-wrappers: make printed message match the one from recursive
     @@ Commit message
          being processed by another function that made additional changes:
            * It added an implicit "error: " prefix
            * It added an implicit trailing newline
     -
     -    Add these things, but do so in a couple extra steps to avoid having
     -    translators need to translate another not-quite-identical string.
     +    We can get these things by making use of the error() function.
      
          Signed-off-by: Elijah Newren <[email-masked]>
      
     @@ merge-ort-wrappers.c: static int unclean(struct merge_options *opt, struct tree
       
       	if (head && repo_index_has_changes(opt->repo, head, &sb)) {
      -		fprintf(stderr, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
     -+		struct strbuf err = STRBUF_INIT;
     -+		strbuf_addstr(&err, "error: ");
     -+		strbuf_addf(&err, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
     - 		    sb.buf);
     -+		strbuf_addch(&err, '\n');
     -+		fputs(err.buf, stderr);
     -+		strbuf_release(&err);
     +-		    sb.buf);
     ++		error(_("Your local changes to the following files would be overwritten by merge:\n  %s"),
     ++		      sb.buf);
       		strbuf_release(&sb);
       		return -1;
       	}
 2:  2810dec7608 = 2:  b79f44e54b9 merge-resolve: abort if index does not match HEAD
 3:  b41853e3f99 = 3:  02930448ea1 merge: do not abort early if one strategy fails to handle the merge
 4:  64700338a28 = 4:  daf8d224160 merge: fix save_state() to work when there are stat-dirty files
 5:  91c495c770e ! 5:  f401bd5ad0d merge: make restore_state() restore staged state too
     @@ Commit message
          Unfortunately, if users had staged changes before calling `git merge`,
          builtin/merge.c could do the following:
      
     -       * stash the changes, in order to clean up after the strategies
     -       * try all the merge strategies in turn, each of which report they
     +       * stash the changes, in order to clean up after the strategies
     +       * try all the merge strategies in turn, each of which report they
               cannot function due to the index not matching HEAD
     -       * restore the changes via "git stash apply"
     +       * restore the changes via "git stash apply"
      
          But that last step would have the net effect of unstaging the user's
          changes.  Fix this by adding the "--index" option to "git stash apply".
 6:  887967c1f3f = 6:  ad5354c219c merge: ensure we can actually restore pre-merge state
 7:  81c40492a62 = 7:  6212d572604 merge: do not exit restore_state() prematurely

-- 
gitgitgadget