hgext/parentrevspec.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Tue, 21 Aug 2007 02:13:32 +0200
changeset 5207 212de429e000
parent 5194 b111e9a907b1
child 7637 1d54e2f6c0b7
permissions -rw-r--r--
make hg add of a removed file unremove the file if a file was removed, hg add shouldn't mark the file as added, but as normal see issue683
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
#
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
'''\
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     8
use suffixes to refer to ancestor revisions
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
This extension allows you to use git-style suffixes to refer to
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    11
the ancestors of a specific revision.
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
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
    14
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    15
- foo^N = Nth parent of foo:
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
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    21
- foo~N = Nth first grandparent of foo
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
'''
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    26
import mercurial.repo
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)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    37
            except mercurial.repo.RepoError:
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)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
            except mercurial.repo.RepoError:
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
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    70
                        n = int(suffix[i+1:j])
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
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
                    n = int(suffix[i+1:j])
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