hgext/bookmarks.py
author David Soria Parra <dsp@php.net>
Fri, 05 Dec 2008 11:12:46 +0100
changeset 7479 cae586246331
parent 7478 4c3e0ad58c5b
child 7480 31f70804f1b1
permissions -rw-r--r--
bookmarks: Fix indention
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.
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
     7
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     8
'''mercurial bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
     9
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    10
Mercurial bookmarks are local moveable pointers to changesets. Every
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    11
bookmark points to a changeset identified by its hash. If you commit a
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    12
changeset that is based on a changeset that has a bookmark on it, the
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    13
bookmark is forwarded to the new changeset.
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    14
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    15
It is possible to use bookmark names in every revision lookup (e.g. hg
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    16
merge, hg update).
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    17
'''
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    18
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    19
from mercurial.commands import templateopts, hex, short
7478
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
    20
from mercurial import extensions
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    21
from mercurial.i18n import _
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    22
from mercurial import cmdutil, util, commands, changelog
7256
df800e004077 bookmarks: Avoid unconditional forwarding of bookmarks for the null revision
Joel Rosdahl <joel@rosdahl.net>
parents: 7255
diff changeset
    23
from mercurial.node import nullid, nullrev
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    24
from mercurial.repo import RepoError
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    25
import mercurial, mercurial.localrepo, mercurial.repair, os
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
def parse(repo):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    28
    '''Parse .hg/bookmarks file and return a dictionary
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    29
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    30
    Bookmarks are stored as {HASH}\s{NAME}\n (localtags format) values
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    31
    in the .hg/bookmarks file. They are read by the parse() method and
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    32
    returned as a dictionary with name => hash values.
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    33
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    34
    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
    35
    '''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    36
    try:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    37
        if repo._bookmarks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    38
            return repo._bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    39
        repo._bookmarks = {}
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    40
        for line in repo.opener('bookmarks'):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    41
            sha, refspec = line.strip().split(' ', 1)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    42
            repo._bookmarks[refspec] = repo.lookup(sha)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    43
    except:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    44
        pass
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    45
    return repo._bookmarks
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    46
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    47
def write(repo, refs):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    48
    '''Write bookmarks
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    49
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    50
    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
    51
    in a format equal to those of localtags.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    52
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    53
    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
    54
    can be copied back on rollback.
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    55
    '''
7253
8b81d1e2dc04 bookmarks: Only save undo.bookmarks if bookmarks exist
Joel Rosdahl <joel@rosdahl.net>
parents: 7252
diff changeset
    56
    if os.path.exists(repo.join('bookmarks')):
8b81d1e2dc04 bookmarks: Only save undo.bookmarks if bookmarks exist
Joel Rosdahl <joel@rosdahl.net>
parents: 7252
diff changeset
    57
        util.copyfile(repo.join('bookmarks'), repo.join('undo.bookmarks'))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    58
    file = repo.opener('bookmarks', 'w+')
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    59
    for refspec, node in refs.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    60
        file.write("%s %s\n" % (hex(node), refspec))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    61
    file.close()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    62
7255
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
    63
def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False, rename=None):
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    64
    '''mercurial bookmarks
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    65
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    66
    Bookmarks are pointers to certain commits that move when
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    67
    commiting. Bookmarks are local. They can be renamed, copied and
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    68
    deleted. It is possible to use bookmark names in 'hg merge' and 'hg
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
    69
    update' to update to a given bookmark.
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    70
7257
7fb0e130cf14 bookmarks: Improve documentation
Joel Rosdahl <joel@rosdahl.net>
parents: 7256
diff changeset
    71
    You can use 'hg bookmark NAME' to set a bookmark on the current
7fb0e130cf14 bookmarks: Improve documentation
Joel Rosdahl <joel@rosdahl.net>
parents: 7256
diff changeset
    72
    tip with the given name. If you specify a revision using -r REV
7fb0e130cf14 bookmarks: Improve documentation
Joel Rosdahl <joel@rosdahl.net>
parents: 7256
diff changeset
    73
    (where REV may be an existing bookmark), the bookmark is set to
7fb0e130cf14 bookmarks: Improve documentation
Joel Rosdahl <joel@rosdahl.net>
parents: 7256
diff changeset
    74
    that revision.
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    75
    '''
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    76
    hexfn = ui.debugflag and hex or short
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    77
    marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    78
    cur   = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    79
7255
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
    80
    if rename:
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
    81
        if rename not in marks:
7251
444d88175e33 bookmarks: Fix spelling and grammar
Joel Rosdahl <joel@rosdahl.net>
parents: 7250
diff changeset
    82
            raise util.Abort(_("a bookmark of this name does not exist"))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    83
        if mark in marks and not force:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    84
            raise util.Abort(_("a bookmark of the same name already exists"))
7254
d892211d670e bookmarks: Require new bookmark name when renaming
Joel Rosdahl <joel@rosdahl.net>
parents: 7253
diff changeset
    85
        if mark is None:
