Patchwork [1,of,2,v9] graft: support grafting across move/copy (issue4028)

login
register
mail settings
Submitter Gábor Stefanik
Date Sept. 16, 2016, 8:45 p.m.
Message ID <63a2ea1bc3aa51caaf22.1474058718@waste.org>
Download mbox | patch
Permalink /patch/16651/
State Accepted
Headers show

Comments

Gábor Stefanik - Sept. 16, 2016, 8:45 p.m.
# HG changeset patch
# User Gábor Stefanik <gabor.stefanik@nng.com>
# Date 1474058125 -7200
#      Fri Sep 16 22:35:25 2016 +0200
# Node ID 63a2ea1bc3aa51caaf22bdfe31e7ae685ac3894f
# Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
graft: support grafting across move/copy (issue4028)

Graft performs a merge with a false common ancestor, which must be taken into
account when tracking copies. Explicitly pass the real common ancestor in this
case, and track copies between the real and false common ancestors in reverse.

With this change, when grafting a commit with a change to a file moved earlier
on the graft's source branch, the change is merged as expected into the original
(unmoved) file, rather than recreating it under its new name.
It should also make it possible to eventually enable cross-branch updates with
merge.
Yuya Nishihara - Oct. 2, 2016, 4:01 p.m.
On Fri, 16 Sep 2016 15:45:18 -0500, Gábor Stefanik wrote:
> # HG changeset patch
> # User Gábor Stefanik <gabor.stefanik@nng.com>
> # Date 1474058125 -7200
> #      Fri Sep 16 22:35:25 2016 +0200
> # Node ID 63a2ea1bc3aa51caaf22bdfe31e7ae685ac3894f
> # Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110
> graft: support grafting across move/copy (issue4028)

> diff --git a/mercurial/copies.py b/mercurial/copies.py
> --- a/mercurial/copies.py
> +++ b/mercurial/copies.py
> @@ -321,6 +321,23 @@
>      if repo.ui.configbool('experimental', 'disablecopytrace'):
>          return {}, {}, {}, {}
>  
> +    # In certain scenarios (e.g. graft, update or rebase), ca can be overridden
> +    # We still need to know a real common ancestor in this case
> +    # We can't just compute _c1.ancestor(_c2) and compare it to ca, because
> +    # there can be multiple common ancestors, e.g. in case of bidmerge.
> +    # Because our caller may not know if the revision passed in lieu of the CA
> +    # is a genuine common ancestor or not without explicitly checking it, it's
> +    # better to determine that here.
> +    cta = ca
> +    # ca.descendant(wc) and ca.descendant(ca) are False, work around that
> +    _c1 = c1.p1() if c1.rev() is None else c1
> +    _c2 = c2.p1() if c2.rev() is None else c2
> +    dirty_c1 = not (ca == _c1 or ca.descendant(_c1))
> +    dirty_c2 = not (ca == _c2 or ca.descendant(_c2))
> +    graft = dirty_c1 or dirty_c2
> +    if graft:
> +        cta = _c1.ancestor(_c2)

Can you fix ctx.descendant() to handle wctx? or maybe this could be

  dirtyc<n> = ca != ca.ancestor(c<n>)  # for n = 1, 2

and one of ca.ancestor(c<n>) would be cta. (I don't carefully investigate
criss-cross merge case, so I might be wrong.)

>      # find interesting file sets from manifests
> +    if graft:
> +        repo.ui.debug("  computing unmatched files in rotated DAG\n")
>      addedinm1 = m1.filesnotin(ma)
>      addedinm2 = m2.filesnotin(ma)
> -    u1, u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)
> +    _u1, _u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)

Can you give them better names? '_' prefix in local variables generally
means they are placeholder (unused output) variables. (Perhaps we can start
with a trivial patch that separates u1/2 and _u1/2, which will describe why
we need them.)

> +    # combine partial copy paths discovered in the previous step
> +    copyfrom, copyto = incomplete1, incomplete2
> +    if dirty_c1:
> +        copyfrom, copyto = incomplete2, incomplete1
> +    for f in copyfrom:
> +        if f in copyto:
> +            copy[copyto[f]] = copyfrom[f]
> +            del copyto[f]
> +    for f in incompletediverge:
> +        assert f not in diverge
> +        ic = incompletediverge[f]
> +        if ic[0] in copyto:
> +            diverge[f] = [copyto[ic[0]], ic[1]]

Perhaps this can be a separate function, which would avoid spilling out
temporary variables, copyfrom/to.

> +    _incomplete = _incomplete2
> +    if dirty_c1:
> +        _incomplete = _incomplete1
> +        assert _incomplete2 == {}
> +    else:
> +        assert _incomplete1 == {}
> +    for f in _diverge:
> +        assert f not in bothdiverge
> +        ic = _diverge[f]
> +        if ic[0] in _incomplete:
> +            bothdiverge[f] = [_incomplete[ic[0]], ic[1]]
> +        elif ic[0] in (m1 if dirty_c1 else m2):
> +            # backed-out rename on one side, but watch out for deleted files
> +            bothdiverge[f] = ic

This looks very similar to the previous incomplete1/2 loop. Can we make them
share the implementation?

> +def checkcopies(ctx, f, m1, m2, ca, cta, remote_ca, limit, diverge, copy,
> +                fullcopy, incomplete, incompletediverge):

nit: s/remote_ca/remoteca/ to conform to the coding style.

> @@ -460,14 +525,25 @@
>      f = the filename to check
>      m1 = the source manifest
>      m2 = the destination manifest
> -    ca = the changectx of the common ancestor
> +    ca = the changectx of the common ancestor, overridden on graft
> +    cta = topological common ancestor for graft-like scenarios
> +    remote_ca = True if ca is outside cta::m1, False otherwise

nit: you mean cta::ctx?

And it would be nice if there's a brief comment when to use ca/ma over
cta/mta.

> -    seen = set([f])
> -    for oc in getfctx(f, m1[f]).ancestors():
> +    seen = {f: [getfctx(f, m1[f])]}
> +    for oc in seen[f][0].ancestors():
>          ocr = oc.linkrev()
>          of = oc.path()
>          if of in seen:
> +            seen[of].append(oc)
>              # check limit late - grab last rename before
>              if ocr < limit:
>                  break
>              continue
> -        seen.add(of)
> +        seen[of] = [oc]

