[PATCH] parentrevspec: add foo~~ to follow back to the start of a branch

Florent Guillaume fg at nuxeo.com
Fri Feb 22 21:00:59 CST 2008


# HG changeset patch
# User Florent Guillaume <fg at nuxeo.com>
# Date 1203733549 -3600
# Node ID fd7f39fc974d9455bfb3a4768839998b9f660e8b
# Parent  50a277e6ceaeaa5f890cacc9dcab745a4387dd58
parentrevspec: add foo~~ to follow back to the start of a branch

diff --git a/hgext/parentrevspec.py b/hgext/parentrevspec.py
--- a/hgext/parentrevspec.py
+++ b/hgext/parentrevspec.py
@@ -22,6 +22,7 @@ For example, if you can refer to a revis
   foo~0 = foo
   foo~1 = foo^1 = foo^ = first parent of foo
   foo~2 = foo^1^1 = foo^^ = first parent of first parent of foo
+  foo~~ = oldest grandparent still on the same branch as foo
 '''
 import mercurial.repo
 
@@ -75,6 +76,25 @@ def reposetup(ui, repo):
                     if n:
                         rev = p[n - 1]
                     i = j
+                # foo~~ = oldest grandparent still on the same branch as foo
+                elif suffix[i:i+2] == '~~':
+                    i += 2
+                    branch = cl.read(cl.node(rev))[5]['branch']
+                    while True:
+                        r1, r2 = cl.parentrevs(rev)
+                        if r1 == -1:
+                            break
+                        b1 = cl.read(cl.node(r1))[5]['branch']
+                        if r2 != -1:
+                            b2 = cl.read(cl.node(r2))[5]['branch']
+                        else:
+                            b2 = 0 # never equal to branch
+                        if b1 == branch and b2 != branch:
+                            rev = r1
+                        elif b1 != branch and b2 == branch:
+                            rev = r2
+                        else:
+                            break
                 # foo~N => Nth first grandparent of foo
                 # foo~0 = foo
                 # foo~1 = foo^1 == foo^ == 1st parent of foo
diff --git a/tests/test-parentrevspec b/tests/test-parentrevspec
--- a/tests/test-parentrevspec
+++ b/tests/test-parentrevspec
@@ -66,4 +66,19 @@ echo 'with a tag "foo^bar" pointing to r
 echo 'with a tag "foo^bar" pointing to rev 2'
 hg tag -l -r 2 "foo^bar"
 lookup "foo^bar" "foo^bar^"
+echo
 
+echo 'follow default up until merge'
+lookup "default~~"
+echo
+
+hg branch -q dev
+commit '7: start branch'
+commit '8: blah on branch'
+commit '9: again'
+
+echo 'find top of dev branch'
+lookup "dev~~"
+lookup "dev^~~"
+lookup "dev~~~~"
+
diff --git a/tests/test-parentrevspec.out b/tests/test-parentrevspec.out
--- a/tests/test-parentrevspec.out
+++ b/tests/test-parentrevspec.out
@@ -42,3 +42,11 @@ with a tag "foo^bar" pointing to rev 2
 with a tag "foo^bar" pointing to rev 2
 foo^bar: 2
 foo^bar^: abort: unknown revision 'foo^bar^'!
+
+follow default up until merge
+default~~: 5
+
+find top of dev branch
+dev~~: 7
+dev^~~: 7
+dev~~~~: 7


More information about the Mercurial-devel mailing list