hgkw/keyword.py
changeset 506 7d5c8c734b74
parent 505 a93358a05b80
child 507 51296bd1652f
equal deleted inserted replaced
503:5cf987b0f3a1 506:7d5c8c734b74
   139 
   139 
   140         templatefilters.filters['utcdate'] = utcdate
   140         templatefilters.filters['utcdate'] = utcdate
   141         self.ct = cmdutil.changeset_templater(self.ui, self.repo,
   141         self.ct = cmdutil.changeset_templater(self.ui, self.repo,
   142                                               False, '', False)
   142                                               False, '', False)
   143 
   143 
   144     def getnode(self, path, fnode):
   144     def substitute(self, data, path, ctx, subfunc):
   145         '''Derives changenode from file path and filenode.'''
       
   146         # used by kwfilelog.read and kwexpand
       
   147         c = self.repo.filectx(path, fileid=fnode)
       
   148         return c.node()
       
   149 
       
   150     def substitute(self, data, path, node, subfunc):
       
   151         '''Replaces keywords in data with expanded template.'''
   145         '''Replaces keywords in data with expanded template.'''
   152         def kwsub(mobj):
   146         def kwsub(mobj):
   153             kw = mobj.group(1)
   147             kw = mobj.group(1)
   154             self.ct.use_template(self.templates[kw])
   148             self.ct.use_template(self.templates[kw])
   155             self.ui.pushbuffer()
   149             self.ui.pushbuffer()
   156             self.ct.show(changenode=node, root=self.repo.root, file=path)
   150             self.ct.show(ctx, root=self.repo.root, file=path)
   157             ekw = templatefilters.firstline(self.ui.popbuffer())
   151             ekw = templatefilters.firstline(self.ui.popbuffer())
   158             return '$%s: %s $' % (kw, ekw)
   152             return '$%s: %s $' % (kw, ekw)
   159         return subfunc(kwsub, data)
   153         return subfunc(kwsub, data)
   160 
   154 
   161     def expand(self, path, node, data):
   155     def expand(self, path, node, data):
   162         '''Returns data with keywords expanded.'''
   156         '''Returns data with keywords expanded.'''
   163         if not self.restrict and self.matcher(path) and not util.binary(data):
   157         if not self.restrict and self.matcher(path) and not util.binary(data):
   164             changenode = self.getnode(path, node)
   158             ctx = self.repo.filectx(path, fileid=node).changectx()
   165             return self.substitute(data, path, changenode, self.re_kw.sub)
   159             return self.substitute(data, path, ctx, self.re_kw.sub)
   166         return data
   160         return data
   167 
   161 
   168     def iskwfile(self, path, flagfunc):
   162     def iskwfile(self, path, flagfunc):
   169         '''Returns true if path matches [keyword] pattern
   163         '''Returns true if path matches [keyword] pattern
   170         and is not a symbolic link.
   164         and is not a symbolic link.
   177             ctx = self.repo[node]
   171             ctx = self.repo[node]
   178             mf = ctx.manifest()
   172             mf = ctx.manifest()
   179             files = [f for f in ctx.files() if f in mf]
   173             files = [f for f in ctx.files() if f in mf]
   180             notify = self.ui.debug
   174             notify = self.ui.debug
   181         else:                    # kwexpand/kwshrink
   175         else:                    # kwexpand/kwshrink
   182             ctx = self.repo['.']
   176             ctx = self.repo[None]
   183             mf = ctx.manifest()
   177             mf = ctx.manifest()
   184             notify = self.ui.note
   178             notify = self.ui.note
   185         candidates = [f for f in files if self.iskwfile(f, ctx.flags)]
   179         candidates = [f for f in files if self.iskwfile(f, ctx.flags)]
   186         if candidates:
   180         if candidates:
   187             self.restrict = True # do not expand when reading
   181             self.restrict = True # do not expand when reading
   190                 fp = self.repo.file(f)
   184                 fp = self.repo.file(f)
   191                 data = fp.read(mf[f])
   185                 data = fp.read(mf[f])
   192                 if util.binary(data):
   186                 if util.binary(data):
   193                     continue
   187                     continue
   194                 if expand:
   188                 if expand:
   195                     changenode = node or self.getnode(f, mf[f])
   189                     if node is None:
   196                     data, found = self.substitute(data, f, changenode,
   190                         ctx = self.repo.filectx(f, fileid=mf[f]).changectx()
       
   191                     data, found = self.substitute(data, f, ctx,
   197                                                   self.re_kw.subn)
   192                                                   self.re_kw.subn)
   198                 else:
   193                 else:
   199                     found = self.re_kw.search(data)
   194                     found = self.re_kw.search(data)
   200                 if found:
   195                 if found:
   201                     notify(_('overwriting %s %s keywords\n') % (f, action))
   196                     notify(_('overwriting %s %s keywords\n') % (f, action))