d892211d670e bookmarks: Require new bookmark name when renaming
Joel Rosdahl <joel@rosdahl.net>
parents: 7253
diff changeset
    86
            raise util.Abort(_("new bookmark name required"))
7255
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
    87
        marks[mark] = marks[rename]
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
    88
        del marks[rename]
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    89
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    90
        return
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
    91
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    92
    if delete:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    93
        if mark == None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    94
            raise util.Abort(_("bookmark name required"))
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    95
        if mark not in marks:
7251
444d88175e33 bookmarks: Fix spelling and grammar
Joel Rosdahl <joel@rosdahl.net>
parents: 7250
diff changeset
    96
            raise util.Abort(_("a bookmark of this name does not exist"))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    97
        del marks[mark]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    98
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
    99
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   100
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   101
    if mark != None:
7259
18c23375861f bookmarks: Correctly reject newlines in bookmark names
Joel Rosdahl <joel@rosdahl.net>
parents: 7258
diff changeset
   102
        if "\n" in mark:
18c23375861f bookmarks: Correctly reject newlines in bookmark names
Joel Rosdahl <joel@rosdahl.net>
parents: 7258
diff changeset
   103
            raise util.Abort(_("bookmark name cannot contain newlines"))
7260
eb91b9ce2c19 bookmarks: Strip bookmark names of whitespace, just like tag names
Joel Rosdahl <joel@rosdahl.net>
parents: 7259
diff changeset
   104
        mark = mark.strip()
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   105
        if mark in marks and not force:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   106
            raise util.Abort(_("a bookmark of the same name already exists"))
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
   107
        if ((mark in repo.branchtags() or mark == repo.dirstate.branch())
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   108
            and not force):
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   109
            raise util.Abort(
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   110
                _("a bookmark cannot have the name of an existing branch"))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   111
        if rev:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   112
            marks[mark] = repo.lookup(rev)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   113
        else:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   114
            marks[mark] = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   115
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   116
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   117
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   118
    if mark == None:
7258
9bd051efbdd6 bookmarks: Require a bookmark name when a revision is specified
Joel Rosdahl <joel@rosdahl.net>
parents: 7257
diff changeset
   119
        if rev:
9bd051efbdd6 bookmarks: Require a bookmark name when a revision is specified
Joel Rosdahl <joel@rosdahl.net>
parents: 7257
diff changeset
   120
            raise util.Abort(_("bookmark name required"))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   121
        if len(marks) == 0:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   122
            ui.status("no bookmarks set\n")
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   123
        else:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   124
            for bmark, n in marks.iteritems():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   125
                prefix = (n == cur) and '*' or ' '
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   126
                ui.write(" %s %-25s %d:%s\n" % (
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   127
                    prefix, bmark, repo.changelog.rev(n), hexfn(n)))
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   128
        return
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   129
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   130
def _revstostrip(changelog, node):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   131
    srev = changelog.rev(node)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   132
    tostrip = [srev]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   133
    saveheads = []
7283
b19c0200c90b bookmarks: fix strip handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7280
diff changeset
   134
    for r in xrange(srev, len(changelog)):
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   135
        parents = changelog.parentrevs(r)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   136
        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
   137
            tostrip.append(r)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   138
            if parents[1] != nullrev:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   139
                for p in parents:
7283
b19c0200c90b bookmarks: fix strip handling
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7280
diff changeset
   140
                    if p not in tostrip and p > srev:
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   141
                        saveheads.append(p)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   142
    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
   143
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   144
def strip(ui, repo, node, backup="all"):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   145
    """Strip bookmarks if revisions are stripped using
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   146
    the mercurial.strip method. This usually happens during
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   147
    qpush and qpop"""
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   148
    revisions = _revstostrip(repo.changelog, node)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   149
    marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   150
    update = []
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   151
    for mark, n in marks.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   152
        if repo.changelog.rev(n) in revisions:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   153
            update.append(mark)
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7262
diff changeset
   154
    oldstrip(ui, repo, node, backup)
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   155
    if len(update) > 0:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   156
        for m in update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   157
            marks[m] = repo.changectx('.').node()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   158
        write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   159
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   160
oldstrip = mercurial.repair.strip
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   161
mercurial.repair.strip = strip
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   162
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   163
def reposetup(ui, repo):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   164
    if not isinstance(repo, mercurial.localrepo.localrepository):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   165
        return
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
    # 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
   168
    # in lookup()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   169
    repo._bookmarks = None
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   170
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   171
    class bookmark_repo(repo.__class__):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   172
        def rollback(self):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   173
            if os.path.exists(self.join('undo.bookmarks')):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   174
                util.rename(self.join('undo.bookmarks'), self.join('bookmarks'))
