1 # Mercurial extension to make it easy to refer to the parent of a revision
3 # Copyright (C) 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
8 '''interpret suffixes to refer to ancestor revisions
10 This extension allows you to use git-style suffixes to refer to the
11 ancestors of a specific revision.
13 For example, if you can refer to a revision as "foo", then::
15 foo^N = Nth parent of foo
17 foo^1 = first parent of foo
18 foo^2 = second parent of foo
21 foo~N = Nth first grandparent of foo
23 foo~1 = foo^1 = foo^ = first parent of foo
24 foo~2 = foo^1^1 = foo^^ = first parent of first parent of foo
26 from mercurial import error
28 def reposetup(ui, repo):
32 class parentrevspecrepo(repo.__class__):
33 def lookup(self, key):
35 _super = super(parentrevspecrepo, self)
36 return _super.lookup(key)
37 except error.RepoError:
42 if circ < 0 and tilde < 0:
44 elif circ >= 0 and tilde >= 0:
45 end = min(circ, tilde)
47 end = max(circ, tilde)
52 node = _super.lookup(base)
53 except error.RepoError:
54 # eek - reraise the first error
55 return _super.lookup(key)
60 while i < len(suffix):
61 # foo^N => Nth parent of foo
63 # foo^1 == foo^ == 1st parent of foo
64 # foo^2 == 2nd parent of foo
67 p = cl.parentrevs(rev)
68 if j < len(suffix) and suffix[j].isdigit():
70 n = int(suffix[i+1:j])
71 if n > 2 or n == 2 and p[1] == -1:
78 # foo~N => Nth first grandparent of foo
80 # foo~1 = foo^1 == foo^ == 1st parent of foo
81 # foo~2 = foo^1^1 == foo^^ == 1st parent of 1st parent of foo
82 elif suffix[i] == '~':
84 while j < len(suffix) and suffix[j].isdigit():
88 n = int(suffix[i+1:j])
90 rev = cl.parentrevs(rev)[0]
96 repo.__class__ = parentrevspecrepo