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