[PATCH STABLE] merge: respect parents order when using `graft` on a merge

Pierre-Yves David pierre-yves.david at ens-lyon.org
Mon Sep 9 11:42:27 EDT 2019

# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at octobus.net>
# Date 1568043141 -7200
#      Mon Sep 09 17:32:21 2019 +0200
# Branch stable
# Node ID 3083d5cff75906cfa379d398c57e4a4014093659
# Parent  344a086bb7647509624102ffdc6dc52bfe9beeba
# EXP-Topic issue-6141
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 3083d5cff759
merge: respect parents order when using `graft` on a merge

The previous code did not record the index of the replaced parent. It was always
using the "graft" destination as `p1`. This could switch parents order in some
situation (eg: some of the evolve evolving merge case). Recording and using the
information fixes the issue in evolve.

We are not aware of core commands calling graft in that fashion, so we could not
build a simple test case for it using core commands.

diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -2249,17 +2249,23 @@ def graft(repo, ctx, pctx, labels=None, 
                    mergeancestor=mergeancestor, labels=labels)
+    potherp1 = False
     if keepconflictparent and stats.unresolvedcount:
         pother = ctx.node()
         pother = nullid
         parents = ctx.parents()
         if keepparent and len(parents) == 2 and pctx in parents:
+            if pctx == parents[0]:
+                potherp1 = True
             pother = parents[0].node()
     with repo.dirstate.parentchange():
-        repo.setparents(repo['.'].node(), pother)
+        if potherp1:
+            repo.setparents(pother, repo['.'].node())
+        else:
+            repo.setparents(repo['.'].node(), pother)
         # fix up dirstate for copies and renames
         copies.duplicatecopies(repo, repo[None], ctx.rev(), pctx.rev())

More information about the Mercurial-devel mailing list