hgkw/keyword.py
changeset 330 c5bba07dadd5
parent 328 f5154819a11b
child 331 178f3e9ff5eb
equal deleted inserted replaced
328:f5154819a11b 330:c5bba07dadd5
   107 
   107 
   108     def __init__(self, ui, repo, inc, exc):
   108     def __init__(self, ui, repo, inc, exc):
   109         self.ui = ui
   109         self.ui = ui
   110         self.repo = repo
   110         self.repo = repo
   111         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   111         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   112         self.node = None
   112         self.commitnode = None
   113         self.path = ''
   113         self.path = ''
   114 
   114 
   115         kwmaps = self.ui.configitems('keywordmaps')
   115         kwmaps = self.ui.configitems('keywordmaps')
   116         if kwmaps: # override default templates
   116         if kwmaps: # override default templates
   117             kwmaps = [(k, templater.parsestring(v, quoted=False))
   117             kwmaps = [(k, templater.parsestring(v, quoted=False))
   124         templater.common_filters['utcdate'] = utcdate
   124         templater.common_filters['utcdate'] = utcdate
   125         self.ct = cmdutil.changeset_templater(self.ui, self.repo,
   125         self.ct = cmdutil.changeset_templater(self.ui, self.repo,
   126                                               False, '', False)
   126                                               False, '', False)
   127 
   127 
   128     def substitute(self, node, data, subfunc):
   128     def substitute(self, node, data, subfunc):
   129         '''Obtains node if missing, and calls given substitution function.'''
   129         '''Obtains file's changenode if commit node not given,
   130         if not self.node:
   130         and calls given substitution function.'''
       
   131         if self.commitnode:
       
   132             fnode = self.commitnode
       
   133         else:
   131             c = context.filectx(self.repo, self.path, fileid=node)
   134             c = context.filectx(self.repo, self.path, fileid=node)
   132             self.node = c.node()
   135             fnode = c.node()
   133 
   136 
   134         def kwsub(mobj):
   137         def kwsub(mobj):
   135             '''Substitutes keyword using corresponding template.'''
   138             '''Substitutes keyword using corresponding template.'''
   136             kw = mobj.group(1)
   139             kw = mobj.group(1)
   137             self.ct.use_template(self.templates[kw])
   140             self.ct.use_template(self.templates[kw])
   138             self.ui.pushbuffer()
   141             self.ui.pushbuffer()
   139             self.ct.show(changenode=self.node,
   142             self.ct.show(changenode=fnode, root=self.repo.root, file=self.path)
   140                          root=self.repo.root, file=self.path)
       
   141             return '$%s: %s $' % (kw, templater.firstline(self.ui.popbuffer()))
   143             return '$%s: %s $' % (kw, templater.firstline(self.ui.popbuffer()))
   142 
   144 
   143         return subfunc(kwsub, data)
   145         return subfunc(kwsub, data)
   144 
   146 
   145     def expand(self, node, data):
   147     def expand(self, node, data):
   228 
   230 
   229 def _overwrite(ui, repo, node=None, expand=True, files=None):
   231 def _overwrite(ui, repo, node=None, expand=True, files=None):
   230     '''Overwrites selected files expanding/shrinking keywords.'''
   232     '''Overwrites selected files expanding/shrinking keywords.'''
   231     ctx = repo.changectx(node)
   233     ctx = repo.changectx(node)
   232     mf = ctx.manifest()
   234     mf = ctx.manifest()
   233     if files is None:
   235     if node is not None:   # commit
   234         notify = ui.debug # commit
   236         _kwtemplater.commitnode = node
   235         files = [f for f in ctx.files() if mf.has_key(f)]
   237         files = [f for f in ctx.files() if mf.has_key(f)]
   236     else:
   238         notify = ui.debug
   237         notify = ui.note  # kwexpand/kwshrink
   239     else:                  # kwexpand/kwshrink
       
   240         notify = ui.note
   238     candidates = [f for f in files if _iskwfile(f, mf.linkf)]
   241     candidates = [f for f in files if _iskwfile(f, mf.linkf)]
   239     if candidates:
   242     if candidates:
   240         candidates.sort()
   243         candidates.sort()
   241         action = expand and 'expanding' or 'shrinking'
   244         action = expand and 'expanding' or 'shrinking'
   242         _kwtemplater.node = node or ctx.node()
       
   243         for f in candidates:
   245         for f in candidates:
   244             fp = repo.file(f, kwmatch=True)
   246             fp = repo.file(f, kwmatch=True)
   245             data, kwfound = fp.kwctread(mf[f], expand)
   247             data, kwfound = fp.kwctread(mf[f], expand)
   246             if kwfound:
   248             if kwfound:
   247                 notify(_('overwriting %s %s keywords\n') % (f, action))
   249                 notify(_('overwriting %s %s keywords\n') % (f, action))