hgext/bookmarks.py
author David Soria Parra <dsp@php.net>
Wed, 22 Oct 2008 21:53:27 +0200
changeset 7239 135003a470f3
child 7250 352627bcafc3
permissions -rw-r--r--
Bookmarks: Add the bookmarks extension Mercurial bookmarks are local moveable pointers to changesets. If you commit a changeset that is based on a changeset that has a bookmark on it, the bookmark is forwarded to the new changeset. Thanks to Ian Dees, Ronny Pfannschmidt for their patches, Thanks to ronny, tonfa, prianha, mpm, #mercurial for their comments and their support that helped me to get things done.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     1
# Mercurial extension to provide the 'hg bookmark' command
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     2
#
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     3
# Copyright 2008 David Soria Parra <dsp@php.net>
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     4
#
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     7
'''mercurial bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     8
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     9
Mercurial bookmarks are local moveable pointers to changesets. Every bookmark
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    10
points to a changesets identified by it's hash. If you commit a changeset
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    11
that is based on a changeset that has a bookmark on it, the bookmark is forwarded
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    12
to the new changeset.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    13
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    14
It is possible to use bookmark names in every revision lookup (e.g. hg merge, hg update)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    15
'''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    16
from mercurial.commands import templateopts, hex, short
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    17
from mercurial.i18n import _
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    18
from mercurial import cmdutil, util, commands, changelog
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    19
from mercurial.node import nullrev
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    20
from mercurial.repo import RepoError
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    21
import mercurial, mercurial.localrepo, mercurial.repair, os
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    22
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    23
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    24
def parse(repo):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    25
    '''Parse .hg/bookmarks file and return a dictionary
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    26
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    27
    Bookmarks are stored as {HASH}\s{NAME}\n (localtags format) 
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    28
    values in the .hg/bookmarks file.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    29
    They are read by the parse() method and returned as a dictionary with
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    30
    name => hash values.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    31
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    32
    The parsed dictionary is cached until a write() operation is done.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    33
    '''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    34
    try:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    35
        if repo._bookmarks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    36
            return repo._bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    37
        repo._bookmarks = {}
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    38
        for line in repo.opener('bookmarks'):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    39
            sha, refspec = line.strip().split(' ', 1)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    40
            repo._bookmarks[refspec] = repo.lookup(sha)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    41
    except:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    42
        pass
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    43
    return repo._bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    44
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    45
def write(repo, refs):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    46
    '''Write bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    47
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    48
    Write the given bookmark => hash dictionary to the .hg/bookmarks file
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    49
    in a format equal to those of localtags.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    50
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    51
    We also store a backup of the previous state in undo.bookmarks that
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    52
    can be copied back on rollback.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    53
    '''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    54
    util.copyfile(repo.join('bookmarks'), repo.join('undo.bookmarks'))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    55
    file = repo.opener('bookmarks', 'w+')
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    56
    for refspec, node in refs.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    57
        file.write("%s %s\n" % (hex(node), refspec))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    58
    file.close()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    59
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    60
def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False, move=None):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    61
    '''mercurial bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    62
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    63
    Bookmarks are pointer to certain commits that move when commiting.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    64
    Bookmarks are local. They can be renamed, copied and deleted.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    65
    It is possible to use bookmark names in 'hg merge' and 'hg update' to 
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    66
    update to a given bookmark.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    67
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    68
    You can use 'hg bookmark [NAME]' to set a bookmark on the current tip
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    69
    with the given name. If you specify a second [NAME] the bookmark is
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    70
    set to the bookmark that has that name. You can also pass revision
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    71
    identifiers to set bookmarks too.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    72
    '''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    73
    hexfn = ui.debugflag and hex or short
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    74
    marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    75
    cur   = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    76
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    77
    if move:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    78
        if move not in marks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    79
            raise util.Abort(_("a bookmark of this name doesnot exists"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    80
        if mark in marks and not force:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    81
            raise util.Abort(_("a bookmark of the same name already exists"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    82
        marks[mark] = marks[move]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    83
        del marks[move]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    84
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    85
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    86
    
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    87
    if delete:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    88
        if mark == None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    89
            raise util.Abort(_("bookmark name required"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    90
        if mark not in marks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    91
            raise util.Abort(_("a bookmark of this name does not exists"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    92
        del marks[mark]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    93
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    94
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    95
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    96
    if mark != None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    97
        if mark.strip().count("\n") > 0:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    98
            raise Exception("bookmark cannot contain newlines")
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    99
        if mark in marks and not force:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   100
            raise util.Abort(_("a bookmark of the same name already exists"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   101
        if ((mark in repo.branchtags() or mark == repo.dirstate.branch()) 
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   102
            and not force):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   103
            raise util.Abort(_("a bookmark cannot have the name of an existing branch"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   104
        if rev:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   105
            marks[mark] = repo.lookup(rev)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   106
        else:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   107
            marks[mark] = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   108
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   109
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   110
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   111
    if mark == None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   112
        if len(marks) == 0:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   113
            ui.status("no bookmarks set\n")
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   114
        else:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   115
            for bmark, n in marks.iteritems():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   116
                prefix = (n == cur) and '*' or ' '
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   117
                ui.write(" %s %-25s %d:%s\n" % (prefix, bmark, repo.changelog.rev(n), hexfn(n)))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   118
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   119
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   120
def _revstostrip(changelog, node):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   121
    srev = changelog.rev(node)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   122
    tostrip = [srev]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   123
    saveheads = []
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   124
    for r in xrange(srev, changelog.rev(changelog.tip()) + 1):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   125
        parents = changelog.parentrevs(r)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   126
        if parents[0] in tostrip or parents[1] in tostrip:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   127
            tostrip.append(r)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   128
            if parents[1] != nullrev:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   129
                for p in parents:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   130
                    if p not in tostrip and p > striprev:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   131
                        saveheads.append(p)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   132
    return [r for r in tostrip if r not in saveheads]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   133
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   134
def strip(ui, repo, node, backup="all"):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   135
    """Strip bookmarks if revisions are stripped using
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   136
    the mercurial.strip method. This usually happens during
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   137
    qpush and qpop"""
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   138
    revisions = _revstostrip(repo.changelog, node)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   139
    marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   140
    update = []
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   141
    for mark, n in marks.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   142
        if repo.changelog.rev(n) in revisions:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   143
            update.append(mark)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   144
    result = oldstrip(ui, repo, node, backup)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   145
    if len(update) > 0:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   146
        for m in update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   147
            marks[m] = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   148
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   149
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   150
oldstrip = mercurial.repair.strip
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   151
mercurial.repair.strip = strip
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   152
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   153
def reposetup(ui, repo):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   154
    if not isinstance(repo, mercurial.localrepo.localrepository):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   155
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   156
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   157
    # init a bookmark cache as otherwise we would get a infinite reading
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   158
    # in lookup()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   159
    repo._bookmarks = None
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   160
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   161
    class bookmark_repo(repo.__class__):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   162
        def rollback(self):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   163
            if os.path.exists(self.join('undo.bookmarks')):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   164
                util.rename(self.join('undo.bookmarks'), self.join('bookmarks'))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   165
            return super(bookmark_repo, self).rollback() 
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   166
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   167
        def lookup(self, key):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   168
            if self._bookmarks is None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   169
                self._bookmarks = parse(self)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   170
            if key in self._bookmarks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   171
                key = self._bookmarks[key]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   172
            return super(bookmark_repo, self).lookup(key)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   173
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   174
        def commit(self, *k, **kw):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   175
            """Add a revision to the repository and
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   176
            move the bookmark"""
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   177
            node  = super(bookmark_repo, self).commit(*k, **kw)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   178
            parents = repo.changelog.parents(node)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   179
            marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   180
            update = False
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   181
            for mark, n in marks.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   182
                if n in parents:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   183
                    marks[mark] = node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   184
                    update = True
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   185
            if update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   186
                write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   187
            return node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   188
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   189
        def addchangegroup(self, source, srctype, url, emptyok=False):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   190
            try:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   191
                onode = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   192
            except RepoError, inst:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   193
                pass
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   194
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   195
            result = super(bookmark_repo, self).addchangegroup(source, srctype, url, emptyok)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   196
            if result > 1:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   197
                # We have more heads than before
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   198
                return result
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   199
            node = repo.changelog.tip()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   200
            marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   201
            update = False
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   202
            for mark, n in marks.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   203
                if n == onode:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   204
                    marks[mark] = node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   205
                    update = True
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   206
            if update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   207
                write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   208
            return result
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   209
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   210
    repo.__class__ = bookmark_repo
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   211
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   212
cmdtable = {
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   213
    "bookmarks":
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   214
        (bookmark,
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   215
         [('f', 'force', False, _('force')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   216
          ('r', 'rev', '', _('revision')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   217
          ('d', 'delete', False, _('delete a given bookmark')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   218
          ('m', 'move', '', _('move a given bookmark'))],
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   219
         _('hg bookmarks [-d] [-m NAME] [-r NAME] [NAME]')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   220
}