hgkw/keyword.py
branchstable
changeset 374 03fc5e667518
parent 373 56573f49772c
child 375 ed160b097bad
equal deleted inserted replaced
373:56573f49772c 374:03fc5e667518
    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 do not act on keywords
       
    92 nokwcommands = ('add addremove bundle copy export grep identify incoming init'
       
    93                 'log outgoing push remove rename rollback tip convert')
       
    94 
    91 # hg commands that trigger expansion only when writing to working dir,
    95 # hg commands that trigger expansion only when writing to working dir,
    92 # not when reading filelog, and unexpand when reading from working dir
    96 # not when reading filelog, and unexpand when reading from working dir
    93 restricted = ('diff1', 'record',
    97 restricted = 'diff1 record qfold qimport qnew qpush qrefresh qrecord'
    94               'qfold', 'qimport', 'qnew', 'qpush', 'qrefresh', 'qrecord')
       
    95 
    98 
    96 def utcdate(date):
    99 def utcdate(date):
    97     '''Returns hgdate in cvs-like UTC format.'''
   100     '''Returns hgdate in cvs-like UTC format.'''
    98     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   101     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
    99 
   102 
   113         'Source': '{root}/{file},v',
   116         'Source': '{root}/{file},v',
   114         'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
   117         'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
   115         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
   118         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
   116     }
   119     }
   117 
   120 
   118     def __init__(self, ui, repo, inc, exc, hgcmd):
   121     def __init__(self, ui, repo, inc, exc, restricted):
   119         self.ui = ui
   122         self.ui = ui
   120         self.repo = repo
   123         self.repo = repo
   121         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   124         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
   122         self.hgcmd = hgcmd
   125         self.restricted = restricted
   123         self.commitnode = None
   126         self.commitnode = None
   124         self.path = ''
   127         self.path = ''
   125 
   128 
   126         kwmaps = self.ui.configitems('keywordmaps')
   129         kwmaps = self.ui.configitems('keywordmaps')
   127         if kwmaps: # override default templates
   130         if kwmaps: # override default templates
   156 
   159 
   157         return subfunc(kwsub, data)
   160         return subfunc(kwsub, data)
   158 
   161 
   159     def expand(self, node, data):
   162     def expand(self, node, data):
   160         '''Returns data with keywords expanded.'''
   163         '''Returns data with keywords expanded.'''
   161         if util.binary(data) or self.hgcmd in restricted:
   164         if self.restricted or util.binary(data):
   162             return data
   165             return data
   163         return self.substitute(node, data, self.re_kw.sub)
   166         return self.substitute(node, data, self.re_kw.sub)
   164 
   167 
   165     def process(self, node, data, expand):
   168     def process(self, node, data, expand):
   166         '''Returns a tuple: data, count.
   169         '''Returns a tuple: data, count.
   410     files configured at all for keyword substitution.'''
   413     files configured at all for keyword substitution.'''
   411 
   414 
   412     if not repo.local():
   415     if not repo.local():
   413         return
   416         return
   414 
   417 
   415     nokwcommands = ('add', 'addremove', 'bundle', 'copy', 'export', 'grep',
       
   416                     'identify', 'incoming', 'init', 'log', 'outgoing', 'push',
       
   417                     'remove', 'rename', 'rollback', 'tip',
       
   418                     'convert')
       
   419     hgcmd, func, args, opts, cmdopts = dispatch._parse(ui, sys.argv[1:])
   418     hgcmd, func, args, opts, cmdopts = dispatch._parse(ui, sys.argv[1:])
   420     if hgcmd in nokwcommands:
   419     if hgcmd in nokwcommands.split():
   421         return
   420         return
   422 
   421 
   423     if hgcmd == 'diff':
   422     if hgcmd == 'diff':
   424         # only expand if comparing against working dir
   423         # only expand if comparing against working dir
   425         node1, node2 = cmdutil.revpair(repo, cmdopts.get('rev'))
   424         node1, node2 = cmdutil.revpair(repo, cmdopts.get('rev'))
   437             exc.append(pat)
   436             exc.append(pat)
   438     if not inc:
   437     if not inc:
   439         return
   438         return
   440 
   439 
   441     global _kwtemplater
   440     global _kwtemplater
   442     _kwtemplater = kwtemplater(ui, repo, inc, exc, hgcmd)
   441     _restricted = hgcmd in restricted.split()
       
   442     _kwtemplater = kwtemplater(ui, repo, inc, exc, _restricted)
   443 
   443 
   444     class kwrepo(repo.__class__):
   444     class kwrepo(repo.__class__):
   445         def file(self, f, kwmatch=False):
   445         def file(self, f, kwmatch=False):
   446             if f[0] == '/':
   446             if f[0] == '/':
   447                 f = f[1:]
   447                 f = f[1:]
   449                 return kwfilelog(self.sopener, f)
   449                 return kwfilelog(self.sopener, f)
   450             return filelog.filelog(self.sopener, f)
   450             return filelog.filelog(self.sopener, f)
   451 
   451 
   452         def wread(self, filename):
   452         def wread(self, filename):
   453             data = super(kwrepo, self).wread(filename)
   453             data = super(kwrepo, self).wread(filename)
   454             if hgcmd in restricted and _kwtemplater.matcher(filename):
   454             if _restricted and _kwtemplater.matcher(filename):
   455                 return _kwtemplater.shrink(data)
   455                 return _kwtemplater.shrink(data)
   456             return data
   456             return data
   457 
   457 
   458         def commit(self, files=None, text='', user=None, date=None,
   458         def commit(self, files=None, text='', user=None, date=None,
   459                    match=util.always, force=False, force_editor=False,
   459                    match=util.always, force=False, force_editor=False,