hgkw/keyword.py
branchstable
changeset 637 103120cace93
parent 636 d0c35f5fa25f
child 646 c2b1a05b35e1
equal deleted inserted replaced
632:1357d4a4ad3e 637:103120cace93
    47 
    47 
    48 NOTE: the more specific you are in your filename patterns the less you
    48 NOTE: the more specific you are in your filename patterns the less you
    49 lose speed in huge repositories.
    49 lose speed in huge repositories.
    50 
    50 
    51 For [keywordmaps] template mapping and expansion demonstration and
    51 For [keywordmaps] template mapping and expansion demonstration and
    52 control run "hg kwdemo".
    52 control run "hg kwdemo". See "hg help templates" for a list of
    53 
    53 available templates and filters.
    54 An additional date template filter {date|utcdate} is provided.
    54 
       
    55 An additional date template filter {date|utcdate} is provided. It
       
    56 returns a date like "2006/09/18 15:13:13".
    55 
    57 
    56 The default template mappings (view with "hg kwdemo -d") can be
    58 The default template mappings (view with "hg kwdemo -d") can be
    57 replaced with customized keywords and templates. Again, run "hg
    59 replaced with customized keywords and templates. Again, run "hg
    58 kwdemo" to control the results of your config changes.
    60 kwdemo" to control the results of your config changes.
    59 
    61 
    78 from mercurial import patch, localrepo, templater, templatefilters, util, match
    80 from mercurial import patch, localrepo, templater, templatefilters, util, match
    79 from mercurial.hgweb import webcommands
    81 from mercurial.hgweb import webcommands
    80 from mercurial.lock import release
    82 from mercurial.lock import release
    81 from mercurial.node import nullid
    83 from mercurial.node import nullid
    82 from mercurial.i18n import _
    84 from mercurial.i18n import _
    83 import re, shutil, tempfile, time
    85 import re, shutil, tempfile
    84 
    86 
    85 commands.optionalrepo += ' kwdemo'
    87 commands.optionalrepo += ' kwdemo'
    86 
    88 
    87 # hg commands that do not act on keywords
    89 # hg commands that do not act on keywords
    88 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
    90 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
    91 
    93 
    92 # hg commands that trigger expansion only when writing to working dir,
    94 # hg commands that trigger expansion only when writing to working dir,
    93 # not when reading filelog, and unexpand when reading from working dir
    95 # not when reading filelog, and unexpand when reading from working dir
    94 restricted = 'merge record resolve qfold qimport qnew qpush qrefresh qrecord'
    96 restricted = 'merge record resolve qfold qimport qnew qpush qrefresh qrecord'
    95 
    97 
    96 def utcdate(date):
    98 # provide cvs-like UTC date filter
    97     '''Returns hgdate in cvs-like UTC format.'''
    99 utcdate = lambda x: util.datestr(x, '%Y/%m/%d %H:%M:%S')
    98     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
       
    99 
   100 
   100 # make keyword tools accessible
   101 # make keyword tools accessible
   101 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']}
   102 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']}
   102 
   103 
   103 
   104 
   274     '''print [keywordmaps] configuration and an expansion example
   275     '''print [keywordmaps] configuration and an expansion example
   275 
   276 
   276     Show current, custom, or default keyword template maps and their
   277     Show current, custom, or default keyword template maps and their
   277     expansions.
   278     expansions.
   278 
   279 
   279     Extend current configuration by specifying maps as arguments and
   280     Extend the current configuration by specifying maps as arguments
   280     optionally by reading from an additional hgrc file.
   281     and using -f/--rcfile to source an external hgrc file.
   281 
   282 
   282     Override current keyword template maps with "default" option.
   283     Use -d/--default to disable current configuration.
       
   284 
       
   285     See "hg help templates" for information on templates and filters.
   283     '''
   286     '''
   284     def demoitems(section, items):
   287     def demoitems(section, items):
   285         ui.write('[%s]\n' % section)
   288         ui.write('[%s]\n' % section)
   286         for k, v in items:
   289         for k, v in items:
   287             ui.write('%s = %s\n' % (k, v))
   290             ui.write('%s = %s\n' % (k, v))
   288 
   291 
   289     msg = 'hg keyword config and expansion example'
   292     msg = 'hg keyword config and expansion example'
   290     kwstatus = 'current'
       
   291     fn = 'demo.txt'
   293     fn = 'demo.txt'
   292     branchname = 'demobranch'
   294     branchname = 'demobranch'
   293     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   295     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   294     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   296     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   295     repo = localrepo.localrepository(ui, tmpdir, True)
   297     repo = localrepo.localrepository(ui, tmpdir, True)
   296     ui.setconfig('keyword', fn, '')
   298     ui.setconfig('keyword', fn, '')
       
   299 
       
   300     uikwmaps = ui.configitems('keywordmaps')
   297     if args or opts.get('rcfile'):
   301     if args or opts.get('rcfile'):
   298         kwstatus = 'custom'
   302         ui.status(_('\n\tconfiguration using custom keyword template maps\n'))
   299     if opts.get('rcfile'):
   303         if uikwmaps:
   300         ui.readconfig(opts.get('rcfile'))
   304             ui.status(_('\textending current template maps\n'))
   301     if opts.get('default'):
   305         if opts.get('default') or not uikwmaps:
   302         kwstatus = 'default'
   306             ui.status(_('\toverriding default template maps\n'))
       
   307         if opts.get('rcfile'):
       
   308             ui.readconfig(opts.get('rcfile'))
       
   309         if args:
       
   310             # simulate hgrc parsing
       
   311             rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
       
   312             fp = repo.opener('hgrc', 'w')
       
   313             fp.writelines(rcmaps)
       
   314             fp.close()
       
   315             ui.readconfig(repo.join('hgrc'))
       
   316         kwmaps = dict(ui.configitems('keywordmaps'))
       
   317     elif opts.get('default'):
       
   318         ui.status(_('\n\tconfiguration using default keyword template maps\n'))
   303         kwmaps = kwtemplater.templates
   319         kwmaps = kwtemplater.templates
   304         if ui.configitems('keywordmaps'):
   320         if uikwmaps:
   305             # override maps from optional rcfile
   321             ui.status(_('\tdisabling current template maps\n'))
   306             for k, v in kwmaps.iteritems():
   322             for k, v in kwmaps.iteritems():
   307                 ui.setconfig('keywordmaps', k, v)
   323                 ui.setconfig('keywordmaps', k, v)
   308     elif args:
   324     else:
   309         # simulate hgrc parsing
   325         ui.status(_('\n\tconfiguration using current keyword template maps\n'))
   310         rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
   326         kwmaps = dict(uikwmaps) or kwtemplater.templates
   311         fp = repo.opener('hgrc', 'w')
   327 
   312         fp.writelines(rcmaps)
       
   313         fp.close()
       
   314         ui.readconfig(repo.join('hgrc'))
       
   315     if not opts.get('default'):
       
   316         kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
       
   317     uisetup(ui)
   328     uisetup(ui)
   318     reposetup(ui, repo)
   329     reposetup(ui, repo)
   319     for k, v in ui.configitems('extensions'):
   330     for k, v in ui.configitems('extensions'):
   320         if k.endswith('keyword'):
   331         if k.endswith('keyword'):
   321             extension = '%s = %s' % (k, v)
   332             extension = '%s = %s' % (k, v)
   322             break
   333             break
   323     ui.status(_('\n\tconfig using %s keyword template maps\n') % kwstatus)
       
   324     ui.write('[extensions]\n%s\n' % extension)
   334     ui.write('[extensions]\n%s\n' % extension)
   325     demoitems('keyword', ui.configitems('keyword'))
   335     demoitems('keyword', ui.configitems('keyword'))
   326     demoitems('keywordmaps', kwmaps.iteritems())
   336     demoitems('keywordmaps', kwmaps.iteritems())
   327     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   337     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   328     repo.wopener(fn, 'w').write(keywords)
   338     repo.wopener(fn, 'w').write(keywords)
   329     repo.add([fn])
   339     repo.add([fn])
   330     path = repo.wjoin(fn)
   340     path = repo.wjoin(fn)
   331     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   341     ui.note(_('\nkeywords written to %s:\n') % path)
   332     ui.note(keywords)
   342     ui.note(keywords)
   333     ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
   343     ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
   334     # silence branch command if not verbose
   344     # silence branch command if not verbose
   335     quiet = ui.quiet
   345     quiet = ui.quiet
   336     ui.quiet = not ui.verbose
   346     ui.quiet = not ui.verbose
   340         if name.split('.', 1)[0].find('commit') > -1:
   350         if name.split('.', 1)[0].find('commit') > -1:
   341             repo.ui.setconfig('hooks', name, '')
   351             repo.ui.setconfig('hooks', name, '')
   342     ui.note(_('unhooked all commit hooks\n'))
   352     ui.note(_('unhooked all commit hooks\n'))
   343     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   353     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   344     repo.commit(text=msg)
   354     repo.commit(text=msg)
   345     fmt = ui.verbose and ' in %s' % path or ''
   355     ui.status(_('\n\tkeywords expanded\n'))
   346     ui.status(_('\n\t%s keywords expanded%s\n') % (kwstatus, fmt))
       
   347     ui.write(repo.wread(fn))
   356     ui.write(repo.wread(fn))
   348     ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
   357     ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
   349     shutil.rmtree(tmpdir, ignore_errors=True)
   358     shutil.rmtree(tmpdir, ignore_errors=True)
   350 
   359 
   351 def expand(ui, repo, *pats, **opts):
   360 def expand(ui, repo, *pats, **opts):