nit: maybe we only need a dict of file nodes, not a dict of file contexts?

> -        fullcopy[f] = of # remember for dir rename detection
> +        # remember for dir rename detection
> +        if backwards:
> +            fullcopy[of] = f # grafting backwards through renames
> +        else:
> +            fullcopy[f] = of
>          if of not in m2:
>              continue # no match, keep looking
>          if m2[of] == ma.get(of):
> -            break # no merge needed, quit early
> +            return # no merge needed, quit early

So we no longer set diverge[of] = [f] this case. I don't know if it was
necessary to populate 'renamedelete', but this change seems good for a
separate patch to make sure it never break anything.

> -        cr = _related(oc, c2, ca.rev())
> +        cr = _related(oc, c2, cta.rev())
>          if cr and (of == f or of == c2.path()): # non-divergent
> -            copy[f] = of
> -            of = None
> -            break
> +            if backwards:
> +                copy[of] = f
> +            elif of in ma:
> +                copy[f] = of
> +            elif remote_ca: # special case: a <- b <- a -> b "ping-pong" rename
> +                copy[of] = f
> +                del fullcopy[f]
> +                fullcopy[of] = f
> +            else: # divergence w.r.t. graft CA on one side of topological CA
> +                for sf in seen:
> +                    if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:
> +                        assert sf not in diverge
> +                        diverge[sf] = [f, of]
> +                        break
> +            return
>  
> -    if of in ma:
> -        diverge.setdefault(of, []).append(f)
> +    if of in mta:
> +        if backwards or remote_ca:
> +            incomplete[of] = f
> +        else:
> +            for sf in seen:
> +                if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:
> +                    if cta == ca:
> +                        diverge.setdefault(sf, []).append(f)
> +                    else:
> +                        incompletediverge[sf] = [of, f]
> +                    return

Can we split this to 2 or 3 patches? I guess divergence handling can be
a separate patch, for example. The point is we'll probably want to speed up
the review/feedback process. I think copy tracing is inherently complex.
Reviewing a monolithic patch about it would require a fair amount of continuous
time, which would make hard to start reviewing this series.

(I haven't read the tests and some part of incomplete* handling yet. I'll
revisit them when I found time to.)

Regards,
Gábor Stefanik - Oct. 3, 2016, 10:28 a.m.
>



--------------------------------------------------------------------------
This message, including its attachments, is confidential. For more information please read NNG's email policy here:
http://www.nng.com/emailpolicy/
By responding to this email you accept the email policy.


-----Original Message-----
> From: Yuya Nishihara [mailto:youjah@gmail.com] On Behalf Of Yuya

> Nishihara

> Sent: Sunday, October 2, 2016 6:02 PM

> To: Gábor STEFANIK <Gabor.STEFANIK@nng.com>

> Cc: mercurial-devel@mercurial-scm.org

> Subject: Re: [PATCH 1 of 2 v9] graft: support grafting across move/copy

> (issue4028)

>

> On Fri, 16 Sep 2016 15:45:18 -0500, Gábor Stefanik wrote:

> > # HG changeset patch

> > # User Gábor Stefanik <gabor.stefanik@nng.com> # Date 1474058125 -7200

> > #      Fri Sep 16 22:35:25 2016 +0200

> > # Node ID 63a2ea1bc3aa51caaf22bdfe31e7ae685ac3894f

> > # Parent  285a8c3e53f2183438f0cdbc238e4ab851d0d110

> > graft: support grafting across move/copy (issue4028)

>

> > diff --git a/mercurial/copies.py b/mercurial/copies.py

> > --- a/mercurial/copies.py

> > +++ b/mercurial/copies.py

> > @@ -321,6 +321,23 @@

> >      if repo.ui.configbool('experimental', 'disablecopytrace'):

> >          return {}, {}, {}, {}

> >

> > +    # In certain scenarios (e.g. graft, update or rebase), ca can be

> overridden

> > +    # We still need to know a real common ancestor in this case

> > +    # We can't just compute _c1.ancestor(_c2) and compare it to ca,

> because

> > +    # there can be multiple common ancestors, e.g. in case of bidmerge.

> > +    # Because our caller may not know if the revision passed in lieu of the

> CA

> > +    # is a genuine common ancestor or not without explicitly checking it, it's

> > +    # better to determine that here.

> > +    cta = ca

> > +    # ca.descendant(wc) and ca.descendant(ca) are False, work around that

> > +    _c1 = c1.p1() if c1.rev() is None else c1

> > +    _c2 = c2.p1() if c2.rev() is None else c2

> > +    dirty_c1 = not (ca == _c1 or ca.descendant(_c1))

> > +    dirty_c2 = not (ca == _c2 or ca.descendant(_c2))

> > +    graft = dirty_c1 or dirty_c2

> > +    if graft:

> > +        cta = _c1.ancestor(_c2)

>

> Can you fix ctx.descendant() to handle wctx? or maybe this could be

>

>   dirtyc<n> = ca != ca.ancestor(c<n>)  # for n = 1, 2

>

> and one of ca.ancestor(c<n>) would be cta. (I don't carefully investigate criss-

> cross merge case, so I might be wrong.)


ctx.ancestor() always returns just one common ancestor, not all possible candidates.

bidmerge uses a function called commonancestorsheads() instead (not sure where that
can be found), but a non-head common ancestor also shouldn't trigger graft logic.

So unfortunately there is no way to eliminate the use of descendant() here, nor do I feel
that there is a need to do so. descendant() is still faster than ancestor() or especially
commonancestorsheads().

>

> >      # find interesting file sets from manifests

> > +    if graft:

> > +        repo.ui.debug("  computing unmatched files in rotated DAG\n")

> >      addedinm1 = m1.filesnotin(ma)

> >      addedinm2 = m2.filesnotin(ma)

> > -    u1, u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)

> > +    _u1, _u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)

>

> Can you give them better names? '_' prefix in local variables generally means

> they are placeholder (unused output) variables. (Perhaps we can start with a

> trivial patch that separates u1/2 and _u1/2, which will describe why we need

> them.)


