hgkw/keyword.py
changeset 210 304f9ac35869
parent 209 430837dbe7f4
child 211 110c76a94d1c
equal deleted inserted replaced
209:430837dbe7f4 210:304f9ac35869
   304 
   304 
   305 def demo(ui, repo, *args, **opts):
   305 def demo(ui, repo, *args, **opts):
   306     '''print [keywordmaps] configuration and an expansion example
   306     '''print [keywordmaps] configuration and an expansion example
   307 
   307 
   308     show current, custom, or default keyword template maps and their expansion
   308     show current, custom, or default keyword template maps and their expansion
   309     '''
   309 
       
   310     extend current configuration by specifying maps as arguments
       
   311     and optionally by reading from an additional hgrc file
       
   312 
       
   313     override current keyword template maps with "default" option
       
   314     '''
       
   315     def _demostatus(stat):
       
   316         ui.status(_('\n\t%s\n') % stat)
       
   317 
       
   318     def _showitems(section, items):
       
   319         ui.write('[%s]\n' % section)
       
   320         for k, v in items:
       
   321             ui.write('%s = %s\n' % (k, v))
       
   322 
   310     msg = 'hg keyword config and expansion example'
   323     msg = 'hg keyword config and expansion example'
   311     kwstatus = 'current'
   324     kwstatus = 'current'
   312     fn = 'demo.txt'
   325     fn = 'demo.txt'
       
   326     branchname = 'demobranch'
       
   327     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
       
   328     ui.note(_('creating temporary repo at %s\n') % tmpdir)
       
   329     repo = localrepo.localrepository(ui, path=tmpdir, create=True)
       
   330     repo.ui = ui # backwards compatibility
   313     ui.setconfig('keyword', fn, '')
   331     ui.setconfig('keyword', fn, '')
       
   332     if args or opts['rcfile']:
       
   333         kwstatus = 'custom'
       
   334     if opts['rcfile']:
       
   335         ui.readconfig(opts['rcfile'])
   314     if opts['default']:
   336     if opts['default']:
   315         kwstatus = 'default'
   337         kwstatus = 'default'
   316         kwmaps = kwtemplater.deftemplates
   338         kwmaps = kwtemplater.deftemplates
   317         if ui.configitems('keywordmaps'):
   339         if ui.configitems('keywordmaps'):
       
   340             # override maps from optional rcfile
   318             for k, v in kwmaps.items():
   341             for k, v in kwmaps.items():
   319                 ui.setconfig('keywordmaps', k, v)
   342                 ui.setconfig('keywordmaps', k, v)
   320     else:
   343     elif args:
   321         if args or opts['rcfile']:
   344         # simulate hgrc parsing
   322             kwstatus = 'custom'
   345         rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
   323         for tmap in args:
   346         fp = repo.opener('hgrc', 'w')
   324             k, v = tmap.split('=', 1)
   347         fp.writelines(rcmaps)
   325             ui.setconfig('keywordmaps', k.strip(), v.strip())
   348         fp.close()
   326         if opts['rcfile']:
   349         ui.readconfig(repo.join('hgrc'))
   327             ui.readconfig(opts['rcfile'])
   350     if not opts['default']:
   328         kwmaps = (dict(ui.configitems('keywordmaps')) or
   351         kwmaps = (dict(ui.configitems('keywordmaps')) or
   329                   kwtemplater.deftemplates)
   352                   kwtemplater.deftemplates)
       
   353     reposetup(ui, repo)
   330     for k, v in ui.configitems('extensions'):
   354     for k, v in ui.configitems('extensions'):
   331         if k.endswith('keyword'):
   355         if k.endswith('keyword'):
   332             extension = '%s = %s' % (k, v)
   356             extension = '%s = %s' % (k, v)
   333             break
   357             break
   334     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   358     _demostatus('config using %s keyword template maps' % kwstatus)
   335     ui.note(_('creating temporary repo at %s\n') % tmpdir)
   359     ui.write('[extensions]\n%s\n' % extension)
   336     repo = localrepo.localrepository(ui, path=tmpdir, create=True)
   360     _showitems('keyword', ui.configitems('keyword'))
   337     repo.ui = ui # backwards compatibility
   361     _showitems('keywordmaps', kwmaps.items())
   338     reposetup(ui, repo)
       
   339     ui.status(_('config using %s keyword template maps:\n') % kwstatus)
       
   340     ui.write('[extensions]\n%s\n'
       
   341              '[keyword]\n%s =\n'
       
   342              '[keywordmaps]\n' % (extension, fn))
       
   343     for k, v in kwmaps.items():
       
   344         ui.write('%s = %s\n' % (k, v))
       
   345     path = repo.wjoin(fn)
       
   346     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   362     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   347     repo.wopener(fn, 'w').write(keywords)
   363     repo.wopener(fn, 'w').write(keywords)
   348     repo.add([fn])
   364     repo.add([fn])
       
   365     path = repo.wjoin(fn)
   349     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   366     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   350     ui.note(keywords)
   367     ui.note(keywords)
   351     ui.note('\nhg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   368     ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
       
   369     # silence branch command if not verbose
       
   370     quiet = ui.quiet
       
   371     verbose = ui.verbose
       
   372     ui.quiet = not verbose
       
   373     commands.branch(ui, repo, branchname)
       
   374     ui.quiet = quiet
       
   375     ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   352     repo.commit(text=msg)
   376     repo.commit(text=msg)
   353     pathinfo = ('', ' in %s' % path)[ui.verbose]
   377     pathinfo = ('', ' in %s' % path)[ui.verbose]
   354     ui.status(_('\n%s keywords expanded%s:\n') % (kwstatus, pathinfo))
   378     _demostatus('%s keywords expanded%s' % (kwstatus, pathinfo))
   355     ui.write(repo.wread(fn))
   379     ui.write(repo.wread(fn))
   356     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   380     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   357     shutil.rmtree(tmpdir)
   381     shutil.rmtree(tmpdir)
   358 
   382 
   359 
   383