hgext/parentrevspec.py
author Bryan O'Sullivan <bos@serpentine.com>
Fri, 29 May 2009 21:31:33 -0700
changeset 8652 64614c7e4bd9
parent 8618 cf6f567e27e9
child 8873 e872ef2e6758
permissions -rw-r--r--
Merge with mpm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     1
# Mercurial extension to make it easy to refer to the parent of a revision
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     2
#
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# Copyright (C) 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7996
diff changeset
     5
# This software may be used and distributed according to the terms of the
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7996
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
8228
eee2319c5895 add blank line after copyright notices and after header
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     7
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     8
'''\
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
use suffixes to refer to ancestor revisions
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
7996
a999dbc915f2 parentrevspec: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7873
diff changeset
    11
This extension allows you to use git-style suffixes to refer to the
a999dbc915f2 parentrevspec: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7873
diff changeset
    12
ancestors of a specific revision.
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
For example, if you can refer to a revision as "foo", then:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    15
8618
cf6f567e27e9 parentrevspec: remove a trailing colon
Cédric Duval <cedricduval@free.fr>
parents: 8228
diff changeset
    16
- foo^N = Nth parent of foo
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
  foo^0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
  foo^1 = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
  foo^2 = second parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    20
  foo^  = foo^1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    21
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    22
- foo~N = Nth first grandparent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    23
  foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    24
  foo~1 = foo^1 = foo^ = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    25
  foo~2 = foo^1^1 = foo^^ = first parent of first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    26
'''
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    27
from mercurial import error
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    28
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    29
def reposetup(ui, repo):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    30
    if not repo.local():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    31
        return
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
    class parentrevspecrepo(repo.__class__):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
        def lookup(self, key):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
                _super = super(parentrevspecrepo, self)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    37
                return _super.lookup(key)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    38
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
                pass
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
            circ = key.find('^')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
            tilde = key.find('~')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
            if circ < 0 and tilde < 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
                raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
            elif circ >= 0 and tilde >= 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
                end = min(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
            else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
                end = max(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
            cl = self.changelog
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
            base = key[:end]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
                node = _super.lookup(base)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    54
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    55
                # eek - reraise the first error
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
                return _super.lookup(key)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
            rev = cl.rev(node)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
            suffix = key[end:]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
            i = 0
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
            while i < len(suffix):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
                # foo^N => Nth parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
                # foo^0 == foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    64
                # foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    65
                # foo^2 == 2nd parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    66
                if suffix[i] == '^':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    67
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    68
                    p = cl.parentrevs(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    69
                    if j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    70
                        j += 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    71
                        n = int(suffix[i+1:j])
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    72
                        if n > 2 or n == 2 and p[1] == -1:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    73
                            raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    74
                    else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    75
                        n = 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    76
                    if n:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    77
                        rev = p[n - 1]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    79
                # foo~N => Nth first grandparent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    80
                # foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    81
                # foo~1 = foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    82
                # foo~2 = foo^1^1 == foo^^ == 1st parent of 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    83
                elif suffix[i] == '~':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    84
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    85
                    while j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
                        j += 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
                    if j == i + 1:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
                        raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
                    n = int(suffix[i+1:j])
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
                    for k in xrange(n):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    91
                        rev = cl.parentrevs(rev)[0]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    92
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    93
                else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
                    raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
            return cl.node(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    96
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    97
    repo.__class__ = parentrevspecrepo