u1r, u2r, u1u, u2u (for rotated/unrotated)? I'm wary of using longer names, as many lines
in copies.py are already borderline for the 80-character rule, and will require reformatting
with the introduction of longer variable names.

>

> > +    # combine partial copy paths discovered in the previous step

> > +    copyfrom, copyto = incomplete1, incomplete2

> > +    if dirty_c1:

> > +        copyfrom, copyto = incomplete2, incomplete1

> > +    for f in copyfrom:

> > +        if f in copyto:

> > +            copy[copyto[f]] = copyfrom[f]

> > +            del copyto[f]

> > +    for f in incompletediverge:

> > +        assert f not in diverge

> > +        ic = incompletediverge[f]

> > +        if ic[0] in copyto:

> > +            diverge[f] = [copyto[ic[0]], ic[1]]

>

> Perhaps this can be a separate function, which would avoid spilling out

> temporary variables, copyfrom/to.


Sounds doable.

>

> > +    _incomplete = _incomplete2

> > +    if dirty_c1:

> > +        _incomplete = _incomplete1

> > +        assert _incomplete2 == {}

> > +    else:

> > +        assert _incomplete1 == {}

> > +    for f in _diverge:

> > +        assert f not in bothdiverge

> > +        ic = _diverge[f]

> > +        if ic[0] in _incomplete:

> > +            bothdiverge[f] = [_incomplete[ic[0]], ic[1]]

> > +        elif ic[0] in (m1 if dirty_c1 else m2):

> > +            # backed-out rename on one side, but watch out for deleted files

> > +            bothdiverge[f] = ic

>

> This looks very similar to the previous incomplete1/2 loop. Can we make

> them share the implementation?


Will do if it can be done without giving up the asserts.

>

> > +def checkcopies(ctx, f, m1, m2, ca, cta, remote_ca, limit, diverge, copy,

> > +                fullcopy, incomplete, incompletediverge):

>

> nit: s/remote_ca/remoteca/ to conform to the coding style.


OK.

>

> > @@ -460,14 +525,25 @@

> >      f = the filename to check

> >      m1 = the source manifest

> >      m2 = the destination manifest

> > -    ca = the changectx of the common ancestor

> > +    ca = the changectx of the common ancestor, overridden on graft

> > +    cta = topological common ancestor for graft-like scenarios

> > +    remote_ca = True if ca is outside cta::m1, False otherwise

>

> nit: you mean cta::ctx?


Right, didn't realize that ctx points to the same revision as m1.

>

> And it would be nice if there's a brief comment when to use ca/ma over

> cta/mta.

>

> > -    seen = set([f])

> > -    for oc in getfctx(f, m1[f]).ancestors():

> > +    seen = {f: [getfctx(f, m1[f])]}

> > +    for oc in seen[f][0].ancestors():

> >          ocr = oc.linkrev()

> >          of = oc.path()

> >          if of in seen:

> > +            seen[of].append(oc)

> >              # check limit late - grab last rename before

> >              if ocr < limit:

> >                  break

> >              continue

> > -        seen.add(of)

> > +        seen[of] = [oc]

>

> nit: maybe we only need a dict of file nodes, not a dict of file contexts?


how do I get a file node id from a file context? ancestors() gives us contexts.

>

> > -        fullcopy[f] = of # remember for dir rename detection

> > +        # remember for dir rename detection

> > +        if backwards:

> > +            fullcopy[of] = f # grafting backwards through renames

> > +        else:

> > +            fullcopy[f] = of

> >          if of not in m2:

> >              continue # no match, keep looking

> >          if m2[of] == ma.get(of):

> > -            break # no merge needed, quit early

> > +            return # no merge needed, quit early

>

> So we no longer set diverge[of] = [f] this case. I don't know if it was

> necessary to populate 'renamedelete', but this change seems good for a

> separate patch to make sure it never break anything.


This isn't just some optimization, it's necessary for correct behavior of the
new checkcopies. And it would break the old checkcopies if applied separately
before the main patch.

>

> > -        cr = _related(oc, c2, ca.rev())

> > +        cr = _related(oc, c2, cta.rev())

> >          if cr and (of == f or of == c2.path()): # non-divergent

> > -            copy[f] = of

> > -            of = None

> > -            break

> > +            if backwards:

> > +                copy[of] = f

> > +            elif of in ma:

> > +                copy[f] = of

> > +            elif remote_ca: # special case: a <- b <- a -> b "ping-pong" rename

> > +                copy[of] = f

> > +                del fullcopy[f]

> > +                fullcopy[of] = f

> > +            else: # divergence w.r.t. graft CA on one side of topological CA

> > +                for sf in seen:

> > +                    if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:

> > +                        assert sf not in diverge

> > +                        diverge[sf] = [f, of]

> > +                        break

> > +            return

> >

> > -    if of in ma:

> > -        diverge.setdefault(of, []).append(f)

> > +    if of in mta:

> > +        if backwards or remote_ca:

> > +            incomplete[of] = f

> > +        else:

> > +            for sf in seen:

> > +                if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:

> > +                    if cta == ca:

> > +                        diverge.setdefault(sf, []).append(f)

> > +                    else:

> > +                        incompletediverge[sf] = [of, f]

> > +                    return

>

> Can we split this to 2 or 3 patches? I guess divergence handling can be a

> separate patch, for example. The point is we'll probably want to speed up

> the review/feedback process. I think copy tracing is inherently complex.

> Reviewing a monolithic patch about it would require a fair amount of

> continuous time, which would make hard to start reviewing this series.


Is that really necessary? It's a lot of additional work for me, to find intermediate states that may work,
and fix each of them so they don't break anything in the testsuite, only for those fixes to be reverted
by the next patch in the series. It would be the digital equipment of digging and filling pits in the ground.

Splitting isn't the hard part; ensuring no breakage in the intermediate states is.

>

> (I haven't read the tests and some part of incomplete* handling yet. I'll

> revisit them when I found time to.)

>

