eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/hgext/parentrevspec.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 # Mercurial extension to make it easy to refer to the parent of a revision
       
     2 #
       
     3 # Copyright (C) 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
       
     4 #
       
     5 # This software may be used and distributed according to the terms of the
       
     6 # GNU General Public License version 2 or any later version.
       
     7 
       
     8 '''interpret suffixes to refer to ancestor revisions
       
     9 
       
    10 This extension allows you to use git-style suffixes to refer to the
       
    11 ancestors of a specific revision.
       
    12 
       
    13 For example, if you can refer to a revision as "foo", then::
       
    14 
       
    15   foo^N = Nth parent of foo
       
    16   foo^0 = foo
       
    17   foo^1 = first parent of foo
       
    18   foo^2 = second parent of foo
       
    19   foo^  = foo^1
       
    20 
       
    21   foo~N = Nth first grandparent of foo
       
    22   foo~0 = 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
       
    25 '''
       
    26 from mercurial import error
       
    27 
       
    28 def reposetup(ui, repo):
       
    29     if not repo.local():
       
    30         return
       
    31 
       
    32     class parentrevspecrepo(repo.__class__):
       
    33         def lookup(self, key):
       
    34             try:
       
    35                 _super = super(parentrevspecrepo, self)
       
    36                 return _super.lookup(key)
       
    37             except error.RepoError:
       
    38                 pass
       
    39 
       
    40             circ = key.find('^')
       
    41             tilde = key.find('~')
       
    42             if circ < 0 and tilde < 0:
       
    43                 raise
       
    44             elif circ >= 0 and tilde >= 0:
       
    45                 end = min(circ, tilde)
       
    46             else:
       
    47                 end = max(circ, tilde)
       
    48 
       
    49             cl = self.changelog
       
    50             base = key[:end]
       
    51             try:
       
    52                 node = _super.lookup(base)
       
    53             except error.RepoError:
       
    54                 # eek - reraise the first error
       
    55                 return _super.lookup(key)
       
    56 
       
    57             rev = cl.rev(node)
       
    58             suffix = key[end:]
       
    59             i = 0
       
    60             while i < len(suffix):
       
    61                 # foo^N => Nth parent of foo
       
    62                 # foo^0 == foo
       
    63                 # foo^1 == foo^ == 1st parent of foo
       
    64                 # foo^2 == 2nd parent of foo
       
    65                 if suffix[i] == '^':
       
    66                     j = i + 1
       
    67                     p = cl.parentrevs(rev)
       
    68                     if j < len(suffix) and suffix[j].isdigit():
       
    69                         j += 1
       
    70                         n = int(suffix[i + 1:j])
       
    71                         if n > 2 or n == 2 and p[1] == -1:
       
    72                             raise
       
    73                     else:
       
    74                         n = 1
       
    75                     if n:
       
    76                         rev = p[n - 1]
       
    77                     i = j
       
    78                 # foo~N => Nth first grandparent of foo
       
    79                 # foo~0 = 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] == '~':
       
    83                     j = i + 1
       
    84                     while j < len(suffix) and suffix[j].isdigit():
       
    85                         j += 1
       
    86                     if j == i + 1:
       
    87                         raise
       
    88                     n = int(suffix[i + 1:j])
       
    89                     for k in xrange(n):
       
    90                         rev = cl.parentrevs(rev)[0]
       
    91                     i = j
       
    92                 else:
       
    93                     raise
       
    94             return cl.node(rev)
       
    95 
       
    96     repo.__class__ = parentrevspecrepo