hgkw/keyword.py
branchsolo-extension
changeset 54 ac67adabce13
parent 53 b0db8ec7ac78
child 55 34fb92c050bc
equal deleted inserted replaced
53:b0db8ec7ac78 54:ac67adabce13
    13 There are many good reasons why this is not needed in a distributed
    13 There are many good reasons why this is not needed in a distributed
    14 SCM, still it may be useful in very small projects based on single
    14 SCM, still it may be useful in very small projects based on single
    15 files (like LaTeX packages), that are mostly addressed to an audience
    15 files (like LaTeX packages), that are mostly addressed to an audience
    16 not running a version control system.
    16 not running a version control system.
    17 
    17 
    18 The extension consists actually in 2 parts:
    18 The extension, according to its hackish nature, is a hybrid and consists
       
    19 actually in 2 parts:
    19 
    20 
    20     1. extension code (reposetup) that is triggered on checkout and
    21     1. pure extension code (reposetup) that is triggered on checkout and
    21        logging of changes.
    22        logging of changes.
    22     2. a pretxncommit hook (hgrc (5)) that expands keywords immediately
    23     2. a pretxncommit hook (hgrc (5)) that expands keywords immediately
    23        at commit time in the working directory.
    24        at commit time in the working directory.
    24 
    25 
    25 Simple setup in hgrc:
    26 Simple setup in hgrc:
    80             self._path = path
    81             self._path = path
    81 
    82 
    82         def read(self, node):
    83         def read(self, node):
    83             data = super(kwfilelog, self).read(node)
    84             data = super(kwfilelog, self).read(node)
    84             if not self._path.startswith('.hg') and not util.binary(data):
    85             if not self._path.startswith('.hg') and not util.binary(data):
    85 
       
    86                 c = context.filectx(self._repo, self._path, fileid=node,
    86                 c = context.filectx(self._repo, self._path, fileid=node,
    87                                      filelog=self)
    87                                      filelog=self)
    88                 for pat, opt in self._repo.ui.configitems('keyword'):
    88                 for pat, opt in self._repo.ui.configitems('keyword'):
    89                     if opt == 'expand':
    89                     if opt == 'expand':
    90                         mf = util.matcher(self._repo.root,
    90                         mf = util.matcher(self._repo.root,
   138 
   138 
   139     files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
   139     files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
   140     # validity checks should have been done already
   140     # validity checks should have been done already
   141     modified, added = repo.status(files=files, match=match)[:2]
   141     modified, added = repo.status(files=files, match=match)[:2]
   142     candidates = [f for f in modified + added if not f.startswith('.hg')]
   142     candidates = [f for f in modified + added if not f.startswith('.hg')]
   143 
       
   144     if not candidates:
   143     if not candidates:
   145         return False
   144         return False
   146 
   145 
   147     # only check files that are configured in keyword section
   146     # only check files that are configured in keyword section
   148     files = []
   147     files = []
   149     # python2.4: files = set()
       
   150     for pat, opt in repo.ui.configitems('keyword'):
   148     for pat, opt in repo.ui.configitems('keyword'):
   151         if opt == 'expand':
   149         if opt == 'expand':
   152             mf = util.matcher(repo.root, '', [pat], [], [])[1]
   150             mf = util.matcher(repo.root, '', [pat], [], [])[1]
   153             for candidate in candidates:
   151             for candidate in candidates:
   154                 if mf(candidate) and candidate not in files:
   152                 if mf(candidate) and candidate not in files:
   155                     files.append(candidate)
   153                     files.append(candidate)
   156                 # python2.4:
       
   157                 # if mf(candidate): files.add(candidate)
       
   158 
       
   159     if not files:
   154     if not files:
   160         return False
   155         return False
   161 
   156 
   162     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
   157     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
   163     # expand both expanded and unexpanded keywords
   158     # update both expanded and unexpanded keywords
   164     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
   159     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
   165 
   160 
   166     for f in files:
   161     for f in files:
   167         data = repo.wfile(f).read()
   162         data = repo.wfile(f).read()
   168         if not util.binary(data):
   163         if not util.binary(data):
   172                         repo, args['node'][:12], f, date, user)
   167                         repo, args['node'][:12], f, date, user)
   173 
   168 
   174             data, kwct = re_kw.subn(kwexpander, data)
   169             data, kwct = re_kw.subn(kwexpander, data)
   175             if kwct:
   170             if kwct:
   176                 ui.note(_('expanding keywords in %s\n' % f))
   171                 ui.note(_('expanding keywords in %s\n' % f))
   177                 # backup file?
       
   178                 repo.wfile(f, 'w').write(data)
   172                 repo.wfile(f, 'w').write(data)