hgext/keyword.py
branchstable
changeset 40404 956ec6f1320d
parent 40220 c7ffc53fbd19
child 42620 d98ec36be808
equal deleted inserted replaced
38806:535fc8a22365 40404:956ec6f1320d
   206     return templates
   206     return templates
   207 
   207 
   208 def _shrinktext(text, subfunc):
   208 def _shrinktext(text, subfunc):
   209     '''Helper for keyword expansion removal in text.
   209     '''Helper for keyword expansion removal in text.
   210     Depending on subfunc also returns number of substitutions.'''
   210     Depending on subfunc also returns number of substitutions.'''
   211     return subfunc(r'$\1$', text)
   211     return subfunc(br'$\1$', text)
   212 
   212 
   213 def _preselect(wstatus, changed):
   213 def _preselect(wstatus, changed):
   214     '''Retrieves modified and added files from a working directory state
   214     '''Retrieves modified and added files from a working directory state
   215     and returns the subset of each contained in given changed files
   215     and returns the subset of each contained in given changed files
   216     retrieved from a change context.'''
   216     retrieved from a change context.'''
   248         return '|'.join(map(stringutil.reescape, self.templates.keys()))
   248         return '|'.join(map(stringutil.reescape, self.templates.keys()))
   249 
   249 
   250     @util.propertycache
   250     @util.propertycache
   251     def rekw(self):
   251     def rekw(self):
   252         '''Returns regex for unexpanded keywords.'''
   252         '''Returns regex for unexpanded keywords.'''
   253         return re.compile(r'\$(%s)\$' % self.escape)
   253         return re.compile(br'\$(%s)\$' % self.escape)
   254 
   254 
   255     @util.propertycache
   255     @util.propertycache
   256     def rekwexp(self):
   256     def rekwexp(self):
   257         '''Returns regex for expanded keywords.'''
   257         '''Returns regex for expanded keywords.'''
   258         return re.compile(r'\$(%s): [^$\n\r]*? \$' % self.escape)
   258         return re.compile(br'\$(%s): [^$\n\r]*? \$' % self.escape)
   259 
   259 
   260     def substitute(self, data, path, ctx, subfunc):
   260     def substitute(self, data, path, ctx, subfunc):
   261         '''Replaces keywords in data with expanded template.'''
   261         '''Replaces keywords in data with expanded template.'''
   262         def kwsub(mobj):
   262         def kwsub(mobj):
   263             kw = mobj.group(1)
   263             kw = mobj.group(1)
   428     See :hg:`help templates` for information on templates and filters.
   428     See :hg:`help templates` for information on templates and filters.
   429     '''
   429     '''
   430     def demoitems(section, items):
   430     def demoitems(section, items):
   431         ui.write('[%s]\n' % section)
   431         ui.write('[%s]\n' % section)
   432         for k, v in sorted(items):
   432         for k, v in sorted(items):
       
   433             if isinstance(v, bool):
       
   434                 v = stringutil.pprint(v)
   433             ui.write('%s = %s\n' % (k, v))
   435             ui.write('%s = %s\n' % (k, v))
   434 
   436 
   435     fn = 'demo.txt'
   437     fn = 'demo.txt'
   436     tmpdir = pycompat.mkdtemp('', 'kwdemo.')
   438     tmpdir = pycompat.mkdtemp('', 'kwdemo.')
   437     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   439     ui.note(_('creating temporary repository at %s\n') % tmpdir)
   438     if repo is None:
   440     if repo is None:
   439         baseui = ui
   441         baseui = ui
   440     else:
   442     else:
   441         baseui = repo.baseui
   443         baseui = repo.baseui
   442     repo = localrepo.localrepository(baseui, tmpdir, True)
   444     repo = localrepo.instance(baseui, tmpdir, create=True)
   443     ui.setconfig('keyword', fn, '', 'keyword')
   445     ui.setconfig('keyword', fn, '', 'keyword')
   444     svn = ui.configbool('keywordset', 'svn')
   446     svn = ui.configbool('keywordset', 'svn')
   445     # explicitly set keywordset for demo output
   447     # explicitly set keywordset for demo output
   446     ui.setconfig('keywordset', 'svn', svn, 'keyword')
   448     ui.setconfig('keywordset', 'svn', svn, 'keyword')
   447 
   449 
   565         showfiles = [], [], []
   567         showfiles = [], [], []
   566     if opts.get('all') or opts.get('ignore'):
   568     if opts.get('all') or opts.get('ignore'):
   567         showfiles += ([f for f in files if f not in kwfiles],
   569         showfiles += ([f for f in files if f not in kwfiles],
   568                       [f for f in status.unknown if f not in kwunknown])
   570                       [f for f in status.unknown if f not in kwunknown])
   569     kwlabels = 'enabled deleted enabledunknown ignored ignoredunknown'.split()
   571     kwlabels = 'enabled deleted enabledunknown ignored ignoredunknown'.split()
   570     kwstates = zip(kwlabels, 'K!kIi', showfiles)
   572     kwstates = zip(kwlabels, pycompat.bytestr('K!kIi'), showfiles)
   571     fm = ui.formatter('kwfiles', opts)
   573     fm = ui.formatter('kwfiles', opts)
   572     fmt = '%.0s%s\n'
   574     fmt = '%.0s%s\n'
   573     if opts.get('all') or ui.verbose:
   575     if opts.get('all') or ui.verbose:
   574         fmt = '%s %s\n'
   576         fmt = '%s %s\n'
   575     for kwstate, char, filenames in kwstates:
   577     for kwstate, char, filenames in kwstates:
   576         label = 'kwfiles.' + kwstate
   578         label = 'kwfiles.' + kwstate
   577         for f in filenames:
   579         for f in filenames:
   578             fm.startitem()
   580             fm.startitem()
   579             fm.write('kwstatus path', fmt, char,
   581             fm.data(kwstatus=char, path=f)
   580                      repo.pathto(f, cwd), label=label)
   582             fm.plain(fmt % (char, repo.pathto(f, cwd)), label=label)
   581     fm.end()
   583     fm.end()
   582 
   584 
   583 @command('kwshrink',
   585 @command('kwshrink',
   584     cmdutil.walkopts,
   586     cmdutil.walkopts,
   585     _('hg kwshrink [OPTION]... [FILE]...'),
   587     _('hg kwshrink [OPTION]... [FILE]...'),