hgkw/keyword.py
changeset 347 c912b8c3f3ff
parent 346 2c2b49def9d8
child 349 c78c74451bac
child 363 263b3b10c55f
equal deleted inserted replaced
346:2c2b49def9d8 347:c912b8c3f3ff
    86 from mercurial.i18n import _
    86 from mercurial.i18n import _
    87 import re, shutil, sys, tempfile, time
    87 import re, shutil, sys, tempfile, time
    88 
    88 
    89 commands.optionalrepo += ' kwdemo'
    89 commands.optionalrepo += ' kwdemo'
    90 
    90 
       
    91 # hg commands that trigger expansion only when writing to working dir,
       
    92 # not when reading filelog, and unexpand when reading from working dir
       
    93 restricted = ('diff1', 'record',
       
    94               'qfold', 'qimport', 'qnew', 'qpush', 'qrefresh', 'qrecord')
       
    95 
    91 def utcdate(date):
    96 def utcdate(date):
    92     '''Returns hgdate in cvs-like UTC format.'''
    97     '''Returns hgdate in cvs-like UTC format.'''
    93     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
    98     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
    94 
       
    95 def _kwrestrict(cmd):
       
    96     '''Returns True if cmd should trigger restricted expansion.
       
    97     Keywords will only expanded when writing to working dir.
       
    98     Crucial for mq as expanded keywords should not make it into patches.'''
       
    99     return cmd in ('diff1', 'record',
       
   100                    'qfold', 'qimport', 'qnew', 'qpush', 'qrefresh', 'qrecord')
       
   101 
    99 
   102 
   100 
   103 _kwtemplater = None
   101 _kwtemplater = None
   104 
   102 
   105 class kwtemplater(object):
   103 class kwtemplater(object):
   157 
   155 
   158         return subfunc(kwsub, data)
   156         return subfunc(kwsub, data)
   159 
   157 
   160     def expand(self, node, data):
   158     def expand(self, node, data):
   161         '''Returns data with keywords expanded.'''
   159         '''Returns data with keywords expanded.'''
   162         if util.binary(data) or _kwrestrict(self.hgcmd):
   160         if util.binary(data) or self.hgcmd in restricted:
   163             return data
   161             return data
   164         return self.substitute(node, data, self.re_kw.sub)
   162         return self.substitute(node, data, self.re_kw.sub)
   165 
   163 
   166     def process(self, node, data, expand):
   164     def process(self, node, data, expand):
   167         '''Returns a tuple: data, count.
   165         '''Returns a tuple: data, count.
   451                 return kwfilelog(self.sopener, f)
   449                 return kwfilelog(self.sopener, f)
   452             return filelog.filelog(self.sopener, f)
   450             return filelog.filelog(self.sopener, f)
   453 
   451 
   454         def wread(self, filename):
   452         def wread(self, filename):
   455             data = super(kwrepo, self).wread(filename)
   453             data = super(kwrepo, self).wread(filename)
   456             if _kwrestrict(hgcmd) and _kwtemplater.matcher(filename):
   454             if hgcmd in restricted and _kwtemplater.matcher(filename):
   457                 return _kwtemplater.shrink(data)
   455                 return _kwtemplater.shrink(data)
   458             return data
   456             return data
   459 
   457 
   460         def commit(self, files=None, text='', user=None, date=None,
   458         def commit(self, files=None, text='', user=None, date=None,
   461                    match=util.always, force=False, force_editor=False,
   459                    match=util.always, force=False, force_editor=False,