hgkw/keyword.py
changeset 203 20eaa6147391
parent 201 e826c3cdc52d
child 204 2d089b691b31
equal deleted inserted replaced
202:e2a2f9ca8271 203:20eaa6147391
    80 '''
    80 '''
    81 
    81 
    82 from mercurial import commands, cmdutil, context, fancyopts
    82 from mercurial import commands, cmdutil, context, fancyopts
    83 from mercurial import filelog, localrepo, templater, util, hg
    83 from mercurial import filelog, localrepo, templater, util, hg
    84 from mercurial.i18n import gettext as _
    84 from mercurial.i18n import gettext as _
    85 # findcmd might be in cmdutil or commands
    85 import os, re, shutil, sys, tempfile, time
    86 # depending on mercurial version
    86 
    87 if hasattr(cmdutil, 'findcmd'):
    87 # findcmd, bail_if_changed were in commands until 0c61124ad877
       
    88 try:
    88     findcmd = cmdutil.findcmd
    89     findcmd = cmdutil.findcmd
    89 else:
    90     bail_if_changed = cmdutil.bail_if_changed
       
    91 except AttributeError:
    90     findcmd = commands.findcmd
    92     findcmd = commands.findcmd
    91 import os, re, shutil, sys, tempfile, time
    93     bail_if_changed = commands.bail_if_changed
    92 
    94 
    93 commands.optionalrepo += ' kwdemo'
    95 commands.optionalrepo += ' kwdemo'
    94 
    96 
    95 deftemplates = {
    97 deftemplates = {
    96     'Revision': '{node|short}',
    98     'Revision': '{node|short}',
   263 
   265 
   264 def overwrite(ui, repo, files=None, expand=True):
   266 def overwrite(ui, repo, files=None, expand=True):
   265     '''Expands/shrinks keywords in working directory.'''
   267     '''Expands/shrinks keywords in working directory.'''
   266     wlock = repo.wlock()
   268     wlock = repo.wlock()
   267     try:
   269     try:
       
   270         bail_if_changed(repo)
   268         ctx = repo.changectx()
   271         ctx = repo.changectx()
   269         if not ctx:
   272         if not ctx:
   270             raise hg.RepoError(_('no changeset found'))
   273             raise hg.RepoError(_('no revision checked out'))
   271         for changed in repo.status()[:4]:
       
   272             if changed:
       
   273                 raise util.Abort(_('local changes detected'))
       
   274         kwfmatcher = keywordmatcher(ui, repo)
   274         kwfmatcher = keywordmatcher(ui, repo)
   275         if kwfmatcher is None:
   275         if kwfmatcher is None:
   276             ui.warn(_('no files configured for keyword expansion\n'))
   276             ui.warn(_('no files configured for keyword expansion\n'))
   277             return
   277             return
   278         m = ctx.manifest()
   278         m = ctx.manifest()
   280             files = [f for f in files if f in m.keys()]
   280             files = [f for f in files if f in m.keys()]
   281         else:
   281         else:
   282             files = m.keys()
   282             files = m.keys()
   283         files = [f for f in files if kwfmatcher(f) and not os.path.islink(f)]
   283         files = [f for f in files if kwfmatcher(f) and not os.path.islink(f)]
   284         if not files:
   284         if not files:
   285             ui.warn(_('given files not tracked or '
   285             ui.warn(_('files not configured for expansion or untracked\n'))
   286                       'not configured for expansion\n'))
       
   287             return
   286             return
   288         kwt = kwtemplater(ui, repo, node=ctx.node(), expand=expand)
   287         kwt = kwtemplater(ui, repo, node=ctx.node(), expand=expand)
   289         kwt.overwrite(files, m, commit=False)
   288         kwt.overwrite(files, m, commit=False)
   290     finally:
   289     finally:
   291         wlock.release()
   290         wlock.release()