hgkw/keyword.py
branch0.9.2compat
changeset 348 63ebc698d06b
parent 343 f5927e5574e6
child 350 83aa851f0d84
child 379 290d023e8306
equal deleted inserted replaced
343:f5927e5574e6 348:63ebc698d06b
   187 fancyopts.fancyopts = _fancyopts
   187 fancyopts.fancyopts = _fancyopts
   188 
   188 
   189 
   189 
   190 commands.optionalrepo += ' kwdemo'
   190 commands.optionalrepo += ' kwdemo'
   191 
   191 
       
   192 # hg commands that trigger expansion only when writing to working dir,
       
   193 # not when reading filelog, and unexpand when reading from working dir
       
   194 restricted = ('diff1', 'record',
       
   195               'qfold', 'qimport', 'qnew', 'qpush', 'qrefresh', 'qrecord')
       
   196 
   192 def utcdate(date):
   197 def utcdate(date):
   193     '''Returns hgdate in cvs-like UTC format.'''
   198     '''Returns hgdate in cvs-like UTC format.'''
   194     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   199     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   195 
       
   196 def _kwrestrict(cmd):
       
   197     '''Returns True if cmd should trigger restricted expansion.
       
   198     Keywords will only expanded when writing to working dir.
       
   199     Crucial for mq as expanded keywords should not make it into patches.'''
       
   200     return cmd in ('diff1', 
       
   201                    'qimport', 'qnew', 'qpush', 'qrefresh', 'record', 'qrecord')
       
   202 
   200 
   203 
   201 
   204 _kwtemplater = None
   202 _kwtemplater = None
   205 
   203 
   206 class kwtemplater(object):
   204 class kwtemplater(object):
   267 
   265 
   268         return subfunc(kwsub, data)
   266         return subfunc(kwsub, data)
   269 
   267 
   270     def expand(self, node, data):
   268     def expand(self, node, data):
   271         '''Returns data with keywords expanded.'''
   269         '''Returns data with keywords expanded.'''
   272         if util.binary(data) or _kwrestrict(self.hgcmd):
   270         if util.binary(data) or self.hgcmd in restricted:
   273             return data
   271             return data
   274         return self.substitute(node, data, self.re_kw.sub)
   272         return self.substitute(node, data, self.re_kw.sub)
   275 
   273 
   276     def process(self, node, data, expand):
   274     def process(self, node, data, expand):
   277         '''Returns a tuple: data, count.
   275         '''Returns a tuple: data, count.
   338     '''Overwrites selected files expanding/shrinking keywords.'''
   336     '''Overwrites selected files expanding/shrinking keywords.'''
   339     ctx = repo.changectx(node)
   337     ctx = repo.changectx(node)
   340     mf = ctx.manifest()
   338     mf = ctx.manifest()
   341     if node is not None:   # commit
   339     if node is not None:   # commit
   342         _kwtemplater.commitnode = node
   340         _kwtemplater.commitnode = node
   343         files = [f for f in ctx.files() if mf.has_key(f)]
   341         files = [f for f in ctx.files() if f in mf]
   344         notify = ui.debug
   342         notify = ui.debug
   345     else:                  # kwexpand/kwshrink
   343     else:                  # kwexpand/kwshrink
   346         notify = ui.note
   344         notify = ui.note
   347     candidates = [f for f in files if _iskwfile(f, mf.linkf)]
   345     candidates = [f for f in files if _iskwfile(f, mf.linkf)]
   348     if candidates:
   346     if candidates:
   407     if opts.get('default'):
   405     if opts.get('default'):
   408         kwstatus = 'default'
   406         kwstatus = 'default'
   409         kwmaps = kwtemplater.templates
   407         kwmaps = kwtemplater.templates
   410         if ui.configitems('keywordmaps'):
   408         if ui.configitems('keywordmaps'):
   411             # override maps from optional rcfile
   409             # override maps from optional rcfile
   412             for k, v in kwmaps.items():
   410             for k, v in kwmaps.iteritems():
   413                 ui.setconfig('keywordmaps', k, v)
   411                 ui.setconfig('keywordmaps', k, v)
   414     elif args:
   412     elif args:
   415         # simulate hgrc parsing
   413         # simulate hgrc parsing
   416         rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
   414         rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
   417         fp = repo.opener('hgrc', 'w')
   415         fp = repo.opener('hgrc', 'w')
   426             extension = '%s = %s' % (k, v)
   424             extension = '%s = %s' % (k, v)
   427             break
   425             break
   428     demostatus('config using %s keyword template maps' % kwstatus)
   426     demostatus('config using %s keyword template maps' % kwstatus)
   429     ui.write('[extensions]\n%s\n' % extension)
   427     ui.write('[extensions]\n%s\n' % extension)
   430     demoitems('keyword', ui.configitems('keyword'))
   428     demoitems('keyword', ui.configitems('keyword'))
   431     demoitems('keywordmaps', kwmaps.items())
   429     demoitems('keywordmaps', kwmaps.iteritems())
   432     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   430     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   433     repo.wopener(fn, 'w').write(keywords)
   431     repo.wopener(fn, 'w').write(keywords)
   434     repo.add([fn])
   432     repo.add([fn])
   435     path = repo.wjoin(fn)
   433     path = repo.wjoin(fn)
   436     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   434     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   550                 return kwfilelog(self.sopener, f)
   548                 return kwfilelog(self.sopener, f)
   551             return filelog.filelog(self.sopener, f)
   549             return filelog.filelog(self.sopener, f)
   552 
   550 
   553         def wread(self, filename):
   551         def wread(self, filename):
   554             data = super(kwrepo, self).wread(filename)
   552             data = super(kwrepo, self).wread(filename)
   555             if _kwrestrict(hgcmd) and _kwtemplater.matcher(filename):
   553             if hgcmd in restricted and _kwtemplater.matcher(filename):
   556                 return _kwtemplater.shrink(data)
   554                 return _kwtemplater.shrink(data)
   557             return data
   555             return data
   558 
   556 
   559         def _commit(self, files, text, user, date, match, force, lock, wlock,
   557         def _commit(self, files, text, user, date, match, force, lock, wlock,
   560                     force_editor, p1, p2, extra):
   558                     force_editor, p1, p2, extra):
   587                 if not _wlock:
   585                 if not _wlock:
   588                     _wlock = self.wlock()
   586                     _wlock = self.wlock()
   589                 if not _lock:
   587                 if not _lock:
   590                     _lock = self.lock()
   588                     _lock = self.lock()
   591                 # store and postpone commit hooks
   589                 # store and postpone commit hooks
   592                 commithooks = []
   590                 commithooks = {}
   593                 for name, cmd in ui.configitems('hooks'):
   591                 for name, cmd in ui.configitems('hooks'):
   594                     if name.split('.', 1)[0] == 'commit':
   592                     if name.split('.', 1)[0] == 'commit':
   595                         commithooks.append((name, cmd))
   593                         commithooks[name] = cmd
   596                         ui.setconfig('hooks', name, '')
   594                         ui.setconfig('hooks', name, '')
   597                 if commithooks:
   595                 if commithooks:
   598                     # store parents for commit hook environment
   596                     # store parents for commit hook environment
   599                     if p1 is None:
   597                     if p1 is None:
   600                         _p1, _p2 = repo.dirstate.parents()
   598                         _p1, _p2 = repo.dirstate.parents()
   608 
   606 
   609                 node = self._commit(files, text, user, date, match, force,
   607                 node = self._commit(files, text, user, date, match, force,
   610                                     _lock, _wlock, force_editor, p1, p2, extra)
   608                                     _lock, _wlock, force_editor, p1, p2, extra)
   611 
   609 
   612                 # restore commit hooks
   610                 # restore commit hooks
   613                 for name, cmd in commithooks:
   611                 for name, cmd in commithooks.iteritems():
   614                     ui.setconfig('hooks', name, cmd)
   612                     ui.setconfig('hooks', name, cmd)
   615                 if node is not None:
   613                 if node is not None:
   616                     _overwrite(ui, self, node=node)
   614                     _overwrite(ui, self, node=node)
   617                     repo.hook('commit', node=node, parent1=_p1, parent2=_p2)
   615                     repo.hook('commit', node=node, parent1=_p1, parent2=_p2)
   618                 return node
   616                 return node