comparison hgext/keyword.py @ 6502:ba8a0338baf7

keyword: collect filename patterns, wrap dispatch._parse in uisetup Store [keyword] config in kwtools. hgcmd defaults to empty string.
author Christian Ebert <blacktrash@gmx.net>
date Wed, 09 Apr 2008 14:05:56 +0200
parents c0476a6df325
children 4572beeacff1
comparison
equal deleted inserted replaced
6501:4f7feeb6d6ee 6502:ba8a0338baf7
100 '''Returns hgdate in cvs-like UTC format.''' 100 '''Returns hgdate in cvs-like UTC format.'''
101 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) 101 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
102 102
103 103
104 # make keyword tools accessible 104 # make keyword tools accessible
105 kwtools = {'templater': None, 'hgcmd': None} 105 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']}
106 106
107 # store originals of monkeypatches 107 # store originals of monkeypatches to be done at end of reposetup
108 # that is, only if needed
108 _patchfile_init = patch.patchfile.__init__ 109 _patchfile_init = patch.patchfile.__init__
109 _patch_diff = patch.diff 110 _patch_diff = patch.diff
110 _dispatch_parse = dispatch._parse
111 _webcommands_changeset = webcommands.changeset 111 _webcommands_changeset = webcommands.changeset
112 _webcommands_filediff = webcommands.filediff 112 _webcommands_filediff = webcommands.filediff
113 113
114 def _kwpatchfile_init(self, ui, fname, missing=False): 114 def _kwpatchfile_init(self, ui, fname, missing=False):
115 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid 115 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
138 def _kwweb_filediff(web, req, tmpl): 138 def _kwweb_filediff(web, req, tmpl):
139 '''Wraps webcommands.filediff turning off keyword expansion.''' 139 '''Wraps webcommands.filediff turning off keyword expansion.'''
140 kwtools['templater'].matcher = util.never 140 kwtools['templater'].matcher = util.never
141 return _webcommands_filediff(web, req, tmpl) 141 return _webcommands_filediff(web, req, tmpl)
142 142
143 def _kwdispatch_parse(ui, args):
144 '''Monkeypatch dispatch._parse to obtain running hg command.'''
145 cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
146 kwtools['hgcmd'] = cmd
147 return cmd, func, args, options, cmdoptions
148
149 # dispatch._parse is run before reposetup, so wrap it here
150 # all other actual monkey patching is done at end of reposetup
151 dispatch._parse = _kwdispatch_parse
152
153 143
154 class kwtemplater(object): 144 class kwtemplater(object):
155 ''' 145 '''
156 Sets up keyword templates, corresponding keyword regex, and 146 Sets up keyword templates, corresponding keyword regex, and
157 provides keyword substitution functions. 147 provides keyword substitution functions.
164 'Source': '{root}/{file},v', 154 'Source': '{root}/{file},v',
165 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}', 155 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
166 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}', 156 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
167 } 157 }
168 158
169 def __init__(self, ui, repo, inc, exc): 159 def __init__(self, ui, repo):
170 self.ui = ui 160 self.ui = ui
171 self.repo = repo 161 self.repo = repo
172 self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1] 162 self.matcher = util.matcher(repo.root,
163 inc=kwtools['inc'], exc=kwtools['exc'])[1]
173 self.restrict = kwtools['hgcmd'] in restricted.split() 164 self.restrict = kwtools['hgcmd'] in restricted.split()
174 165
175 kwmaps = self.ui.configitems('keywordmaps') 166 kwmaps = self.ui.configitems('keywordmaps')
176 if kwmaps: # override default templates 167 if kwmaps: # override default templates
177 kwmaps = [(k, templater.parsestring(v, quoted=False)) 168 kwmaps = [(k, templater.parsestring(v, quoted=False))
368 fp.writelines(rcmaps) 359 fp.writelines(rcmaps)
369 fp.close() 360 fp.close()
370 ui.readconfig(repo.join('hgrc')) 361 ui.readconfig(repo.join('hgrc'))
371 if not opts.get('default'): 362 if not opts.get('default'):
372 kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates 363 kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
364 uisetup(ui)
373 reposetup(ui, repo) 365 reposetup(ui, repo)
374 for k, v in ui.configitems('extensions'): 366 for k, v in ui.configitems('extensions'):
375 if k.endswith('keyword'): 367 if k.endswith('keyword'):
376 extension = '%s = %s' % (k, v) 368 extension = '%s = %s' % (k, v)
377 break 369 break
449 ''' 441 '''
450 # 3rd argument sets expansion to False 442 # 3rd argument sets expansion to False
451 _kwfwrite(ui, repo, False, *pats, **opts) 443 _kwfwrite(ui, repo, False, *pats, **opts)
452 444
453 445
446 def uisetup(ui):
447 '''Collects [keyword] config in kwtools.
448 Monkeypatches dispatch._parse if needed.'''
449
450 for pat, opt in ui.configitems('keyword'):
451 if opt != 'ignore':
452 kwtools['inc'].append(pat)
453 else:
454 kwtools['exc'].append(pat)
455
456 if kwtools['inc']:
457 def kwdispatch_parse(ui, args):
458 '''Monkeypatch dispatch._parse to obtain running hg command.'''
459 cmd, func, args, options, cmdoptions = dispatch_parse(ui, args)
460 kwtools['hgcmd'] = cmd
461 return cmd, func, args, options, cmdoptions
462
463 dispatch_parse = dispatch._parse
464 dispatch._parse = kwdispatch_parse
465
454 def reposetup(ui, repo): 466 def reposetup(ui, repo):
455 '''Sets up repo as kwrepo for keyword substitution. 467 '''Sets up repo as kwrepo for keyword substitution.
456 Overrides file method to return kwfilelog instead of filelog 468 Overrides file method to return kwfilelog instead of filelog
457 if file matches user configuration. 469 if file matches user configuration.
458 Wraps commit to overwrite configured files with updated 470 Wraps commit to overwrite configured files with updated
459 keyword substitutions. 471 keyword substitutions.
460 This is done for local repos only, and only if there are 472 This is done for local repos only, and only if there are
461 files configured at all for keyword substitution.''' 473 files configured at all for keyword substitution.'''
462 474
463 try: 475 try:
464 if (not repo.local() or kwtools['hgcmd'] in nokwcommands.split() 476 if (not repo.local() or not kwtools['inc']
477 or kwtools['hgcmd'] in nokwcommands.split()
465 or '.hg' in util.splitpath(repo.root) 478 or '.hg' in util.splitpath(repo.root)
466 or repo._url.startswith('bundle:')): 479 or repo._url.startswith('bundle:')):
467 return 480 return
468 except AttributeError: 481 except AttributeError:
469 pass 482 pass
470 483
471 inc, exc = [], ['.hg*'] 484 kwtools['templater'] = kwt = kwtemplater(ui, repo)
472 for pat, opt in ui.configitems('keyword'):
473 if opt != 'ignore':
474 inc.append(pat)
475 else:
476 exc.append(pat)
477 if not inc:
478 return
479
480 kwtools['templater'] = kwt = kwtemplater(ui, repo, inc, exc)
481 485
482 class kwrepo(repo.__class__): 486 class kwrepo(repo.__class__):
483 def file(self, f): 487 def file(self, f):
484 if f[0] == '/': 488 if f[0] == '/':
485 f = f[1:] 489 f = f[1:]