hgext/parentrevspec.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Mon, 19 Apr 2010 21:44:21 +0200
changeset 10943 15aaa3637f19
parent 10282 08a0f04b56bd
permissions -rw-r--r--
merge with stable
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
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9268
diff changeset
     6
# GNU General Public License version 2 or any later version.
8228
eee2319c5895 add blank line after copyright notices and after header
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     7
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8873
diff changeset
     8
'''interpret suffixes to refer to ancestor revisions
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
9268
1746da638d95 parentrevspec: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9213
diff changeset
    10
This extension allows you to use git-style suffixes to refer to the
1746da638d95 parentrevspec: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9213
diff changeset
    11
ancestors of a specific revision.
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
9213
ee0af2f28794 parentrevspec: change list to a literal block
Martin Geisler <mg@lazybytes.net>
parents: 9070
diff changeset
    13
For example, if you can refer to a revision as "foo", then::
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
9213
ee0af2f28794 parentrevspec: change list to a literal block
Martin Geisler <mg@lazybytes.net>
parents: 9070
diff changeset
    15
  foo^N = Nth parent of foo
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    16
  foo^0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
  foo^1 = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
  foo^2 = second parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
  foo^  = foo^1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    20
9213
ee0af2f28794 parentrevspec: change list to a literal block
Martin Geisler <mg@lazybytes.net>
parents: 9070
diff changeset
    21
  foo~N = Nth first grandparent of foo
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    22
  foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    23
  foo~1 = foo^1 = foo^ = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    24
  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
    25
'''
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    26
from mercurial import error
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    27
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    28
def reposetup(ui, repo):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    29
    if not repo.local():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    30
        return
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    31
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
    class parentrevspecrepo(repo.__class__):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
        def lookup(self, key):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
                _super = super(parentrevspecrepo, self)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
                return _super.lookup(key)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    37
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    38
                pass
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
            circ = key.find('^')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
            tilde = key.find('~')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
            if circ < 0 and tilde < 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
                raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
            elif circ >= 0 and tilde >= 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
                end = min(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
            else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
                end = max(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
            cl = self.changelog
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
            base = key[:end]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
                node = _super.lookup(base)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    53
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    54
                # eek - reraise the first error
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    55
                return _super.lookup(key)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
            rev = cl.rev(node)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
            suffix = key[end:]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
            i = 0
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
            while i < len(suffix):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
                # foo^N => Nth parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
                # foo^0 == foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
                # foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    64
                # foo^2 == 2nd parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    65
                if suffix[i] == '^':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    66
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    67
                    p = cl.parentrevs(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    68
                    if j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    69
                        j += 1
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    70
                        n = int(suffix[i + 1:j])
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    71
                        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
    72
                            raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    73
                    else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    74
                        n = 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    75
                    if n:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    76
                        rev = p[n - 1]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    77
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
                # foo~N => Nth first grandparent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    79
                # foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    80
                # foo~1 = foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    81
                # 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
    82
                elif suffix[i] == '~':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    83
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    84
                    while j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    85
                        j += 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
                    if j == i + 1:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
                        raise
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    88
                    n = int(suffix[i + 1:j])
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
                    for k in xrange(n):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
                        rev = cl.parentrevs(rev)[0]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    91
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    92
                else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    93
                    raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
            return cl.node(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    96
    repo.__class__ = parentrevspecrepo