hgkw/keyword.py
branchkwmap-templates
changeset 122 d5c80df59655
parent 120 4a8a861da58c
child 123 6cf933de685a
equal deleted inserted replaced
121:85ed7d71ba0f 122:d5c80df59655
   106             return k
   106             return k
   107     else:
   107     else:
   108         sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
   108         sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
   109         return os.path.splitext(os.path.basename(__file__))[0]
   109         return os.path.splitext(os.path.basename(__file__))[0]
   110 
   110 
   111 def kwfmatches(ui, repo, files):
       
   112     '''Selects and weeds out candidates for keyword substitution
       
   113     by patterns configured in [keyword] section in hgrc.'''
       
   114     inc, exc = [], ['.hg*']
       
   115     for pat, opt in ui.configitems('keyword'):
       
   116         if opt != 'ignore':
       
   117             inc.append(pat)
       
   118         else:
       
   119             exc.append(pat)
       
   120     if not inc:
       
   121         return []
       
   122     kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
       
   123     return [f for f in files if kwfmatcher(f)]
       
   124 
       
   125 class kwtemplater(object):
   111 class kwtemplater(object):
   126     '''
   112     '''
   127     Sets up keyword templates, corresponding keyword regex, and
   113     Sets up keyword templates, corresponding keyword regex, and
   128     provides keyword expansion function.
   114     provides keyword expansion function.
   129     '''
   115     '''
   153     for keyword substitution. This is done for local repos only.'''
   139     for keyword substitution. This is done for local repos only.'''
   154 
   140 
   155     if not repo.local():
   141     if not repo.local():
   156         return
   142         return
   157 
   143 
       
   144     inc, exc = [], ['.hg*']
       
   145     for pat, opt in ui.configitems('keyword'):
       
   146         if opt != 'ignore':
       
   147             inc.append(pat)
       
   148         else:
       
   149             exc.append(pat)
       
   150     if not inc:
       
   151         return
       
   152 
       
   153     ui.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
       
   154 
   158     class kwrepo(repo.__class__):
   155     class kwrepo(repo.__class__):
   159         def file(self, f):
   156         def file(self, f):
   160             if f[0] == '/':
   157             if f[0] == '/':
   161                 f = f[1:]
   158                 f = f[1:]
   162             return filelog.filelog(self.sopener, f, self, self.revlogversion)
   159             return filelog.filelog(self.sopener, f, self, self.revlogversion)
   170                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   167                      defversion=revlog.REVLOG_DEFAULT_VERSION):
   171             super(kwfilelog, self).__init__(opener, path, defversion)
   168             super(kwfilelog, self).__init__(opener, path, defversion)
   172             self._repo = repo
   169             self._repo = repo
   173             self._path = path
   170             self._path = path
   174             # only init kwtemplater if needed
   171             # only init kwtemplater if needed
   175             if not isinstance(repo, int) and kwfmatches(ui, repo, [path]):
   172             if not isinstance(repo, int) and ui.kwfmatcher(path):
   176                 self.kwt = kwtemplater(ui, repo)
   173                 self.kwt = kwtemplater(ui, repo)
   177             else:
   174             else:
   178                 self.kwt = None
   175                 self.kwt = None
   179 
   176 
   180         def iskwcandidate(self, data):
   177         def iskwcandidate(self, data):
   219     if repr(cmd).split()[1] in ('tag', 'import_'):
   216     if repr(cmd).split()[1] in ('tag', 'import_'):
   220         return
   217         return
   221 
   218 
   222     files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
   219     files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
   223     modified, added = repo.status(files=files, match=match)[:2]
   220     modified, added = repo.status(files=files, match=match)[:2]
   224     candidates = kwfmatches(ui, repo, modified+added)
   221     candidates = [f for f in modified + added if ui.kwfmatcher(f)]
   225     if not candidates:
   222     if not candidates:
   226         return
   223         return
   227 
   224 
   228     kwt = kwtemplater(ui, repo)
   225     kwt = kwtemplater(ui, repo)
   229     node = bin(args['node'])
   226     node = bin(args['node'])