hgkw/keyword.py
changeset 633 ca4b437a817c
parent 631 ec683ca3ba28
child 635 49307521cf96
equal deleted inserted replaced
631:ec683ca3ba28 633:ca4b437a817c
   274     '''print [keywordmaps] configuration and an expansion example
   274     '''print [keywordmaps] configuration and an expansion example
   275 
   275 
   276     Show current, custom, or default keyword template maps and their
   276     Show current, custom, or default keyword template maps and their
   277     expansions.
   277     expansions.
   278 
   278 
   279     Extend current configuration by specifying maps as arguments and
   279     Extend the current configuration by specifying maps as arguments
   280     optionally by reading from an additional hgrc file.
   280     and using -f/--rcfile to source an external hgrc file.
   281 
   281 
   282     Override current keyword template maps with "default" option.
   282     Use -d/--default to disable current configuration.
   283     '''
   283     '''
   284     def demoitems(section, items):
   284     def demoitems(section, items):
   285         ui.write('[%s]\n' % section)
   285         ui.write('[%s]\n' % section)
   286         for k, v in items:
   286         for k, v in items:
   287             ui.write('%s = %s\n' % (k, v))
   287             ui.write('%s = %s\n' % (k, v))
   288 
   288 
   289     msg = 'hg keyword config and expansion example'
   289     msg = 'hg keyword config and expansion example'
   290     kwstatus = 'current'
       
   291     fn = 'demo.txt'
   290     fn = 'demo.txt'
   292     branchname = 'demobranch'
   291     branchname = 'demobranch'
   293     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   292     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   294     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   293     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   295     repo = localrepo.localrepository(ui, tmpdir, True)
   294     repo = localrepo.localrepository(ui, tmpdir, True)
   296     ui.setconfig('keyword', fn, '')
   295     ui.setconfig('keyword', fn, '')
       
   296 
       
   297     uikwmaps = ui.configitems('keywordmaps')
   297     if args or opts.get('rcfile'):
   298     if args or opts.get('rcfile'):
   298         kwstatus = 'custom'
   299         ui.status(_('\n\tconfiguration using custom keyword template maps\n'))
   299     if opts.get('rcfile'):
   300         if uikwmaps:
   300         ui.readconfig(opts.get('rcfile'))
   301             ui.status(_('\textending current template maps\n'))
   301     if opts.get('default'):
   302         if opts.get('default') or not uikwmaps:
   302         kwstatus = 'default'
   303             ui.status(_('\toverriding default template maps\n'))
       
   304         if opts.get('rcfile'):
       
   305             ui.readconfig(opts.get('rcfile'))
       
   306         if args:
       
   307             # simulate hgrc parsing
       
   308             rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
       
   309             fp = repo.opener('hgrc', 'w')
       
   310             fp.writelines(rcmaps)
       
   311             fp.close()
       
   312             ui.readconfig(repo.join('hgrc'))
       
   313         kwmaps = dict(ui.configitems('keywordmaps'))
       
   314     elif opts.get('default'):
       
   315         ui.status(_('\n\tconfiguration using default keyword template maps\n'))
   303         kwmaps = kwtemplater.templates
   316         kwmaps = kwtemplater.templates
   304         if ui.configitems('keywordmaps'):
   317         if uikwmaps:
   305             # override maps from optional rcfile
   318             ui.status(_('\tdisabling current template maps\n'))
   306             for k, v in kwmaps.iteritems():
   319             for k, v in kwmaps.iteritems():
   307                 ui.setconfig('keywordmaps', k, v)
   320                 ui.setconfig('keywordmaps', k, v)
   308     elif args:
   321     else:
   309         # simulate hgrc parsing
   322         ui.status(_('\n\tconfiguration using current keyword template maps\n'))
   310         rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
   323         kwmaps = dict(uikwmaps) or kwtemplater.templates
   311         fp = repo.opener('hgrc', 'w')
   324 
   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)
   325     uisetup(ui)
   318     reposetup(ui, repo)
   326     reposetup(ui, repo)
   319     for k, v in ui.configitems('extensions'):
   327     for k, v in ui.configitems('extensions'):
   320         if k.endswith('keyword'):
   328         if k.endswith('keyword'):
   321             extension = '%s = %s' % (k, v)
   329             extension = '%s = %s' % (k, v)
   322             break
   330             break
   323     ui.status(_('\n\tconfig using %s keyword template maps\n') % kwstatus)
       
   324     ui.write('[extensions]\n%s\n' % extension)
   331     ui.write('[extensions]\n%s\n' % extension)
   325     demoitems('keyword', ui.configitems('keyword'))
   332     demoitems('keyword', ui.configitems('keyword'))
   326     demoitems('keywordmaps', kwmaps.iteritems())
   333     demoitems('keywordmaps', kwmaps.iteritems())
   327     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   334     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   328     repo.wopener(fn, 'w').write(keywords)
   335     repo.wopener(fn, 'w').write(keywords)
   329     repo.add([fn])
   336     repo.add([fn])
   330     path = repo.wjoin(fn)
   337     path = repo.wjoin(fn)
   331     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   338     ui.note(_('\nkeywords written to %s:\n') % path)
   332     ui.note(keywords)
   339     ui.note(keywords)
   333     ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
   340     ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
   334     # silence branch command if not verbose
   341     # silence branch command if not verbose
   335     quiet = ui.quiet
   342     quiet = ui.quiet
   336     ui.quiet = not ui.verbose
   343     ui.quiet = not ui.verbose
   340         if name.split('.', 1)[0].find('commit') > -1:
   347         if name.split('.', 1)[0].find('commit') > -1:
   341             repo.ui.setconfig('hooks', name, '')
   348             repo.ui.setconfig('hooks', name, '')
   342     ui.note(_('unhooked all commit hooks\n'))
   349     ui.note(_('unhooked all commit hooks\n'))
   343     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   350     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   344     repo.commit(text=msg)
   351     repo.commit(text=msg)
   345     fmt = ui.verbose and ' in %s' % path or ''
   352     ui.status(_('\n\tkeywords expanded\n'))
   346     ui.status(_('\n\t%s keywords expanded%s\n') % (kwstatus, fmt))
       
   347     ui.write(repo.wread(fn))
   353     ui.write(repo.wread(fn))
   348     ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
   354     ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
   349     shutil.rmtree(tmpdir, ignore_errors=True)
   355     shutil.rmtree(tmpdir, ignore_errors=True)
   350 
   356 
   351 def expand(ui, repo, *pats, **opts):
   357 def expand(ui, repo, *pats, **opts):