> Regards,
Yuya Nishihara - Oct. 3, 2016, 3:16 p.m.
On Mon, 3 Oct 2016 10:28:37 +0000, Gábor STEFANIK wrote:
> > > +    _c1 = c1.p1() if c1.rev() is None else c1
> > > +    _c2 = c2.p1() if c2.rev() is None else c2
> > > +    dirty_c1 = not (ca == _c1 or ca.descendant(_c1))
> > > +    dirty_c2 = not (ca == _c2 or ca.descendant(_c2))
> > > +    graft = dirty_c1 or dirty_c2
> > > +    if graft:
> > > +        cta = _c1.ancestor(_c2)
> >
> > Can you fix ctx.descendant() to handle wctx? or maybe this could be
> >
> >   dirtyc<n> = ca != ca.ancestor(c<n>)  # for n = 1, 2
> >
> > and one of ca.ancestor(c<n>) would be cta. (I don't carefully investigate criss-
> > cross merge case, so I might be wrong.)
> 
> ctx.ancestor() always returns just one common ancestor, not all possible candidates.

Yes. My guess was ca.ancestor(c1) (not c1.ancestor(c2)) would have only one
common ancestor if it is ca, but I didn't think that deeply.

> So unfortunately there is no way to eliminate the use of descendant() here, nor do I feel
> that there is a need to do so. descendant() is still faster than ancestor() or especially
> commonancestorsheads().

Anyway, if descendant() is faster than ancestor(), no need to take a slow path.

> > > -    u1, u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)
> > > +    _u1, _u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)
> >
> > Can you give them better names? '_' prefix in local variables generally means
> > they are placeholder (unused output) variables. (Perhaps we can start with a
> > trivial patch that separates u1/2 and _u1/2, which will describe why we need
> > them.)
> 
> u1r, u2r, u1u, u2u (for rotated/unrotated)? I'm wary of using longer names, as many lines
> in copies.py are already borderline for the 80-character rule, and will require reformatting
> with the introduction of longer variable names.

Shorter names seem good so long as they are consistent.

> > And it would be nice if there's a brief comment when to use ca/ma over
> > cta/mta.
> >
> > > -    seen = set([f])
> > > -    for oc in getfctx(f, m1[f]).ancestors():
> > > +    seen = {f: [getfctx(f, m1[f])]}
> > > +    for oc in seen[f][0].ancestors():
> > >          ocr = oc.linkrev()
> > >          of = oc.path()
> > >          if of in seen:
> > > +            seen[of].append(oc)
> > >              # check limit late - grab last rename before
> > >              if ocr < limit:
> > >                  break
> > >              continue
> > > -        seen.add(of)
> > > +        seen[of] = [oc]
> >
> > nit: maybe we only need a dict of file nodes, not a dict of file contexts?
> 
> how do I get a file node id from a file context? ancestors() gives us contexts.

fctx.filenode()

See also basefilectx.__eq__() to make sure if using .filenode() is appropriate
compared to the current code.

> > > -        fullcopy[f] = of # remember for dir rename detection
> > > +        # remember for dir rename detection
> > > +        if backwards:
> > > +            fullcopy[of] = f # grafting backwards through renames
> > > +        else:
> > > +            fullcopy[f] = of
> > >          if of not in m2:
> > >              continue # no match, keep looking
> > >          if m2[of] == ma.get(of):
> > > -            break # no merge needed, quit early
> > > +            return # no merge needed, quit early
> >
> > So we no longer set diverge[of] = [f] this case. I don't know if it was
> > necessary to populate 'renamedelete', but this change seems good for a
> > separate patch to make sure it never break anything.
> 
> This isn't just some optimization, it's necessary for correct behavior of the
> new checkcopies. And it would break the old checkcopies if applied separately
> before the main patch.

Can you add a test that should fail if we only do s/break/return/ ?
Unfortunately, all tests passed if I tried that.

> Is that really necessary? It's a lot of additional work for me, to find intermediate states that may work,
> and fix each of them so they don't break anything in the testsuite, only for those fixes to be reverted
> by the next patch in the series. It would be the digital equipment of digging and filling pits in the ground.
> 
> Splitting isn't the hard part; ensuring no breakage in the intermediate states is.

If that requires lots of unwanted intermediate states, that wouldn't be what
we should take. I guess this patch could be split to a) a few trivial changes
plus b) one (or two) core change. Hopefully (a) will reduce noises in (b).

(I'm not skilled around this code, so please feel free to ignore my comments
if they seem wrong.)

Patch

diff --git a/mercurial/copies.py b/mercurial/copies.py
--- a/mercurial/copies.py
+++ b/mercurial/copies.py
@@ -321,6 +321,23 @@ 
     if repo.ui.configbool('experimental', 'disablecopytrace'):
         return {}, {}, {}, {}
 
+    # In certain scenarios (e.g. graft, update or rebase), ca can be overridden
+    # We still need to know a real common ancestor in this case
+    # We can't just compute _c1.ancestor(_c2) and compare it to ca, because
+    # there can be multiple common ancestors, e.g. in case of bidmerge.
+    # Because our caller may not know if the revision passed in lieu of the CA
+    # is a genuine common ancestor or not without explicitly checking it, it's
+    # better to determine that here.
+    cta = ca
+    # ca.descendant(wc) and ca.descendant(ca) are False, work around that
+    _c1 = c1.p1() if c1.rev() is None else c1
+    _c2 = c2.p1() if c2.rev() is None else c2
+    dirty_c1 = not (ca == _c1 or ca.descendant(_c1))
+    dirty_c2 = not (ca == _c2 or ca.descendant(_c2))
+    graft = dirty_c1 or dirty_c2
+    if graft:
+        cta = _c1.ancestor(_c2)
+
     limit = _findlimit(repo, c1.rev(), c2.rev())
     if limit is None:
         # no common ancestor, no copies
@@ -330,28 +347,56 @@ 
     m1 = c1.manifest()
     m2 = c2.manifest()
     ma = ca.manifest()
+    mta = cta.manifest()
 
-    copy1, copy2, = {}, {}
+    # see checkcopies documentation below for these dicts
+    copy1, copy2 = {}, {}
+    incomplete1, incomplete2 = {}, {}
     movewithdir1, movewithdir2 = {}, {}
     fullcopy1, fullcopy2 = {}, {}
-    diverge = {}
+    diverge, incompletediverge = {}, {}
 
     # find interesting file sets from manifests
