hgext3rd/topic/__init__.py
changeset 2899 32306ee32806
parent 2898 3dfc88c06378
child 2900 1928e9c802dd
equal deleted inserted replaced
2898:3dfc88c06378 2899:32306ee32806
    55 import re
    55 import re
    56 import time
    56 import time
    57 
    57 
    58 from mercurial.i18n import _
    58 from mercurial.i18n import _
    59 from mercurial import (
    59 from mercurial import (
       
    60     bookmarks,
    60     cmdutil,
    61     cmdutil,
    61     commands,
    62     commands,
    62     context,
    63     context,
    63     error,
    64     error,
    64     extensions,
    65     extensions,
   436     if topic is None and repo.currenttopic:
   437     if topic is None and repo.currenttopic:
   437         topic = repo.currenttopic
   438         topic = repo.currenttopic
   438     if topic is None:
   439     if topic is None:
   439         branch = repo[None].branch()
   440         branch = repo[None].branch()
   440     return stack.showstack(ui, repo, branch=branch, topic=topic, opts=opts)
   441     return stack.showstack(ui, repo, branch=branch, topic=topic, opts=opts)
       
   442 
       
   443 @command('debugcb|debugconvertbookmark', [
       
   444         ('b', 'bookmark', '', _('bookmark to convert to topic')),
       
   445         ('', 'all', None, _('convert all bookmarks to topics')),
       
   446     ],
       
   447     _('[-b BOOKMARK] [--all]'))
       
   448 def debugconvertbookmark(ui, repo, **opts):
       
   449     """Converts a bookmark to a topic with the same name.
       
   450     """
       
   451 
       
   452     bookmark = opts.get('bookmark')
       
   453     convertall = opts.get('all')
       
   454 
       
   455     if convertall and bookmark:
       
   456         raise error.Abort(_("cannot use '--all' and '-b' together"))
       
   457     if not (convertall or bookmark):
       
   458         raise error.Abort(_("you must specify either '--all' or '-b'"))
       
   459 
       
   460     bmstore = repo._bookmarks
       
   461     lock = wlock = tr = None
       
   462 
       
   463     if bookmark:
       
   464         try:
       
   465             node = bmstore[bookmark]
       
   466         except KeyError:
       
   467             raise error.Abort(_("no such bookmark exists: '%s'") % bookmark)
       
   468 
       
   469         revnum = repo[node].rev()
       
   470         try:
       
   471             wlock = repo.wlock()
       
   472             lock = repo.lock()
       
   473             tr = repo.transaction('debugconvertbookmark')
       
   474             _convertbmarktopic(ui, repo, revnum, bookmark, tr)
       
   475             tr.close()
       
   476         finally:
       
   477             lockmod.release(tr, lock, wlock)
       
   478 
       
   479     elif convertall:
       
   480         # deletion of bookmark will result in change in size of bmstore during
       
   481         # iteration, so let's make a copy to iterate
       
   482         storecopy = bmstore.copy()
       
   483         try:
       
   484             wlock = repo.wlock()
       
   485             lock = repo.lock()
       
   486             tr = repo.transaction('debugconvertbookmark')
       
   487             for bmark, revnode in sorted(storecopy.iteritems()):
       
   488                 if bmark == '@':
       
   489                     continue
       
   490                 _convertbmarktopic(ui, repo, repo[revnode].rev(), bmark, tr)
       
   491             tr.close()
       
   492         finally:
       
   493             lockmod.release(tr, lock, wlock)
       
   494 
       
   495 def _convertbmarktopic(ui, repo, rev, bmark, tr):
       
   496     """Sets a topic as same as bname to all the changesets under the bookmark
       
   497     and delete the bookmark, if topic is set to any changeset
       
   498 
       
   499     rev is the revision on which bookmark bmark is and tr is transaction object.
       
   500     """
       
   501 
       
   502     # copied from mercurial.repair.stripbmrevset
       
   503     bookrevset = ("ancestors(bookmark(%s)) - ancestors(head() and not "
       
   504                   "bookmark(%s)) - ancestors(bookmark() and not "
       
   505                   "bookmark(%s))")
       
   506     touchedrevs = repo.revs(bookrevset, bmark, bmark, bmark)
       
   507     rewrote = _changetopics(ui, repo, touchedrevs, bmark)
       
   508     # We didn't changed topic to any changesets because the revset
       
   509     # returned an empty set of revisions, so let's skip deleting the
       
   510     # bookmark corresponding to which we didn't put a topic on any
       
   511     # changeset
       
   512     if rewrote == 0:
       
   513         return
       
   514     ui.status(_('changed topic to "%s" on %d revisions\n') % (bmark,
       
   515               rewrote))
       
   516     ui.debug('removing bookmark "%s" from "%d"' % (bmark, rev))
       
   517     bookmarks.delete(repo, tr, [bmark])
   441 
   518 
   442 def _changecurrenttopic(repo, newtopic):
   519 def _changecurrenttopic(repo, newtopic):
   443     """changes the current topic."""
   520     """changes the current topic."""
   444 
   521 
   445     if newtopic:
   522     if newtopic: