hgkw/keyword.py
changeset 261 28e64bebc298
parent 260 af9ddafeeb96
child 262 397a5957cc48
equal deleted inserted replaced
260:af9ddafeeb96 261:28e64bebc298
   298         if self.renamed(node):
   298         if self.renamed(node):
   299             t2 = super(kwfilelog, self).read(node)
   299             t2 = super(kwfilelog, self).read(node)
   300             return t2 != text
   300             return t2 != text
   301         return revlog.revlog.cmp(self, node, text)
   301         return revlog.revlog.cmp(self, node, text)
   302 
   302 
   303 def _keywordmatcher(ui, repo):
   303 def _weedcandidates(ui, man, candidates):
   304     '''Collects include/exclude filename patterns for expansion
       
   305     candidates of current configuration. Returns filename matching
       
   306     function if include patterns exist, None otherwise.'''
       
   307     inc, exc = [], ['.hgtags']
       
   308     for pat, opt in ui.configitems('keyword'):
       
   309         if opt != 'ignore':
       
   310             inc.append(pat)
       
   311         else:
       
   312             exc.append(pat)
       
   313     if inc:
       
   314         return util.matcher(repo.root, inc=inc, exc=exc)[1]
       
   315     return None
       
   316 
       
   317 def _weedcandidates(man, kwfmatcher, candidates):
       
   318     '''Weeds out files that do not match keyword file matcher,
   304     '''Weeds out files that do not match keyword file matcher,
   319     are not tracked, or are links.'''
   305     are not tracked, or are links.'''
   320     files = man.keys()
   306     files = man.keys()
   321     if candidates:
   307     if candidates:
   322         return [f for f in candidates if kwfmatcher(f)
   308         return [f for f in candidates if ui.kwfmatcher(f)
   323                 and f in files and not man.linkf(f)]
   309                 and f in files and not man.linkf(f)]
   324     return [f for f in files if kwfmatcher(f) and not man.linkf(f)]
   310     return [f for f in files if ui.kwfmatcher(f) and not man.linkf(f)]
   325 
   311 
   326 def _overwrite(ui, repo, files, expand):
   312 def _overwrite(ui, repo, files, expand):
   327     '''Expands/shrinks keywords in working directory.'''
   313     '''Expands/shrinks keywords in working directory.'''
       
   314     if not hasattr(ui, 'kwfmatcher'):
       
   315         ui.warn(_('no files configured for keyword expansion\n'))
       
   316         return
   328     wlock = lock = None
   317     wlock = lock = None
   329     try:
   318     try:
   330         wlock = repo.wlock()
   319         wlock = repo.wlock()
   331         lock = repo.lock()
   320         lock = repo.lock()
   332         bail_if_changed(repo)
   321         bail_if_changed(repo)
   333         ctx = repo.changectx()
   322         ctx = repo.changectx()
   334         kwfmatcher = _keywordmatcher(ui, repo)
       
   335         if kwfmatcher is None:
       
   336             ui.warn(_('no files configured for keyword expansion\n'))
       
   337             return
       
   338         man = ctx.manifest()
   323         man = ctx.manifest()
   339         if files:
   324         if files:
   340             cwd = repo.getcwd()
   325             cwd = repo.getcwd()
   341             files = [util.canonpath(repo.root, cwd, f) for f in files]
   326             files = [util.canonpath(repo.root, cwd, f) for f in files]
   342         files = _weedcandidates(man, kwfmatcher, files)
   327         files = _weedcandidates(ui, man, files)
   343         if files:
   328         if files:
   344             kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   329             kwt = kwtemplater(ui, repo, expand, node=ctx.node())
   345             # 3rd argument sets commit to False
   330             # 3rd argument sets commit to False
   346             kwt.overwrite(files, man, False)
   331             kwt.overwrite(files, man, False)
   347     finally:
   332     finally:
   369     '''print files currently configured for keyword expansion
   354     '''print files currently configured for keyword expansion
   370 
   355 
   371     Crosscheck which files in working directory
   356     Crosscheck which files in working directory
   372     are matched by [keyword] config patterns.
   357     are matched by [keyword] config patterns.
   373     '''
   358     '''
   374     kwfmatcher = _keywordmatcher(ui, repo)
   359     if hasattr(ui, 'kwfmatcher'):
   375     if kwfmatcher is not None:
       
   376         ctx = repo.workingctx()
   360         ctx = repo.workingctx()
   377         man = ctx.manifest()
   361         man = ctx.manifest().copy()
   378         for f in ctx.unknown():
   362         for f in ctx.unknown():
   379             del man[f]
   363             del man[f]
   380         files = _weedcandidates(man, kwfmatcher, None)
   364         files = _weedcandidates(ui, man, None)
   381         files.sort()
   365         files.sort()
   382         ui.write('\n'.join(files) + '\n')
   366         ui.write('\n'.join(files) + '\n')
   383 
   367 
   384 def demo(ui, repo, *args, **opts):
   368 def demo(ui, repo, *args, **opts):
   385     '''print [keywordmaps] configuration and an expansion example
   369     '''print [keywordmaps] configuration and an expansion example
   473                     'remove', 'rename', 'rollback']
   457                     'remove', 'rename', 'rollback']
   474 
   458 
   475     if not repo.local() or _parse(ui, sys.argv[1:])[0] in nokwcommands:
   459     if not repo.local() or _parse(ui, sys.argv[1:])[0] in nokwcommands:
   476         return
   460         return
   477 
   461 
   478     kwfmatcher = _keywordmatcher(ui, repo)
   462     inc, exc = [], ['.hgtags']
   479     if kwfmatcher is None:
   463     for pat, opt in ui.configitems('keyword'):
       
   464         if opt != 'ignore':
       
   465             inc.append(pat)
       
   466         else:
       
   467             exc.append(pat)
       
   468     if not inc:
   480         return
   469         return
       
   470 
       
   471     ui.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   481 
   472 
   482     class kwrepo(repo.__class__):
   473     class kwrepo(repo.__class__):
   483         def file(self, f, kwexp=True, kwcnt=False, kwmatch=False):
   474         def file(self, f, kwexp=True, kwcnt=False, kwmatch=False):
   484             if f[0] == '/':
   475             if f[0] == '/':
   485                 f = f[1:]
   476                 f = f[1:]
   486             if kwmatch or kwfmatcher(f):
   477             if kwmatch or ui.kwfmatcher(f):
   487                 kwt = kwtemplater(ui, self, kwexp, path=f)
   478                 kwt = kwtemplater(ui, self, kwexp, path=f)
   488                 return kwfilelog(self.sopener, f, kwt, kwcnt)
   479                 return kwfilelog(self.sopener, f, kwt, kwcnt)
   489             return filelog.filelog(self.sopener, f)
   480             return filelog.filelog(self.sopener, f)
   490 
   481 
   491         def _commit(self, files, text, user, date, match, force, lock, wlock,
   482         def _commit(self, files, text, user, date, match, force, lock, wlock,
   522                 node = self._commit(files, text, user, date, match, force,
   513                 node = self._commit(files, text, user, date, match, force,
   523                                     _lock, _wlock, force_editor, p1, p2, extra)
   514                                     _lock, _wlock, force_editor, p1, p2, extra)
   524                 if node is not None:
   515                 if node is not None:
   525                     cl = self.changelog.read(node)
   516                     cl = self.changelog.read(node)
   526                     mn = self.manifest.read(cl[0])
   517                     mn = self.manifest.read(cl[0])
   527                     candidates = _weedcandidates(mn, kwfmatcher, cl[3])
   518                     candidates = _weedcandidates(ui, mn, cl[3])
   528                     if candidates:
   519                     if candidates:
   529                         # 3rd argument sets expansion to True
   520                         # 3rd argument sets expansion to True
   530                         kwt = kwtemplater(ui, self, True, node=node)
   521                         kwt = kwtemplater(ui, self, True, node=node)
   531                         # 3rd argument sets commit to True
   522                         # 3rd argument sets commit to True
   532                         kwt.overwrite(candidates, mn, True)
   523                         kwt.overwrite(candidates, mn, True)