+    if graft:
+        repo.ui.debug("  computing unmatched files in rotated DAG\n")
     addedinm1 = m1.filesnotin(ma)
     addedinm2 = m2.filesnotin(ma)
-    u1, u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)
+    _u1, _u2 = _computenonoverlap(repo, c1, c2, addedinm1, addedinm2)
+    if not graft:
+        u1, u2 = _u1, _u2
+    else: # need to recompute this for directory move handling when grafting
+        repo.ui.debug("  computing unmatched files in unrotated DAG\n")
+        u1, u2 = _computenonoverlap(repo, c1, c2, m1.filesnotin(mta),
+                                                  m2.filesnotin(mta))
+
     bothnew = sorted(addedinm1 & addedinm2)
 
     for f in u1:
-        checkcopies(c1, f, m1, m2, ca, limit, diverge, copy1, fullcopy1)
+        checkcopies(c1, f, m1, m2, ca, cta, dirty_c1, limit, diverge, copy1,
+                    fullcopy1, incomplete1, incompletediverge)
 
     for f in u2:
-        checkcopies(c2, f, m2, m1, ca, limit, diverge, copy2, fullcopy2)
+        checkcopies(c2, f, m2, m1, ca, cta, dirty_c2, limit, diverge, copy2,
+                    fullcopy2, incomplete2, incompletediverge)
 
     copy = dict(copy1.items() + copy2.items())
     movewithdir = dict(movewithdir1.items() + movewithdir2.items())
     fullcopy = dict(fullcopy1.items() + fullcopy2.items())
 
+    # combine partial copy paths discovered in the previous step
+    copyfrom, copyto = incomplete1, incomplete2
+    if dirty_c1:
+        copyfrom, copyto = incomplete2, incomplete1
+    for f in copyfrom:
+        if f in copyto:
+            copy[copyto[f]] = copyfrom[f]
+            del copyto[f]
+    for f in incompletediverge:
+        assert f not in diverge
+        ic = incompletediverge[f]
+        if ic[0] in copyto:
+            diverge[f] = [copyto[ic[0]], ic[1]]
+
     renamedelete = {}
     renamedeleteset = set()
     divergeset = set()
@@ -369,10 +414,29 @@ 
     if bothnew:
         repo.ui.debug("  unmatched files new in both:\n   %s\n"
                       % "\n   ".join(bothnew))
-    bothdiverge, _copy, _fullcopy = {}, {}, {}
+    bothdiverge = {}
+    _copy, _fullcopy = {}, {} # dummy dicts
+    _incomplete1, _incomplete2, _diverge = {}, {}, {}
     for f in bothnew:
-        checkcopies(c1, f, m1, m2, ca, limit, bothdiverge, _copy, _fullcopy)
-        checkcopies(c2, f, m2, m1, ca, limit, bothdiverge, _copy, _fullcopy)
+        checkcopies(c1, f, m1, m2, ca, cta, dirty_c1, limit, bothdiverge,
+                    _copy, _fullcopy, _incomplete1, _diverge)
+        checkcopies(c2, f, m2, m1, ca, cta, dirty_c2, limit, bothdiverge,
+                    _copy, _fullcopy, _incomplete2, _diverge)
+    _incomplete = _incomplete2
+    if dirty_c1:
+        _incomplete = _incomplete1
+        assert _incomplete2 == {}
+    else:
+        assert _incomplete1 == {}
+    for f in _diverge:
+        assert f not in bothdiverge
+        ic = _diverge[f]
+        if ic[0] in _incomplete:
+            bothdiverge[f] = [_incomplete[ic[0]], ic[1]]
+        elif ic[0] in (m1 if dirty_c1 else m2):
+            # backed-out rename on one side, but watch out for deleted files
+            bothdiverge[f] = ic
+
     for of, fl in bothdiverge.items():
         if len(fl) == 2 and fl[0] == fl[1]:
             copy[fl[0]] = of # not actually divergent, just matching renames
@@ -438,7 +502,7 @@ 
                       (d, dirmove[d]))
 
     # check unaccounted nonoverlapping files against directory moves
-    for f in u1 + u2:
+    for f in _u1 + _u2:
         if f not in fullcopy:
             for d in dirmove:
                 if f.startswith(d):
@@ -452,7 +516,8 @@ 
 
     return copy, movewithdir, diverge, renamedelete
 
-def checkcopies(ctx, f, m1, m2, ca, limit, diverge, copy, fullcopy):
+def checkcopies(ctx, f, m1, m2, ca, cta, remote_ca, limit, diverge, copy,
+                fullcopy, incomplete, incompletediverge):
     """
     check possible copies of f from m1 to m2
 
@@ -460,14 +525,25 @@ 
     f = the filename to check
     m1 = the source manifest
     m2 = the destination manifest
-    ca = the changectx of the common ancestor
+    ca = the changectx of the common ancestor, overridden on graft
+    cta = topological common ancestor for graft-like scenarios
+    remote_ca = True if ca is outside cta::m1, False otherwise
     limit = the rev number to not search beyond
     diverge = record all diverges in this dict
     copy = record all non-divergent copies in this dict
     fullcopy = record all copies in this dict
+    incomplete = record non-divergent partial copies here
+    incompletediverge = record divergent partial copies here
+    
+    note: limit is only an optimization, and there is no guarantee that
+    irrelevant revisions will not be limited
+    there is no easy way to make this algorithm stop in a guaranteed way
+    once it "goes behind a certain revision".
     """
 
     ma = ca.manifest()
+    mta = cta.manifest()
+    backwards = ca != cta and not remote_ca and f in ma
     getfctx = _makegetfctx(ctx)
 
     def _related(f1, f2, limit):
@@ -502,31 +578,57 @@ 
             return False
 
     of = None
-    seen = set([f])
-    for oc in getfctx(f, m1[f]).ancestors():
+    seen = {f: [getfctx(f, m1[f])]}
+    for oc in seen[f][0].ancestors():
         ocr = oc.linkrev()
         of = oc.path()
         if of in seen:
+            seen[of].append(oc)
             # check limit late - grab last rename before
             if ocr < limit:
                 break
             continue
-        seen.add(of)
+        seen[of] = [oc]
 
