hgkw/keyword.py
branchkwmap-templates
changeset 173 5329863fb64e
parent 172 ad0da655bd12
child 174 c5f655d5d8b4
equal deleted inserted replaced
172:ad0da655bd12 173:5329863fb64e
    96         'Source': '{root}/{file},v',
    96         'Source': '{root}/{file},v',
    97         'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
    97         'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
    98         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
    98         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
    99         }
    99         }
   100 
   100 
   101 nokwcommands = ('add', 'remove', 'addremove', 'rollback',
   101 nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy', 'export',
   102                 'incoming', 'outgoing', 'export', 'bundle', 'push')
   102                 'incoming', 'outgoing', 'push', 'remove', 'rename', 'rollback')
   103 
   103 
   104 def utcdate(date):
   104 def utcdate(date):
   105     '''Returns hgdate in cvs-like UTC format.'''
   105     '''Returns hgdate in cvs-like UTC format.'''
   106     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   106     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   107 
   107 
   151         self.ui.pushbuffer()
   151         self.ui.pushbuffer()
   152         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   152         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   153         keywordsub = templater.firstline(self.ui.popbuffer())
   153         keywordsub = templater.firstline(self.ui.popbuffer())
   154         return '$%s: %s $' % (kw, keywordsub)
   154         return '$%s: %s $' % (kw, keywordsub)
   155 
   155 
   156     def expand(self, node, flog, data):
   156     def expand(self, node, data):
   157         '''Returns data with expanded keywords.'''
   157         '''Returns data with expanded keywords.'''
   158         if util.binary(data):
   158         if util.binary(data):
   159             return data
   159             return data
   160         c = context.filectx(self.repo, self.path, fileid=node, filelog=flog)
   160         c = context.filectx(self.repo, self.path, fileid=node)
   161         self.node = c.node()
   161         self.node = c.node()
   162         return self.re_kw.sub(self.kwsub, data)
   162         return self.re_kw.sub(self.kwsub, data)
   163 
   163 
   164     def shrink(self, text):
   164     def shrink(self, text):
   165         '''Returns text with all keyword substitutions removed.'''
   165         '''Returns text with all keyword substitutions removed.'''
   182         if files:
   182         if files:
   183             self.repo.dirstate.update(files, 'n')
   183             self.repo.dirstate.update(files, 'n')
   184 
   184 
   185 class kwfilelog(filelog.filelog):
   185 class kwfilelog(filelog.filelog):
   186     '''
   186     '''
   187     Superclass over filelog to customize its read, add, cmp methods.
   187     Subclass of filelog to hook into its read, add, cmp methods.
   188     Keywords are "stored" unexpanded, and expanded on reading.
   188     Keywords are "stored" unexpanded, and expanded on reading.
   189     '''
   189     '''
   190     def __init__(self, opener, path, kwtemplater):
   190     def __init__(self, opener, path, kwtemplater):
   191         super(kwfilelog, self).__init__(opener, path)
   191         super(kwfilelog, self).__init__(opener, path)
   192         self.kwtemplater = kwtemplater
   192         self.kwtemplater = kwtemplater
   193 
   193 
   194     def read(self, node):
   194     def read(self, node):
   195         '''Substitutes keywords when reading filelog.'''
   195         '''Substitutes keywords when reading filelog.'''
   196         data = super(kwfilelog, self).read(node)
   196         data = super(kwfilelog, self).read(node)
   197         return self.kwtemplater.expand(node, super(kwfilelog, self), data)
   197         return self.kwtemplater.expand(node, data)
   198 
   198 
   199     def add(self, text, meta, tr, link, p1=None, p2=None):
   199     def add(self, text, meta, tr, link, p1=None, p2=None):
   200         '''Removes keyword substitutions when adding to filelog.'''
   200         '''Removes keyword substitutions when adding to filelog.'''
   201         text = self.kwtemplater.shrink(text)
   201         text = self.kwtemplater.shrink(text)
   202         return super(kwfilelog, self).add(text, meta, tr, link, p1=p1, p2=p2)
   202         return super(kwfilelog, self).add(text, meta, tr, link, p1=p1, p2=p2)
   229         return
   229         return
   230 
   230 
   231     kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   231     kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   232 
   232 
   233     class kwrepo(repo.__class__):
   233     class kwrepo(repo.__class__):
       
   234         '''
       
   235         Subclass of repo's class attribute to wrap its file and commit methods.
       
   236         '''
       
   237 
   234         def file(self, f):
   238         def file(self, f):
       
   239             '''Opens kwfilelog instead of filelog if needed.'''
   235             if f[0] == '/':
   240             if f[0] == '/':
   236                 f = f[1:]
   241                 f = f[1:]
   237             # only use kwfilelog when needed
       
   238             if kwfmatcher(f):
   242             if kwfmatcher(f):
   239                 kwt = kwtemplater(repo.ui, self, path=f)
   243                 kwt = kwtemplater(repo.ui, self, path=f)
   240                 return kwfilelog(self.sopener, f, kwt)
   244                 return kwfilelog(self.sopener, f, kwt)
   241             else:
   245             else:
   242                 return filelog.filelog(self.sopener, f)
   246                 return filelog.filelog(self.sopener, f)