hgkw/keyword.py
changeset 818 b742a071ad9c
parent 816 00570f650792
child 819 8f2c2cc51789
equal deleted inserted replaced
817:cd834b4035ec 818:b742a071ad9c
   161         if kwmaps: # override default templates
   161         if kwmaps: # override default templates
   162             self.templates = dict((k, templater.parsestring(v, False))
   162             self.templates = dict((k, templater.parsestring(v, False))
   163                                   for k, v in kwmaps)
   163                                   for k, v in kwmaps)
   164         else:
   164         else:
   165             self.templates = _defaultkwmaps(self.ui)
   165             self.templates = _defaultkwmaps(self.ui)
   166         escaped = map(re.escape, self.templates.keys())
   166         escaped = '|'.join(map(re.escape, self.templates.keys()))
   167         kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped)
   167         self.re_kw = re.compile(r'\$(%s)\$' % escaped)
   168         self.re_kw = re.compile(kwpat)
   168         self.re_kwexp = re.compile(r'\$(%s): [^$\n\r]*? \$' % escaped)
   169 
   169 
   170         templatefilters.filters.update({'utcdate': utcdate,
   170         templatefilters.filters.update({'utcdate': utcdate,
   171                                         'svnisodate': svnisodate,
   171                                         'svnisodate': svnisodate,
   172                                         'svnutcdate': svnutcdate})
   172                                         'svnutcdate': svnutcdate})
   173 
   173 
   194     def iskwfile(self, cand, ctx):
   194     def iskwfile(self, cand, ctx):
   195         '''Returns subset of candidates which are configured for keyword
   195         '''Returns subset of candidates which are configured for keyword
   196         expansion are not symbolic links.'''
   196         expansion are not symbolic links.'''
   197         return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)]
   197         return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)]
   198 
   198 
   199     def overwrite(self, ctx, candidates, lookup, expand):
   199     def overwrite(self, ctx, candidates, lookup, expand, recsubn=None):
   200         '''Overwrites selected files expanding/shrinking keywords.'''
   200         '''Overwrites selected files expanding/shrinking keywords.'''
   201         if self.restrict or lookup: # exclude kw_copy
   201         if self.restrict or lookup: # exclude kw_copy
   202             candidates = self.iskwfile(candidates, ctx)
   202             candidates = self.iskwfile(candidates, ctx)
   203         if not candidates:
   203         if not candidates:
   204             return
   204             return
   205         commit = self.restrict and not lookup
   205         commit = self.restrict and not lookup
   206         if self.restrict or expand and lookup:
   206         if self.restrict or expand and lookup:
   207             mf = ctx.manifest()
   207             mf = ctx.manifest()
   208         fctx = ctx
   208         fctx = ctx
       
   209         subn = (self.restrict and self.re_kw.subn or
       
   210                 recsubn or self.re_kwexp.subn)
   209         msg = (expand and _('overwriting %s expanding keywords\n')
   211         msg = (expand and _('overwriting %s expanding keywords\n')
   210                or _('overwriting %s shrinking keywords\n'))
   212                or _('overwriting %s shrinking keywords\n'))
   211         for f in candidates:
   213         for f in candidates:
   212             if self.restrict:
   214             if self.restrict:
   213                 data = self.repo.file(f).read(mf[f])
   215                 data = self.repo.file(f).read(mf[f])
   216             if util.binary(data):
   218             if util.binary(data):
   217                 continue
   219                 continue
   218             if expand:
   220             if expand:
   219                 if lookup:
   221                 if lookup:
   220                     fctx = self.repo.filectx(f, fileid=mf[f]).changectx()
   222                     fctx = self.repo.filectx(f, fileid=mf[f]).changectx()
   221                 data, found = self.substitute(data, f, fctx, self.re_kw.subn)
   223                 data, found = self.substitute(data, f, fctx, subn)
   222             elif self.restrict:
   224             elif self.restrict:
   223                 found = self.re_kw.search(data)
   225                 found = self.re_kw.search(data)
   224             else:
   226             else:
   225                 data, found = _shrinktext(data, self.re_kw.subn)
   227                 data, found = _shrinktext(data, subn)
   226             if found:
   228             if found:
   227                 self.ui.note(msg % f)
   229                 self.ui.note(msg % f)
   228                 self.repo.wwrite(f, data, ctx.flags(f))
   230                 self.repo.wwrite(f, data, ctx.flags(f))
   229                 if commit:
   231                 if commit:
   230                     self.repo.dirstate.normal(f)
   232                     self.repo.dirstate.normal(f)
   232                     self.repo.dirstate.normallookup(f)
   234                     self.repo.dirstate.normallookup(f)
   233 
   235 
   234     def shrink(self, fname, text):
   236     def shrink(self, fname, text):
   235         '''Returns text with all keyword substitutions removed.'''
   237         '''Returns text with all keyword substitutions removed.'''
   236         if self.match(fname) and not util.binary(text):
   238         if self.match(fname) and not util.binary(text):
   237             return _shrinktext(text, self.re_kw.sub)
   239             return _shrinktext(text, self.re_kwexp.sub)
   238         return text
   240         return text
   239 
   241 
   240     def shrinklines(self, fname, lines):
   242     def shrinklines(self, fname, lines):
   241         '''Returns lines with keyword substitutions removed.'''
   243         '''Returns lines with keyword substitutions removed.'''
   242         if self.match(fname):
   244         if self.match(fname):
   243             text = ''.join(lines)
   245             text = ''.join(lines)
   244             if not util.binary(text):
   246             if not util.binary(text):
   245                 return _shrinktext(text, self.re_kw.sub).splitlines(True)
   247                 return _shrinktext(text, self.re_kwexp.sub).splitlines(True)
   246         return lines
   248         return lines
   247 
   249 
   248     def wread(self, fname, data):
   250     def wread(self, fname, data):
   249         '''If in restricted mode returns data read from wdir with
   251         '''If in restricted mode returns data read from wdir with
   250         keyword substitutions removed.'''
   252         keyword substitutions removed.'''
   567         wlock = repo.wlock()
   569         wlock = repo.wlock()
   568         try:
   570         try:
   569             # record returns 0 even when nothing has changed
   571             # record returns 0 even when nothing has changed
   570             # therefore compare nodes before and after
   572             # therefore compare nodes before and after
   571             ctx = repo['.']
   573             ctx = repo['.']
       
   574             modified, added = repo[None].status()[:2]
   572             ret = orig(ui, repo, commitfunc, *pats, **opts)
   575             ret = orig(ui, repo, commitfunc, *pats, **opts)
   573             recordctx = repo['.']
   576             recctx = repo['.']
   574             if ctx != recordctx:
   577             if ctx != recctx:
   575                 candidates = [f for f in recordctx.files() if f in recordctx]
   578                 modified = [f for f in modified if f in recctx]
       
   579                 added = [f for f in added if f in recctx]
   576                 kwt.restrict = False
   580                 kwt.restrict = False
   577                 kwt.overwrite(recordctx, candidates, False, True)
   581                 kwt.overwrite(recctx, modified, False, True, kwt.re_kwexp.subn)
       
   582                 kwt.overwrite(recctx, added, False, True, kwt.re_kw.subn)
   578                 kwt.restrict = True
   583                 kwt.restrict = True
   579             return ret
   584             return ret
   580         finally:
   585         finally:
   581             wlock.release()
   586             wlock.release()
   582 
   587