Commit b8214858 authored by Steve Borho's avatar Steve Borho
Browse files

bookmarks: use hg-1.8 APIs, remove old hglib methods

--HG--
branch : hgtk
parent f0de3d1b37d2
......@@ -10,8 +10,7 @@
import gtk
import traceback
from mercurial import ui, util
from hgext import bookmarks
from mercurial import ui, util, commands
from tortoisehg.util.i18n import _
from tortoisehg.util import hglib, i18n, settings
......@@ -20,12 +19,10 @@ from tortoisehg.hgtk import dialog, gtklib
TYPE_ADDREMOVE = 1
TYPE_RENAME = 2
TYPE_CURRENT = 3
RESPONSE_ADD = 1
RESPONSE_REMOVE = 2
RESPONSE_RENAME = 3
RESPONSE_CURRENT = 4
RESPONSE_MOVE = 5
class BookmarkDialog(gtk.Dialog):
......@@ -50,8 +47,6 @@ class BookmarkDialog(gtk.Dialog):
self.add_button(_('Remove'), RESPONSE_REMOVE)
elif type == TYPE_RENAME:
self.add_button(_('Rename'), RESPONSE_RENAME)
elif type == TYPE_CURRENT:
self.add_button(_('Set Current'), RESPONSE_CURRENT)
else:
raise _('unexpected type: %s') % type
self.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
......@@ -72,40 +67,25 @@ class BookmarkDialog(gtk.Dialog):
table.add_row(label, self._bookmarklistbox, padding=False)
## add entry
if type == TYPE_CURRENT:
entry = None
else:
entry = gtk.Entry()
if type == TYPE_ADDREMOVE:
self._rev_input = entry
entry.set_width_chars(12)
entry.set_text(rev)
label = _('Revision:')
elif type == TYPE_RENAME:
self._name_input = entry
label = _('New Name:')
table.add_row(label, entry, padding=False)
# Option to make new bookmark the active one
trackcurrent = self.repo.ui.configbool('bookmarks', 'track.current')
if type == TYPE_ADDREMOVE and trackcurrent:
check = gtk.CheckButton(_('Make new/moved bookmark current'))
self.opt_newcurrent = check
check.set_sensitive(hglib.is_rev_current(self.repo, rev))
table.add_row(None, check)
entry = gtk.Entry()
if type == TYPE_ADDREMOVE:
self._rev_input = entry
entry.set_width_chars(12)
entry.set_text(rev)
label = _('Revision:')
elif type == TYPE_RENAME:
self._name_input = entry
label = _('New Name:')
table.add_row(label, entry, padding=False)
# signal handlers
self.connect('response', self.dialog_response)
self._bookmark_input.connect('activate', self.entry_activated, type)
if entry:
entry.connect('activate', self.entry_activated, type)
entry.connect('activate', self.entry_activated, type)
if type == TYPE_ADDREMOVE:
self._bookmark_input.connect('changed', self.bookmark_changed)
if trackcurrent:
self._rev_input.connect('changed', self.rev_changed)
# prepare to show
self.load_settings()
if type == TYPE_ADDREMOVE:
self.set_add_move_button_sensitivity()
self._refresh(clear=False)
......@@ -120,28 +100,15 @@ class BookmarkDialog(gtk.Dialog):
self._bookmarkslist.clear()
# add bookmarks to drop-down list
bookmarks = hglib.get_repo_bookmarks(self.repo)
bookmarks.sort()
for bookmarkname in bookmarks:
if bookmarkname != 'tip':
self._bookmarkslist.append([bookmarkname])
marks = self.repo._bookmarks.keys()
marks.sort()
for mark in marks:
self._bookmarkslist.append([mark])
# clear bookmark name input
if clear:
self._bookmark_input.set_text('')
def load_settings(self):
if hasattr(self, 'opt_newcurrent'):
newcurrent = self.settings.get_value('newcurrent', False)
self.opt_newcurrent.set_active(newcurrent)
def store_settings(self):
if hasattr(self, 'opt_newcurrent'):
newcurrent = self.opt_newcurrent.get_active()
self.settings.set_value('newcurrent', newcurrent)
self.settings.write()
def dialog_response(self, dialog, response_id):
# Add button
if response_id == RESPONSE_ADD:
......@@ -155,12 +122,9 @@ class BookmarkDialog(gtk.Dialog):
# Rename button
elif response_id == RESPONSE_RENAME:
self._do_rename_bookmark()
# Set Current button
elif response_id == RESPONSE_CURRENT:
self._do_current_bookmark()
# Close button or closed by the user
elif response_id in (gtk.RESPONSE_CLOSE, gtk.RESPONSE_DELETE_EVENT):
self.store_settings()
self.settings.write()
self.destroy()
return # close dialog
else:
......@@ -176,8 +140,6 @@ class BookmarkDialog(gtk.Dialog):
self.response(RESPONSE_MOVE)
elif type == TYPE_RENAME:
self.response(RESPONSE_RENAME)
elif type == TYPE_CURRENT:
self.response(RESPONSE_CURRENT)
else:
raise _('unexpected type: %s') % type
......@@ -190,7 +152,7 @@ class BookmarkDialog(gtk.Dialog):
def set_add_move_button_sensitivity(self):
mark = self._bookmark_input.get_text()
if mark in hglib.get_repo_bookmarks(self.repo):
if mark in self.repo._bookmarks:
self._button_add.set_sensitive(False)
self._button_move.set_sensitive(True)
else:
......@@ -215,10 +177,6 @@ class BookmarkDialog(gtk.Dialog):
self._add_hg_bookmark(name, rev)
dialog.info_dialog(self, _('Bookmarking completed'),
_('Bookmark "%s" has been added') % name)
if (hasattr(self, 'opt_newcurrent') and
self.opt_newcurrent.get_property('sensitive') and
self.opt_newcurrent.get_active()):
self._current_hg_bookmark(name)
self._refresh()
except util.Abort, inst:
dialog.error_dialog(self, _('Error in bookmarking'), str(inst))
......@@ -245,10 +203,6 @@ class BookmarkDialog(gtk.Dialog):
self._move_hg_bookmark(name, rev)
dialog.info_dialog(self, _('Bookmarking completed'),
_('Bookmark "%s" has been moved') % name)
if (hasattr(self, 'opt_newcurrent') and
self.opt_newcurrent.get_property('sensitive') and
self.opt_newcurrent.get_active()):
self._current_hg_bookmark(name)
self._refresh()
except util.Abort, inst:
dialog.error_dialog(self, _('Error in bookmarking'), str(inst))
......@@ -315,69 +269,27 @@ class BookmarkDialog(gtk.Dialog):
traceback.format_exc())
return False
def _do_current_bookmark(self):
# gather input data
name = self._bookmark_input.get_text()
# verify input
if name == '':
dialog.error_dialog(self, _('Bookmark input is empty'),
_('Please enter bookmark name'))
self._bookmark_input.grab_focus()
return False
# set current bookmark
try:
self._current_hg_bookmark(name)
dialog.info_dialog(self, _('Bookmarking completed'),
_('Bookmark "%s" has been made current') %
name)
self._refresh()
except util.Abort, inst:
dialog.error_dialog(self, _('Error in bookmarking'), str(inst))
return False
except:
dialog.error_dialog(self, _('Error in bookmarking'),
traceback.format_exc())
return False
def _add_hg_bookmark(self, name, revision):
if name in hglib.get_repo_bookmarks(self.repo):
repo = self.repo
if name in repo._bookmarks:
raise util.Abort(_('a bookmark named "%s" already exists') % name)
bookmarks.bookmark(ui=ui.ui(),
repo=self.repo,
rev=revision,
mark=name)
commands.bookmark(repo.ui, repo, name, revision, True)
def _move_hg_bookmark(self, name, revision):
if name not in hglib.get_repo_bookmarks(self.repo):
repo = self.repo
if name not in repo._bookmarks:
raise util.Abort(_('No bookmark named "%s" exists') % name)
bookmarks.bookmark(ui=ui.ui(),
repo=self.repo,
rev=revision,
mark=name,
force=True)
commands.bookmark(repo.ui, repo, name, revision, True)
def _remove_hg_bookmark(self, name):
if not name in hglib.get_repo_bookmarks(self.repo):
repo = self.repo
if not name in repo._bookmarks:
raise util.Abort(_("Bookmark '%s' does not exist") % name)
bookmarks.bookmark(ui=ui.ui(),
repo=self.repo,
mark=name,
delete=True)
commands.bookmark(repo.ui, repo, name, None, True, True)
def _rename_hg_bookmark(self, name, new_name):
if new_name in hglib.get_repo_bookmarks(self.repo):
repo = self.repo
if new_name in repo._bookmarks:
raise util.Abort(_('a bookmark named "%s" already exists') %
new_name)
bookmarks.bookmark(ui=ui.ui(),
repo=self.repo,
mark=new_name,
rename=name)
def _current_hg_bookmark(self, name):
if name not in hglib.get_repo_bookmarks(self.repo):
raise util.Abort(_('No bookmark named "%s" exists') % name)
bookmarks.setcurrent(self.repo, name)
commands.bookmark(repo.ui, repo, new_name, None, True, False, name)
......@@ -187,25 +187,14 @@ def getfontconfig(_ui=None):
def invalidaterepo(repo):
repo.dirstate.invalidate()
for attr in ('_bookmarks', '_bookmarkcurrent'):
if attr in repo.__dict__:
delattr(repo, attr)
if isinstance(repo, bundlerepo.bundlerepository):
# Work around a bug in hg-1.3. repo.invalidate() breaks
# overlay bundlerepos
return
repo.invalidate()
# way _bookmarks / _bookmarkcurrent cached changed
# from 1.4 to 1.5...
for cachedAttr in ('_bookmarks', '_bookmarkcurrent'):
# Check if it's a property or normal value...
if is_descriptor(repo, cachedAttr):
# The very act of calling hasattr would
# re-cache the property, so just assume it's
# already cached, and catch the error if it wasn't.
try:
delattr(repo, cachedAttr)
except AttributeError:
pass
elif hasattr(repo, cachedAttr):
setattr(repo, cachedAttr, None)
if 'mq' in repo.__dict__: #do not create if it does not exist
repo.mq.invalidate()
......@@ -445,74 +434,21 @@ def validate_synch_path(path, repo):
return_path = path_aux
return return_path
def get_repo_bookmarks(repo, values=False):
"""
Will return the bookmarks for the given repo if the
bookmarks extension is loaded.
By default, returns a list of bookmark names; if
values is True, returns a dict mapping names to
nodes.
If the extension is not loaded, returns an empty
list/dict.
"""
try:
bookmarks = extensions.find('bookmarks')
except KeyError:
return values and {} or []
if bookmarks:
# Bookmarks changed from 1.4 to 1.5...
if hasattr(bookmarks, 'parse'):
marks = bookmarks.parse(repo)
elif hasattr(repo, '_bookmarks'):
marks = repo._bookmarks
else:
marks = {}
else:
marks = {}
if values:
return marks
else:
return marks.keys()
def get_repo_bookmarkcurrent(repo):
"""
Will return the current bookmark for the given repo
if the bookmarks extension is loaded, and the
track.current option is on.
If the extension is not loaded, or track.current
is not set, returns None
"""
try:
bookmarks = extensions.find('bookmarks')
except KeyError:
return None
if bookmarks and repo.ui.configbool('bookmarks', 'track.current'):
# Bookmarks changed from 1.4 to 1.5...
if hasattr(bookmarks, 'current'):
return bookmarks.current(repo)
elif hasattr(repo, '_bookmarkcurrent'):
return repo._bookmarkcurrent
return None
def is_rev_current(repo, rev):
'''
Returns True if the revision indicated by 'rev' is the current
working directory parent.
If rev is '' or None, it is assumed to mean 'tip'.
'''
if rev in ('', None):
rev = 'tip'
rev = repo.lookup(rev)
parents = repo.parents()
if len(parents) > 1:
return False
return rev == parents[0].node()
def is_descriptor(obj, attr):
......@@ -528,15 +464,15 @@ def is_descriptor(obj, attr):
if attr in cls.__dict__:
return hasattr(cls.__dict__[attr], '__get__')
return None
def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
opts=None):
'''
export changesets as hg patches.
Mercurial moved patch.export to cmdutil.export after version 1.5
(change e764f24a45ee in mercurial).
'''
'''
try:
return cmdutil.export(repo, revs, template, fp, switch_parent, opts)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment