hgkw/keyword.py
changeset 213 948500df7c55
parent 211 110c76a94d1c
child 216 f35a0c382168
equal deleted inserted replaced
212:0ac36a08c320 213:948500df7c55
    81 '''
    81 '''
    82 
    82 
    83 from mercurial import commands, cmdutil, context, fancyopts
    83 from mercurial import commands, cmdutil, context, fancyopts
    84 from mercurial import filelog, localrepo, templater, util, hg
    84 from mercurial import filelog, localrepo, templater, util, hg
    85 from mercurial.i18n import gettext as _
    85 from mercurial.i18n import gettext as _
    86 import os, re, shutil, sys, tempfile, time
    86 import re, shutil, sys, tempfile, time
    87 
    87 
    88 # findcmd, bail_if_changed were in commands until 0c61124ad877
    88 # findcmd, bail_if_changed were in commands until 0c61124ad877
    89 try:
    89 try:
    90     findcmd = cmdutil.findcmd
    90     findcmd = cmdutil.findcmd
    91     bail_if_changed = cmdutil.bail_if_changed
    91     bail_if_changed = cmdutil.bail_if_changed
   251             exc.append(pat)
   251             exc.append(pat)
   252     if inc:
   252     if inc:
   253         return util.matcher(repo.root, inc=inc, exc=exc)[1]
   253         return util.matcher(repo.root, inc=inc, exc=exc)[1]
   254     return None
   254     return None
   255 
   255 
       
   256 def _weedcandidates(man, kwfmatcher, candidates):
       
   257     '''Weeds out files that do not match keyword file matcher,
       
   258     are not tracked, or are links.'''
       
   259     files = man.keys()
       
   260     if candidates:
       
   261         return [f for f in candidates if kwfmatcher(f)
       
   262                 and f in files and not man.linkf(f)]
       
   263     # kwexpand w/o args on all files in manifest
       
   264     return [f for f in files if kwfmatcher(f) and not man.linkf(f)]
       
   265 
   256 def _overwrite(ui, repo, files, expand):
   266 def _overwrite(ui, repo, files, expand):
   257     '''Expands/shrinks keywords in working directory.'''
   267     '''Expands/shrinks keywords in working directory.'''
   258     wlock = lock = None
   268     wlock = lock = None
   259     try:
   269     try:
   260         wlock = repo.wlock()
   270         wlock = repo.wlock()
   265             raise hg.RepoError(_('no revision checked out'))
   275             raise hg.RepoError(_('no revision checked out'))
   266         kwfmatcher = _keywordmatcher(ui, repo)
   276         kwfmatcher = _keywordmatcher(ui, repo)
   267         if kwfmatcher is None:
   277         if kwfmatcher is None:
   268             ui.warn(_('no files configured for keyword expansion\n'))
   278             ui.warn(_('no files configured for keyword expansion\n'))
   269             return
   279             return
   270         m = ctx.manifest()
   280         man = ctx.manifest()
   271         if files:
   281         files = _weedcandidates(man, kwfmatcher, files)
   272             files = [f for f in files if f in m.keys()]
       
   273         else:
       
   274             files = m.keys()
       
   275         if hasattr(repo, '_link'):
       
   276             files = [f for f in files if kwfmatcher(f) and not repo._link(f)]
       
   277         else:
       
   278             files = [f for f in files if kwfmatcher(f)
       
   279                      and not os.path.islink(repo.wjoin(f))]
       
   280         if not files:
   282         if not files:
   281             ui.warn(_('files not configured for expansion or untracked\n'))
   283             ui.warn(_('files not configured for expansion or untracked\n'))
   282             return
   284             return
   283         commit = False
   285         commit = False
   284         kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   286         kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   285         kwt.overwrite(files, m, commit)
   287         kwt.overwrite(files, man, commit)
   286     finally:
   288     finally:
   287         del wlock, lock
   289         del wlock, lock
   288 
   290 
   289 
   291 
   290 def shrink(ui, repo, *args):
   292 def shrink(ui, repo, *args):
   457                                               match=match, force=force,
   459                                               match=match, force=force,
   458                                               force_editor=force_editor,
   460                                               force_editor=force_editor,
   459                                               p1=p1, p2=p2, extra=extra)
   461                                               p1=p1, p2=p2, extra=extra)
   460                 if node is not None:
   462                 if node is not None:
   461                     cl = self.changelog.read(node)
   463                     cl = self.changelog.read(node)
   462                     if hasattr(self, '_link'):
   464                     mn = self.manifest.read(cl[0])
   463                         candidates = [f for f in cl[3] if kwfmatcher(f)
   465                     candidates = _weedcandidates(mn, kwfmatcher, cl[3])
   464                                       and f not in removed
       
   465                                       and not self._link(f)]
       
   466                     else:
       
   467                         candidates = [f for f in cl[3] if kwfmatcher(f)
       
   468                                       and f not in removed
       
   469                                       and not os.path.islink(self.wjoin(f))]
       
   470                     if candidates:
   466                     if candidates:
   471                         mn = self.manifest.read(cl[0])
       
   472                         expand = commit = True
   467                         expand = commit = True
   473                         kwt = kwtemplater(ui, self, expand, node=node)
   468                         kwt = kwtemplater(ui, self, expand, node=node)
   474                         kwt.overwrite(candidates, mn, commit)
   469                         kwt.overwrite(candidates, mn, commit)
   475                 return node
   470                 return node
   476             finally:
   471             finally:
   482 cmdtable = {
   477 cmdtable = {
   483     'kwdemo':
   478     'kwdemo':
   484         (demo,
   479         (demo,
   485          [('d', 'default', None, _('show default keyword template maps')),
   480          [('d', 'default', None, _('show default keyword template maps')),
   486           ('f', 'rcfile', [], _('read maps from RCFILE'))],
   481           ('f', 'rcfile', [], _('read maps from RCFILE'))],
   487          _('hg kwdemo [-d || [-f RCFILE] TEMPLATEMAP ...]')),
   482          _('hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP ...]')),
   488     'kwshrink': (shrink, [], _('hg kwshrink [NAME] ...')),
   483     'kwshrink': (shrink, [], _('hg kwshrink [NAME] ...')),
   489     'kwexpand': (expand, [], _('hg kwexpand [NAME] ...')),
   484     'kwexpand': (expand, [], _('hg kwexpand [NAME] ...')),
   490 }
   485 }