bookmarks.py
257 lines
| 9.7 KiB
| text/x-python
|
PythonLexer
/ hgext / bookmarks.py
David Soria Parra
|
r7239 | # Mercurial extension to provide the 'hg bookmark' command | ||
# | ||||
# Copyright 2008 David Soria Parra <dsp@php.net> | ||||
# | ||||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Joel Rosdahl
|
r7252 | |||
Cédric Duval
|
r8894 | '''track a line of development with movable markers | ||
David Soria Parra
|
r7239 | |||
Martin Geisler
|
r9251 | Bookmarks are local movable markers to changesets. Every bookmark | ||
points to a changeset identified by its hash. If you commit a | ||||
changeset that is based on a changeset that has a bookmark on it, the | ||||
bookmark shifts to the new changeset. | ||||
David Soria Parra
|
r7239 | |||
Martin Geisler
|
r11193 | It is possible to use bookmark names in every revision lookup (e.g. | ||
:hg:`merge`, :hg:`update`). | ||||
David Soria Parra
|
r7481 | |||
Martin Geisler
|
r9251 | By default, when several bookmarks point to the same changeset, they | ||
will all move forward together. It is possible to obtain a more | ||||
git-like experience by adding the following configuration option to | ||||
Brodie Rao
|
r12083 | your configuration file:: | ||
David Soria Parra
|
r7481 | |||
Cédric Duval
|
r8892 | [bookmarks] | ||
track.current = True | ||||
David Soria Parra
|
r7481 | |||
Martin Geisler
|
r9251 | This will cause Mercurial to track the bookmark that you are currently | ||
using, and only update it. This is similar to git's approach to | ||||
branching. | ||||
David Soria Parra
|
r7239 | ''' | ||
Joel Rosdahl
|
r7252 | |||
David Soria Parra
|
r7239 | from mercurial.i18n import _ | ||
Augie Fackler
|
r12714 | from mercurial.node import nullid, nullrev, bin, hex, short | ||
David Soria Parra
|
r11431 | from mercurial import util, commands, repair, extensions, pushkey, hg, url | ||
Matt Mackall
|
r13359 | from mercurial import encoding | ||
Matt Mackall
|
r13350 | from mercurial import bookmarks | ||
Matt Mackall
|
r7638 | import os | ||
David Soria Parra
|
r7239 | |||
Joel Rosdahl
|
r7255 | def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False, rename=None): | ||
Cédric Duval
|
r8894 | '''track a line of development with movable markers | ||
Joel Rosdahl
|
r7250 | |||
Martin Geisler
|
r9251 | Bookmarks are pointers to certain commits that move when | ||
committing. Bookmarks are local. They can be renamed, copied and | ||||
Martin Geisler
|
r10973 | deleted. It is possible to use bookmark names in :hg:`merge` and | ||
:hg:`update` to merge and update respectively to a given bookmark. | ||||
Joel Rosdahl
|
r7250 | |||
Martin Geisler
|
r10973 | You can use :hg:`bookmark NAME` to set a bookmark on the working | ||
Martin Geisler
|
r9251 | directory's parent revision with the given name. If you specify | ||
a revision using -r REV (where REV may be an existing bookmark), | ||||
the bookmark is assigned to that revision. | ||||
Kevin Bullock
|
r12772 | |||
Bookmarks can be pushed and pulled between repositories (see :hg:`help | ||||
push` and :hg:`help pull`). This requires the bookmark extension to be | ||||
enabled for both the local and remote repositories. | ||||
David Soria Parra
|
r7239 | ''' | ||
hexfn = ui.debugflag and hex or short | ||||
Nicolas Dumazet
|
r10105 | marks = repo._bookmarks | ||
David Soria Parra
|
r7239 | cur = repo.changectx('.').node() | ||
Joel Rosdahl
|
r7255 | if rename: | ||
if rename not in marks: | ||||
Joel Rosdahl
|
r7251 | raise util.Abort(_("a bookmark of this name does not exist")) | ||
David Soria Parra
|
r7239 | if mark in marks and not force: | ||
raise util.Abort(_("a bookmark of the same name already exists")) | ||||
Joel Rosdahl
|
r7254 | if mark is None: | ||
raise util.Abort(_("new bookmark name required")) | ||||
Joel Rosdahl
|
r7255 | marks[mark] = marks[rename] | ||
del marks[rename] | ||||
Nicolas Dumazet
|
r10107 | if repo._bookmarkcurrent == rename: | ||
Matt Mackall
|
r13350 | bookmarks.setcurrent(repo, mark) | ||
bookmarks.write(repo) | ||||
David Soria Parra
|
r7239 | return | ||
Joel Rosdahl
|
r7250 | |||
David Soria Parra
|
r7239 | if delete: | ||
Martin Geisler
|
r8527 | if mark is None: | ||
David Soria Parra
|
r7239 | raise util.Abort(_("bookmark name required")) | ||
if mark not in marks: | ||||
Joel Rosdahl
|
r7251 | raise util.Abort(_("a bookmark of this name does not exist")) | ||
Nicolas Dumazet
|
r10107 | if mark == repo._bookmarkcurrent: | ||
Matt Mackall
|
r13350 | bookmarks.setcurrent(repo, None) | ||
David Soria Parra
|
r7239 | del marks[mark] | ||
Matt Mackall
|
r13350 | bookmarks.write(repo) | ||
David Soria Parra
|
r7239 | return | ||
Martin Geisler
|
r13031 | if mark is not None: | ||
Joel Rosdahl
|
r7259 | if "\n" in mark: | ||
raise util.Abort(_("bookmark name cannot contain newlines")) | ||||
Joel Rosdahl
|
r7260 | mark = mark.strip() | ||
Idan Kamara
|
r11704 | if not mark: | ||
raise util.Abort(_("bookmark names cannot consist entirely of " | ||||
"whitespace")) | ||||
David Soria Parra
|
r7239 | if mark in marks and not force: | ||
raise util.Abort(_("a bookmark of the same name already exists")) | ||||
Joel Rosdahl
|
r7250 | if ((mark in repo.branchtags() or mark == repo.dirstate.branch()) | ||
David Soria Parra
|
r7239 | and not force): | ||
Joel Rosdahl
|
r7252 | raise util.Abort( | ||
_("a bookmark cannot have the name of an existing branch")) | ||||
David Soria Parra
|
r7239 | if rev: | ||
marks[mark] = repo.lookup(rev) | ||||
else: | ||||
marks[mark] = repo.changectx('.').node() | ||||
Matt Mackall
|
r13350 | bookmarks.setcurrent(repo, mark) | ||
bookmarks.write(repo) | ||||
David Soria Parra
|
r7239 | return | ||
Martin Geisler
|
r8527 | if mark is None: | ||
Joel Rosdahl
|
r7258 | if rev: | ||
raise util.Abort(_("bookmark name required")) | ||||
David Soria Parra
|
r7239 | if len(marks) == 0: | ||
Benoit Boissinot
|
r10510 | ui.status(_("no bookmarks set\n")) | ||
David Soria Parra
|
r7239 | else: | ||
for bmark, n in marks.iteritems(): | ||||
David Soria Parra
|
r7481 | if ui.configbool('bookmarks', 'track.current'): | ||
Nicolas Dumazet
|
r10107 | current = repo._bookmarkcurrent | ||
Brodie Rao
|
r10820 | if bmark == current and n == cur: | ||
prefix, label = '*', 'bookmarks.current' | ||||
else: | ||||
prefix, label = ' ', '' | ||||
David Soria Parra
|
r7481 | else: | ||
Brodie Rao
|
r10820 | if n == cur: | ||
prefix, label = '*', 'bookmarks.current' | ||||
else: | ||||
prefix, label = ' ', '' | ||||
David Soria Parra
|
r7481 | |||
Steve Losh
|
r9459 | if ui.quiet: | ||
Brodie Rao
|
r10820 | ui.write("%s\n" % bmark, label=label) | ||
Steve Losh
|
r9459 | else: | ||
ui.write(" %s %-25s %d:%s\n" % ( | ||||
Brodie Rao
|
r10820 | prefix, bmark, repo.changelog.rev(n), hexfn(n)), | ||
label=label) | ||||
David Soria Parra
|
r7239 | return | ||
Matt Mackall
|
r11378 | def pull(oldpull, ui, repo, source="default", **opts): | ||
# translate bookmark args to rev args for actual pull | ||||
if opts.get('bookmark'): | ||||
# this is an unpleasant hack as pull will do this internally | ||||
source, branches = hg.parseurl(ui.expandpath(source), | ||||
opts.get('branch')) | ||||
other = hg.repository(hg.remoteui(repo, opts), source) | ||||
rb = other.listkeys('bookmarks') | ||||
for b in opts['bookmark']: | ||||
if b not in rb: | ||||
raise util.Abort(_('remote bookmark %s not found!') % b) | ||||
opts.setdefault('rev', []).append(b) | ||||
result = oldpull(ui, repo, source, **opts) | ||||
# update specified bookmarks | ||||
if opts.get('bookmark'): | ||||
for b in opts['bookmark']: | ||||
# explicit pull overrides local bookmark if any | ||||
ui.status(_("importing bookmark %s\n") % b) | ||||
repo._bookmarks[b] = repo[rb[b]].node() | ||||
Matt Mackall
|
r13350 | bookmarks.write(repo) | ||
Matt Mackall
|
r11378 | |||
return result | ||||
Matt Mackall
|
r11379 | def push(oldpush, ui, repo, dest=None, **opts): | ||
dopush = True | ||||
if opts.get('bookmark'): | ||||
dopush = False | ||||
for b in opts['bookmark']: | ||||
if b in repo._bookmarks: | ||||
dopush = True | ||||
opts.setdefault('rev', []).append(b) | ||||
result = 0 | ||||
if dopush: | ||||
result = oldpush(ui, repo, dest, **opts) | ||||
if opts.get('bookmark'): | ||||
# this is an unpleasant hack as push will do this internally | ||||
dest = ui.expandpath(dest or 'default-push', dest or 'default') | ||||
dest, branches = hg.parseurl(dest, opts.get('branch')) | ||||
other = hg.repository(hg.remoteui(repo, opts), dest) | ||||
rb = other.listkeys('bookmarks') | ||||
for b in opts['bookmark']: | ||||
# explicit push overrides remote bookmark if any | ||||
if b in repo._bookmarks: | ||||
ui.status(_("exporting bookmark %s\n") % b) | ||||
new = repo[b].hex() | ||||
David Soria Parra
|
r11994 | elif b in rb: | ||
Matt Mackall
|
r11379 | ui.status(_("deleting remote bookmark %s\n") % b) | ||
new = '' # delete | ||||
David Soria Parra
|
r11994 | else: | ||
Martin Geisler
|
r12146 | ui.warn(_('bookmark %s does not exist on the local ' | ||
'or remote repository!\n') % b) | ||||
David Soria Parra
|
r11994 | return 2 | ||
Matt Mackall
|
r11379 | old = rb.get(b, '') | ||
r = other.pushkey('bookmarks', b, old, new) | ||||
if not r: | ||||
ui.warn(_('updating bookmark %s failed!\n') % b) | ||||
if not result: | ||||
result = 2 | ||||
return result | ||||
David Soria Parra
|
r11431 | def incoming(oldincoming, ui, repo, source="default", **opts): | ||
if opts.get('bookmarks'): | ||||
source, branches = hg.parseurl(ui.expandpath(source), opts.get('branch')) | ||||
other = hg.repository(hg.remoteui(repo, opts), source) | ||||
ui.status(_('comparing with %s\n') % url.hidepassword(source)) | ||||
Matt Mackall
|
r13354 | return bookmarks.diff(ui, repo, other) | ||
David Soria Parra
|
r11431 | else: | ||
Matt Mackall
|
r11444 | return oldincoming(ui, repo, source, **opts) | ||
David Soria Parra
|
r11431 | |||
Matt Mackall
|
r11443 | def outgoing(oldoutgoing, ui, repo, dest=None, **opts): | ||
David Soria Parra
|
r11431 | if opts.get('bookmarks'): | ||
Matt Mackall
|
r11443 | dest = ui.expandpath(dest or 'default-push', dest or 'default') | ||
dest, branches = hg.parseurl(dest, opts.get('branch')) | ||||
other = hg.repository(hg.remoteui(repo, opts), dest) | ||||
ui.status(_('comparing with %s\n') % url.hidepassword(dest)) | ||||
Matt Mackall
|
r13354 | return bookmarks.diff(ui, other, repo) | ||
David Soria Parra
|
r11431 | else: | ||
Matt Mackall
|
r11444 | return oldoutgoing(ui, repo, dest, **opts) | ||
David Soria Parra
|
r11431 | |||
Matt Mackall
|
r7638 | def uisetup(ui): | ||
if ui.configbool('bookmarks', 'track.current'): | ||||
extensions.wrapcommand(commands.table, 'update', updatecurbookmark) | ||||
Matt Mackall
|
r11378 | |||
entry = extensions.wrapcommand(commands.table, 'pull', pull) | ||||
entry[1].append(('B', 'bookmark', [], | ||||
Will Maier
|
r12302 | _("bookmark to import"), | ||
_('BOOKMARK'))) | ||||
Matt Mackall
|
r11379 | entry = extensions.wrapcommand(commands.table, 'push', push) | ||
entry[1].append(('B', 'bookmark', [], | ||||
Will Maier
|
r12302 | _("bookmark to export"), | ||
_('BOOKMARK'))) | ||||
David Soria Parra
|
r11431 | entry = extensions.wrapcommand(commands.table, 'incoming', incoming) | ||
entry[1].append(('B', 'bookmarks', False, | ||||
_("compare bookmark"))) | ||||
entry = extensions.wrapcommand(commands.table, 'outgoing', outgoing) | ||||
entry[1].append(('B', 'bookmarks', False, | ||||
_("compare bookmark"))) | ||||
Matt Mackall
|
r11378 | |||
David Soria Parra
|
r7481 | def updatecurbookmark(orig, ui, repo, *args, **opts): | ||
'''Set the current bookmark | ||||
If the user updates to a bookmark we update the .hg/bookmarks.current | ||||
file. | ||||
''' | ||||
res = orig(ui, repo, *args, **opts) | ||||
rev = opts['rev'] | ||||
if not rev and len(args) > 0: | ||||
rev = args[0] | ||||
Matt Mackall
|
r13350 | bookmarks.setcurrent(repo, rev) | ||
David Soria Parra
|
r7481 | return res | ||
David Soria Parra
|
r7239 | cmdtable = { | ||
"bookmarks": | ||||
(bookmark, | ||||
[('f', 'force', False, _('force')), | ||||
FUJIWARA Katsunori
|
r11321 | ('r', 'rev', '', _('revision'), _('REV')), | ||
David Soria Parra
|
r7239 | ('d', 'delete', False, _('delete a given bookmark')), | ||
FUJIWARA Katsunori
|
r11321 | ('m', 'rename', '', _('rename a given bookmark'), _('NAME'))], | ||
Benoit Allard
|
r7818 | _('hg bookmarks [-f] [-d] [-m NAME] [-r REV] [NAME]')), | ||
David Soria Parra
|
r7239 | } | ||