7250
352627bcafc3 bookmarks: Remove trailing space
Joel Rosdahl <joel@rosdahl.net>
parents: 7239
diff changeset
   175
            return super(bookmark_repo, self).rollback()
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   176
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   177
        def lookup(self, key):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   178
            if self._bookmarks is None:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   179
                self._bookmarks = parse(self)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   180
            if key in self._bookmarks:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   181
                key = self._bookmarks[key]
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   182
            return super(bookmark_repo, self).lookup(key)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   183
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   184
        def commit(self, *k, **kw):
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   185
            """Add a revision to the repository and
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   186
            move the bookmark"""
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   187
            node  = super(bookmark_repo, self).commit(*k, **kw)
7262
3cbadcf28a4c bookmarks: do nothing if commit was not successful.
Dmitriy Taychenachev <dimichxp@gmail.com>
parents: 7260
diff changeset
   188
            if node == None:
3cbadcf28a4c bookmarks: do nothing if commit was not successful.
Dmitriy Taychenachev <dimichxp@gmail.com>
parents: 7260
diff changeset
   189
                return None
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   190
            parents = repo.changelog.parents(node)
7256
df800e004077 bookmarks: Avoid unconditional forwarding of bookmarks for the null revision
Joel Rosdahl <joel@rosdahl.net>
parents: 7255
diff changeset
   191
            if parents[1] == nullid:
df800e004077 bookmarks: Avoid unconditional forwarding of bookmarks for the null revision
Joel Rosdahl <joel@rosdahl.net>
parents: 7255
diff changeset
   192
                parents = (parents[0],)
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   193
            marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   194
            update = False
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   195
            for mark, n in marks.items():
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   196
                if n in parents:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   197
                    marks[mark] = node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   198
                    update = True
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   199
            if update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   200
                write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   201
            return node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   202
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   203
        def addchangegroup(self, source, srctype, url, emptyok=False):
7316
9737041646bc bookmarks: Use dirstate to determine the current node in addchangegroup
David Soria Parra <dsp@php.net>
parents: 7283
diff changeset
   204
            parents = repo.dirstate.parents()
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   205
7252
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   206
            result = super(bookmark_repo, self).addchangegroup(
104a8324798e bookmarks: Avoid long lines
Joel Rosdahl <joel@rosdahl.net>
parents: 7251
diff changeset
   207
                source, srctype, url, emptyok)
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   208
            if result > 1:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   209
                # We have more heads than before
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   210
                return result
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   211
            node = repo.changelog.tip()
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   212
            marks = parse(repo)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   213
            update = False
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   214
            for mark, n in marks.items():
7316
9737041646bc bookmarks: Use dirstate to determine the current node in addchangegroup
David Soria Parra <dsp@php.net>
parents: 7283
diff changeset
   215
                if n in parents:
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   216
                    marks[mark] = node
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   217
                    update = True
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   218
            if update:
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   219
                write(repo, marks)
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   220
            return result
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   221
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   222
    repo.__class__ = bookmark_repo
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   223
7478
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   224
def pushnonbookmarked(orig, ui, repo, *args, **opts):
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   225
    'Call push with only the heads that are not bookmarked'
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   226
    if opts.get('non_bookmarked'):
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   227
        if opts.get('rev'):
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   228
            heads = [repo.lookup(r) for r in opts.get('rev')]
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   229
        else:
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   230
            heads = repo.heads()
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   231
7479
cae586246331 bookmarks: Fix indention
David Soria Parra <dsp@php.net>
parents: 7478
diff changeset
   232
        markheads = parse(repo).values()
7478
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   233
        opts['rev'] = [head for head in heads if not(head in markheads)]
7479
cae586246331 bookmarks: Fix indention
David Soria Parra <dsp@php.net>
parents: 7478
diff changeset
   234
7478
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   235
    orig(ui, repo, *args, **opts)
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   236
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   237
def uisetup(ui):
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   238
    'Replace push with a decorator to provide --non-bookmarked option'
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   239
    entry = extensions.wrapcommand(commands.table, 'push', pushnonbookmarked)
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   240
    entry[1].append(('', 'non-bookmarked', None, _("push all heads that are not bookmarked")))
4c3e0ad58c5b bookmarks: Adds support for a --non-bookmarked option to push
Stefan Rusek <stefan@rusek.org>
parents: 7316
diff changeset
   241
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   242
cmdtable = {
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   243
    "bookmarks":
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   244
        (bookmark,
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   245
         [('f', 'force', False, _('force')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   246
          ('r', 'rev', '', _('revision')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   247
          ('d', 'delete', False, _('delete a given bookmark')),
7255
69e431ea124d bookmarks: Rename --move to --rename
Joel Rosdahl <joel@rosdahl.net>
parents: 7254
diff changeset
   248
          ('m', 'rename', '', _('rename a given bookmark'))],
7239
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   249
         _('hg bookmarks [-d] [-m NAME] [-r NAME] [NAME]')),
135003a470f3 Bookmarks: Add the bookmarks extension
David Soria Parra <dsp@php.net>
parents:
diff changeset
   250
}