hgkw/keyword.py
changeset 205 f2d2e36053f9
parent 204 2d089b691b31
child 206 8d16b70359da
equal deleted inserted replaced
204:2d089b691b31 205:f2d2e36053f9
   297     show current, custom, or default keyword template maps and their expansion
   297     show current, custom, or default keyword template maps and their expansion
   298     '''
   298     '''
   299     msg = 'hg keyword config and expansion example'
   299     msg = 'hg keyword config and expansion example'
   300     kwstatus = 'current'
   300     kwstatus = 'current'
   301     fn = 'demo.txt'
   301     fn = 'demo.txt'
   302     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
       
   303     ui.note(_('creating temporary repo at %s\n') % tmpdir)
       
   304     _repo = localrepo.localrepository(ui, path=tmpdir, create=True)
       
   305     # for backwards compatibility
       
   306     ui = _repo.ui
       
   307     ui.setconfig('keyword', fn, '')
   302     ui.setconfig('keyword', fn, '')
   308     if opts['default']:
   303     if opts['default']:
   309         kwstatus = 'default'
   304         kwstatus = 'default'
   310         kwmaps = kwtemplater.deftemplates
   305         kwmaps = kwtemplater.deftemplates
       
   306         if ui.configitems('keywordmaps'):
       
   307             for k, v in kwmaps.items():
       
   308                 ui.setconfig('keywordmaps', k, v)
   311     else:
   309     else:
   312         if args or opts['rcfile']:
   310         if args or opts['rcfile']:
   313             kwstatus = 'custom'
   311             kwstatus = 'custom'
   314         for tmap in args:
   312         for tmap in args:
   315             k, v = tmap.split('=', 1)
   313             k, v = tmap.split('=', 1)
   316             ui.setconfig('keywordmaps', k.strip(), v.strip())
   314             ui.setconfig('keywordmaps', k.strip(), v.strip())
   317         if opts['rcfile']:
   315         if opts['rcfile']:
   318             ui.readconfig(opts['rcfile'])
   316             ui.readconfig(opts['rcfile'])
   319         kwmaps = (dict(ui.configitems('keywordmaps')) or
   317         kwmaps = (dict(ui.configitems('keywordmaps')) or
   320                   kwtemplater.deftemplates)
   318                   kwtemplater.deftemplates)
   321     if ui.configitems('keywordmaps'):
   319     tmpdir = tempfile.mkdtemp('', 'kwdemo.')
   322         for k, v in kwmaps.items():
   320     ui.note(_('creating temporary repo at %s\n') % tmpdir)
   323             ui.setconfig('keywordmaps', k, v)
   321     repo = localrepo.localrepository(ui, path=tmpdir, create=True)
   324     reposetup(ui, _repo)
   322     repo.ui = ui # backwards compatibility
       
   323     reposetup(ui, repo)
   325     ui.status(_('config with %s keyword template maps:\n') % kwstatus)
   324     ui.status(_('config with %s keyword template maps:\n') % kwstatus)
   326     ui.write('[keyword]\n%s =\n[keywordmaps]\n' % fn)
   325     ui.write('[keyword]\n%s =\n[keywordmaps]\n' % fn)
   327     for k, v in kwmaps.items():
   326     for k, v in kwmaps.items():
   328         ui.write('%s = %s\n' % (k, v))
   327         ui.write('%s = %s\n' % (k, v))
   329     path = _repo.wjoin(fn)
   328     path = repo.wjoin(fn)
   330     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   329     keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
   331     _repo.wopener(fn, 'w').write(keywords)
   330     repo.wopener(fn, 'w').write(keywords)
   332     _repo.add([fn])
   331     repo.add([fn])
   333     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   332     ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
   334     ui.note(keywords)
   333     ui.note(keywords)
   335     ui.note(_("\nhg --repository '%s' commit\n") % tmpdir)
   334     ui.note('\nhg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
   336     _repo.commit(text=msg)
   335     repo.commit(text=msg)
   337     pathinfo = ('', ' in %s' % path)[ui.verbose]
   336     pathinfo = ('', ' in %s' % path)[ui.verbose]
   338     ui.status(_('\n%s keywords expanded%s:\n') % (kwstatus, pathinfo))
   337     ui.status(_('\n%s keywords expanded%s:\n') % (kwstatus, pathinfo))
   339     ui.write(_repo.wread(fn))
   338     ui.write(repo.wread(fn))
   340     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   339     ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
   341     shutil.rmtree(tmpdir)
   340     shutil.rmtree(tmpdir)
   342 
   341 
   343 
   342 
   344 def reposetup(ui, repo):
   343 def reposetup(ui, repo):