Questions about branching in Mercurial

Brendan Cully brendan at kublai.com
Sun May 6 20:03:56 UTC 2007


On Sunday, 06 May 2007 at 12:43, Matt Mackall wrote:
> On Sun, May 06, 2007 at 02:00:07PM +0200, Guido Ostkamp wrote:
> > >They've still got a couple rough edges that should be polished up in the 
> > >next couple months, but I think they're perfectly useful today provided 
> > >you understand how they work.
> > 
> > In the 'git' User Manual I've found this:
> > 
> > "Normally a merge results in a merge commit, with two parents, one 
> > pointing at each of the two lines of development that were merged.
> > 
> > However, if one of the two lines of development is completely contained 
> > within the other -- so every commit present in the one is already 
> > contained in the other -- then git just performs a fast forward; the head 
> > of the current branch is moved forward to point at the head of the 
> > merged-in branch, without any new commits being created."
> > 
> > I think this is exactly what is missing in Mercurial as of now. I would 
> > appreciate if you could add it.
> 
> Yep. Please see:
> 
> http://www.selenic.com/mercurial/wiki/index.cgi/BranchPlan

I think the attached patch may do the trick.

-------------- next part --------------
# HG changeset patch
# User Brendan Cully <brendan at kublai.com>
# Date 1178481642 25200
# Node ID bb94b16711b38c213fcfe4763a85fa7a599ac132
# Parent  1ef4445c6506d8d1f907bba3e48324f39e5c31d1
Add fast-forward branch merging

diff -r 1ef4445c6506 -r bb94b16711b3 mercurial/merge.py
--- a/mercurial/merge.py	Sun May 06 16:40:53 2007 +0200
+++ b/mercurial/merge.py	Sun May 06 13:00:42 2007 -0700
@@ -489,14 +489,19 @@ def update(repo, node, branchmerge, forc
     p1, p2 = pl[0], repo.changectx(node)
     pa = p1.ancestor(p2)
     fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
+    fastforward = False
 
     ### check phase
     if not overwrite and len(pl) > 1:
         raise util.Abort(_("outstanding uncommitted merges"))
     if pa == p1 or pa == p2: # is there a linear path from p1 to p2?
         if branchmerge:
-            raise util.Abort(_("there is nothing to merge, just use "
-                               "'hg update' or look at 'hg heads'"))
+            if p1.branch() != p2.branch():
+                fastforward = True
+                branchmerge = False
+            else:
+                raise util.Abort(_("there is nothing to merge, just use "
+                                   "'hg update' or look at 'hg heads'"))
     elif not (overwrite or branchmerge):
         raise util.Abort(_("update spans branches, use 'hg merge' "
                            "or 'hg update -C' to lose changes"))
@@ -525,7 +530,7 @@ def update(repo, node, branchmerge, forc
     if not partial:
         recordupdates(repo, action, branchmerge)
         repo.dirstate.setparents(fp1, fp2)
-        if not branchmerge:
+        if not branchmerge and not fastforward:
             repo.dirstate.setbranch(p2.branch())
         repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
 
diff -r 1ef4445c6506 -r bb94b16711b3 tests/test-newbranch
--- a/tests/test-newbranch	Sun May 06 16:40:53 2007 +0200
+++ b/tests/test-newbranch	Sun May 06 13:00:42 2007 -0700
@@ -51,4 +51,15 @@ hg branch foobar
 hg branch foobar
 hg up
 
+echo % fastforward merge
+hg branch ff
+echo ff > ff
+hg ci -Am'fast forward' -d '1000000 0'
+hg up foo
+hg merge ff
+hg branch
+hg commit -m'Merge ff into foo' -d '1000000 0'
+hg parents
+hg manifest
+
 exit 0
diff -r 1ef4445c6506 -r bb94b16711b3 tests/test-newbranch.out
--- a/tests/test-newbranch.out	Sun May 06 16:40:53 2007 +0200
+++ b/tests/test-newbranch.out	Sun May 06 13:00:42 2007 -0700
@@ -84,3 +84,18 @@ bf1bc2f45e83
 bf1bc2f45e83
 4909a3732169 (foo) tip
 abort: branch foobar not found
+% fastforward merge
+adding ff
+0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+(branch merge, don't forget to commit)
+foo
+changeset:   6:9cc105113eeb
+branch:      foo
+tag:         tip
+user:        test
+date:        Mon Jan 12 13:46:40 1970 +0000
+summary:     Merge ff into foo
+
+a
+ff


More information about the Mercurial mailing list