-        fullcopy[f] = of # remember for dir rename detection
+        # remember for dir rename detection
+        if backwards:
+            fullcopy[of] = f # grafting backwards through renames
+        else:
+            fullcopy[f] = of
         if of not in m2:
             continue # no match, keep looking
         if m2[of] == ma.get(of):
-            break # no merge needed, quit early
+            return # no merge needed, quit early
         c2 = getfctx(of, m2[of])
-        cr = _related(oc, c2, ca.rev())
+        cr = _related(oc, c2, cta.rev())
         if cr and (of == f or of == c2.path()): # non-divergent
-            copy[f] = of
-            of = None
-            break
+            if backwards:
+                copy[of] = f
+            elif of in ma:
+                copy[f] = of
+            elif remote_ca: # special case: a <- b <- a -> b "ping-pong" rename
+                copy[of] = f
+                del fullcopy[f]
+                fullcopy[of] = f
+            else: # divergence w.r.t. graft CA on one side of topological CA
+                for sf in seen:
+                    if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:
+                        assert sf not in diverge
+                        diverge[sf] = [f, of]
+                        break
+            return
 
-    if of in ma:
-        diverge.setdefault(of, []).append(f)
+    if of in mta:
+        if backwards or remote_ca:
+            incomplete[of] = f
+        else:
+            for sf in seen:
+                if sf in ma and getfctx(sf, ma[sf]) in seen[sf]:
+                    if cta == ca:
+                        diverge.setdefault(sf, []).append(f)
+                    else:
+                        incompletediverge[sf] = [of, f]
+                    return
 
 def duplicatecopies(repo, rev, fromrev, skiprev=None):
     '''reproduce copies from fromrev to rev in the dirstate
diff --git a/tests/test-graft.t b/tests/test-graft.t
--- a/tests/test-graft.t
+++ b/tests/test-graft.t
@@ -179,6 +179,13 @@ 
   committing changelog
   grafting 5:97f8bfe72746 "5"
     searching for copies back to rev 1
+    computing unmatched files in rotated DAG
+    computing unmatched files in unrotated DAG
+    unmatched files in other:
+     c
+    all copies found (* = to merge, ! = divergent, % = renamed and deleted):
+     src: 'c' -> dst: 'b' *
+    checking for directory renames
   resolving manifests
    branchmerge: True, force: True, partial: False
    ancestor: 4c60f11aa304, local: 6b9e5368ca4e+, remote: 97f8bfe72746
@@ -193,6 +200,13 @@ 
   scanning for duplicate grafts
   grafting 4:9c233e8e184d "4"
     searching for copies back to rev 1
+    computing unmatched files in rotated DAG
+    computing unmatched files in unrotated DAG
+    unmatched files in other:
+     c
+    all copies found (* = to merge, ! = divergent, % = renamed and deleted):
+     src: 'c' -> dst: 'b' *
+    checking for directory renames
   resolving manifests
    branchmerge: True, force: True, partial: False
    ancestor: 4c60f11aa304, local: 1905859650ec+, remote: 9c233e8e184d
@@ -427,8 +441,8 @@ 
   $ hg graft 3 --log -u foo
   grafting 3:4c60f11aa304 "3"
   warning: can't find ancestor for 'c' copied from 'b'!
-  $ hg log --template '{rev} {parents} {desc}\n' -r tip
-  14 1:5d205f8b35b6  3
+  $ hg log --template '{rev}:{node|short} {parents} {desc}\n' -r tip
+  14:0c921c65ef1e 1:5d205f8b35b6  3
   (grafted from 4c60f11aa304a54ae1c199feb94e7fc771e51ed8)
 
 Resolve conflicted graft
@@ -620,7 +634,7 @@ 
   date:        Thu Jan 01 00:00:00 1970 +0000
   summary:     2
   
-  changeset:   14:f64defefacee
+  changeset:   14:0c921c65ef1e
   parent:      1:5d205f8b35b6
   user:        foo
   date:        Thu Jan 01 00:00:00 1970 +0000
@@ -842,3 +856,431 @@ 
   |/
   o  0
   
+Graft from behind a move or rename
+==================================
+
+NOTE: This is affected by issue5343, and will need updating when it's fixed
+
+Possible cases during a regular graft (when ca is between cta and c2):
+
+name | c1<-cta | cta<->ca | ca->c2
+A.0  |         |          |
+A.1  |    X    |          |
+A.2  |         |     X    |
+A.3  |         |          |   X
+A.4  |    X    |     X    |
+A.5  |    X    |          |   X
+A.6  |         |     X    |   X
+A.7  |    X    |     X    |   X
+
+A.0 is trivial, and doesn't need copy tracking.
+For A.1, a forward rename is recorded in the c1 pass, to be followed later.
+In A.2, the rename is recorded in the c2 pass and followed backwards.
+A.3 is recorded in the c2 pass as a forward rename to be duplicated on target.
+In A.4, both passes of checkcopies record incomplete renames, which are
+then joined in mergecopies to record a rename to be followed.
+In A.5 and A.7, the c1 pass records an incomplete rename, while the c2 pass
+records an incomplete divergence. The incomplete rename is then joined to the
+appropriate side of the incomplete divergence, and the result is recorded as a
+divergence. The code doesn't distinguish at all between these two cases, since
+the end result of them is the same: an incomplete divergence joined with an
+incomplete rename into a divergence.
+Finally, A.6 records a divergence entirely in the c2 pass. 
+
+A.4 has a degenerate case a<-b<-a->a, where checkcopies isn't needed at all.
+A.5 has a special case a<-b<-b->a, which is treated like a<-b->a in a merge.
+A.6 has a special case a<-a<-b->a. Here, checkcopies will find a spurious
+incomplete divergence, which is in fact complete. This is handled later in
+mergecopies.
+A.7 has 4 special cases: a<-b<-a->b (the "ping-pong" case), a<-b<-c->b,
+a<-b<-a->c and a<-b<-c->a. Of these, only the "ping-pong" case is interesting,
+the others are fairly trivial (a<-b<-c->b and a<-b<-a->c proceed like the base
+case, a<-b<-c->a is treated the same as a<-b<-b->a).
+
+f5a therefore tests the "ping-pong" rename case, where a file is renamed to the
+same name on both branches, then the rename is backed out on one branch, and
+the backout is grafted to the other branch. This creates a challenging rename
+sequence of a<-b<-a->b in the graft target, topological CA, graft CA and graft
+source, respectively. Since rename detection will run on the c1 side for such a
+sequence (as for technical reasons, we split the c1 and c2 sides not at the
+graft CA, but rather at the topological CA), it will pick up a false rename,
+and cause a spurious merge conflict. This false rename is always exactly the
+reverse of the true rename that would be detected on the c2 side, so we can
+correct for it by detecting this condition and reversing as necessary.
+
+First, set up the repository with commits to be grafted
+
+  $ hg init ../graftmove
+  $ cd ../graftmove
+  $ echo c1a > f1a
+  $ echo c2a > f2a
+  $ echo c3a > f3a
+  $ echo c4a > f4a
+  $ echo c5a > f5a
+  $ hg ci -qAm a
+  $ hg mv f1a f1b
+  $ hg mv f3a f3b
+  $ hg mv f5a f5b
+  $ hg ci -qAm b
+  $ echo c1c > f1b
+  $ hg mv f2a f2c
+  $ hg mv f5b f5a
+  $ echo c5c > f5a
+  $ hg ci -qAm c
+  $ hg mv f3b f3d
+  $ echo c4d > f4a
+  $ hg ci -qAm d
+  $ hg log -G
+  @  changeset:   3:aa2584f6dee9
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     d
+  |
+  o  changeset:   2:c8d3926d7649
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     c
+  |
+  o  changeset:   1:7e9aff31b586
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     b
+  |
+  o  changeset:   0:3340a7726e9e
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     a
+  
+
+Test the cases A.2 (f1x), A.3 (f2x) and a special case of A.6 (f5x) where the
+two renames actually converge to the same name (thus no actual divergence).
+
+  $ hg up -q 0 # commit "a"
+  $ hg graft -r 2
+  grafting 2:c8d3926d7649 "c"
+  merging f1a and f1b to f1a
+  merging f5a
+  warning: can't find ancestor for 'f5a' copied from 'f5b'!
+  $ hg status --change .
+  M f1a
+  M f5a
+  A f2c
+  R f2a
+  $ hg cat f1a
+  c1c
+  $ hg cat f1b
+  f1b: no such file in rev 68af396ea7bf
+  [1]
+
+Test the cases A.0 (f4x) and A.6 (f3x)
+
+  $ hg graft -r 3
+  grafting 3:aa2584f6dee9 "d"
+  note: possible conflict - f3b was renamed multiple times to:
+   f3d
+   f3a
+  warning: can't find ancestor for 'f3d' copied from 'f3b'!
+
+Set up the repository for some further tests
+
+  $ hg up -q 0
+  $ hg mv f1a f1e
+  $ echo c2e > f2a
+  $ hg mv f3a f3e
+  $ hg mv f4a f4e
+  $ hg mv f5a f5b
+  $ hg ci -qAm e
+  $ hg log -G
+  @  changeset:   6:52db7f4dcf33
+  |  tag:         tip
+  |  parent:      0:3340a7726e9e
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     e
+  |
+  | o  changeset:   5:29f6ffdbca28
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     d
+  | |
+  | o  changeset:   4:68af396ea7bf
+  |/   parent:      0:3340a7726e9e
+  |    user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     c
+  |
+  | o  changeset:   3:aa2584f6dee9
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     d
+  | |
+  | o  changeset:   2:c8d3926d7649
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     c
+  | |
+  | o  changeset:   1:7e9aff31b586
+  |/   user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     b
+  |
+  o  changeset:   0:3340a7726e9e
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     a
+  
+
+Test the cases A.4 (f1x), the "ping-pong" special case of A.7 (f5x),
+and A.3 with a local content change to be preserved (f2x).
+
+  $ hg graft -r 2
+  grafting 2:c8d3926d7649 "c"
+  merging f1e and f1b to f1e
+  merging f2a and f2c to f2c
+  merging f5b and f5a to f5a
+
+Test the cases A.1 (f4x) and A.7 (f3x).
+
+  $ hg graft -r 3
+  grafting 3:aa2584f6dee9 "d"
+  note: possible conflict - f3b was renamed multiple times to:
+   f3e
+   f3d
+  merging f4e and f4a to f4e
+  warning: can't find ancestor for 'f3d' copied from 'f3b'!
+
+Check the results of the grafts tested
+
+  $ hg log -CGv --patch --git
+  @  changeset:   8:4c243d7a2f50
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  files:       f3d f4e
+  |  description:
+  |  d
+  |
+  |
+  |  diff --git a/f3d b/f3d
+  |  new file mode 100644
+  |  --- /dev/null
+  |  +++ b/f3d
+  |  @@ -0,0 +1,1 @@
+  |  +c3a
+  |  diff --git a/f4e b/f4e
+  |  --- a/f4e
+  |  +++ b/f4e
+  |  @@ -1,1 +1,1 @@
+  |  -c4a
+  |  +c4d
+  |
+  o  changeset:   7:9b7ee4960a74
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  files:       f1e f2a f2c f5a f5b
+  |  copies:      f2c (f2a) f5a (f5b)
+  |  description:
+  |  c
+  |
+  |
+  |  diff --git a/f1e b/f1e
+  |  --- a/f1e
+  |  +++ b/f1e
+  |  @@ -1,1 +1,1 @@
+  |  -c1a
+  |  +c1c
+  |  diff --git a/f2a b/f2c
+  |  rename from f2a
+  |  rename to f2c
+  |  diff --git a/f5b b/f5a
+  |  rename from f5b
+  |  rename to f5a
+  |  --- a/f5b
+  |  +++ b/f5a
+  |  @@ -1,1 +1,1 @@
+  |  -c5a
+  |  +c5c
+  |
+  o  changeset:   6:52db7f4dcf33
+  |  parent:      0:3340a7726e9e
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  files:       f1a f1e f2a f3a f3e f4a f4e f5a f5b
+  |  copies:      f1e (f1a) f3e (f3a) f4e (f4a) f5b (f5a)
+  |  description:
+  |  e
+  |
+  |
+  |  diff --git a/f1a b/f1e
+  |  rename from f1a
+  |  rename to f1e
+  |  diff --git a/f2a b/f2a
+  |  --- a/f2a
+  |  +++ b/f2a
+  |  @@ -1,1 +1,1 @@
+  |  -c2a
+  |  +c2e
+  |  diff --git a/f3a b/f3e
+  |  rename from f3a
+  |  rename to f3e
+  |  diff --git a/f4a b/f4e
+  |  rename from f4a
+  |  rename to f4e
+  |  diff --git a/f5a b/f5b
+  |  rename from f5a
+  |  rename to f5b
+  |
+  | o  changeset:   5:29f6ffdbca28
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  files:       f3d f4a
+  | |  description:
+  | |  d
+  | |
+  | |
+  | |  diff --git a/f3d b/f3d
+  | |  new file mode 100644
+  | |  --- /dev/null
+  | |  +++ b/f3d
+  | |  @@ -0,0 +1,1 @@
+  | |  +c3a
+  | |  diff --git a/f4a b/f4a
+  | |  --- a/f4a
+  | |  +++ b/f4a
+  | |  @@ -1,1 +1,1 @@
+  | |  -c4a
+  | |  +c4d
+  | |
+  | o  changeset:   4:68af396ea7bf
+  |/   parent:      0:3340a7726e9e
+  |    user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    files:       f1a f2a f2c f5a
+  |    copies:      f2c (f2a)
+  |    description:
+  |    c
+  |
+  |
+  |    diff --git a/f1a b/f1a
+  |    --- a/f1a
+  |    +++ b/f1a
+  |    @@ -1,1 +1,1 @@
+  |    -c1a
+  |    +c1c
+  |    diff --git a/f2a b/f2c
+  |    rename from f2a
+  |    rename to f2c
+  |    diff --git a/f5a b/f5a
+  |    --- a/f5a
+  |    +++ b/f5a
+  |    @@ -1,1 +1,1 @@
+  |    -c5a
+  |    +c5c
+  |
+  | o  changeset:   3:aa2584f6dee9
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  files:       f3b f3d f4a
+  | |  copies:      f3d (f3b)
+  | |  description:
+  | |  d
+  | |
+  | |
+  | |  diff --git a/f3b b/f3d
+  | |  rename from f3b
+  | |  rename to f3d
+  | |  diff --git a/f4a b/f4a
+  | |  --- a/f4a
+  | |  +++ b/f4a
+  | |  @@ -1,1 +1,1 @@
+  | |  -c4a
+  | |  +c4d
+  | |
+  | o  changeset:   2:c8d3926d7649
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  files:       f1b f2a f2c f5a f5b
+  | |  copies:      f2c (f2a) f5a (f5b)
+  | |  description:
+  | |  c
+  | |
+  | |
+  | |  diff --git a/f1b b/f1b
+  | |  --- a/f1b
+  | |  +++ b/f1b
+  | |  @@ -1,1 +1,1 @@
+  | |  -c1a
+  | |  +c1c
+  | |  diff --git a/f2a b/f2c
+  | |  rename from f2a
+  | |  rename to f2c
+  | |  diff --git a/f5b b/f5a
+  | |  rename from f5b
+  | |  rename to f5a
+  | |  --- a/f5b
+  | |  +++ b/f5a
+  | |  @@ -1,1 +1,1 @@
+  | |  -c5a
+  | |  +c5c
+  | |
+  | o  changeset:   1:7e9aff31b586
+  |/   user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    files:       f1a f1b f3a f3b f5a f5b
+  |    copies:      f1b (f1a) f3b (f3a) f5b (f5a)
+  |    description:
+  |    b
+  |
+  |
+  |    diff --git a/f1a b/f1b
+  |    rename from f1a
+  |    rename to f1b
+  |    diff --git a/f3a b/f3b
+  |    rename from f3a
+  |    rename to f3b
+  |    diff --git a/f5a b/f5b
+  |    rename from f5a
+  |    rename to f5b
+  |
+  o  changeset:   0:3340a7726e9e
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     files:       f1a f2a f3a f4a f5a
+     description:
+     a
+  
+  
+     diff --git a/f1a b/f1a
+     new file mode 100644
+     --- /dev/null
+     +++ b/f1a
+     @@ -0,0 +1,1 @@
+     +c1a
+     diff --git a/f2a b/f2a
+     new file mode 100644
+     --- /dev/null
+     +++ b/f2a
+     @@ -0,0 +1,1 @@
+     +c2a
+     diff --git a/f3a b/f3a
+     new file mode 100644
+     --- /dev/null
+     +++ b/f3a
+     @@ -0,0 +1,1 @@
+     +c3a
+     diff --git a/f4a b/f4a
+     new file mode 100644
+     --- /dev/null
+     +++ b/f4a
+     @@ -0,0 +1,1 @@
+     +c4a
+     diff --git a/f5a b/f5a
+     new file mode 100644
+     --- /dev/null
+     +++ b/f5a
+     @@ -0,0 +1,1 @@
+     +c5a
+  
+  $ hg cat f2c
+  c2e
diff --git a/tests/test-rebase-conflicts.t b/tests/test-rebase-conflicts.t
--- a/tests/test-rebase-conflicts.t
+++ b/tests/test-rebase-conflicts.t
@@ -238,6 +238,10 @@ 
    merge against 9:e31216eec445
      detach base 8:8e4e2c1a07ae
     searching for copies back to rev 3
+    computing unmatched files in rotated DAG
+    computing unmatched files in unrotated DAG
+    unmatched files in other:
+     f2.txt
   resolving manifests
    branchmerge: True, force: True, partial: False
    ancestor: 8e4e2c1a07ae, local: 4bc80088dc6b+, remote: e31216eec445
@@ -255,6 +259,10 @@ 
    merge against 10:2f2496ddf49d
      detach base 9:e31216eec445
     searching for copies back to rev 3
+    computing unmatched files in rotated DAG
+    computing unmatched files in unrotated DAG
+    unmatched files in other:
+     f2.txt
   resolving manifests
    branchmerge: True, force: True, partial: False
    ancestor: e31216eec445, local: 19c888675e13+, remote: 2f2496ddf49d