hgkw/pretxnkw.py
branchextension
changeset 37 3dc31476c148
parent 32 b70b38b15fa4
child 39 14038784f986
equal deleted inserted replaced
36:b3ace8cc5a33 37:3dc31476c148
     1 # $Hg$
     1 from mercurial.i18n import _
     2 
     2 from mercurial import cmdutil, commands, util
     3 import kwexpander
     3 import os.path, re, sys
     4 from mercurial import cmdutil, commands
       
     5 import sys
       
     6 
     4 
     7 def pretxnkw(ui, repo, hooktype, **args):
     5 def pretxnkw(ui, repo, hooktype, **args):
     8     '''Collects candidates for keyword expansion on commit
     6     '''Collects candidates for keyword expansion on commit
     9     and passes them to kwexpander.'''
     7     and passes them to kwexpander.'''
    10 
     8 
    18 
    16 
    19     # validity checks should have been done already
    17     # validity checks should have been done already
    20     modified, added = repo.status(files=files, match=match)[:2]
    18     modified, added = repo.status(files=files, match=match)[:2]
    21     candidates = modified + added
    19     candidates = modified + added
    22 
    20 
    23     if candidates:
    21     if not candidates:
    24         r = repo.changelog.tip()
    22         return False
    25         n = args['node'][:12]
    23 
    26         # TODO: check whether we need parent1&2 like in updatekw
    24     # only check files that have hgkwencode assigned as encode filter
    27         return kwexpander.expandkw(ui, repo, r, n, candidates, update=False)
    25     files = []
       
    26     # python2.4: files = set()
       
    27     for pat, opt in repo.ui.configitems('keyword'):
       
    28         if opt == 'expand':
       
    29             mf = util.matcher(repo.root, '', [pat], [], [])[1]
       
    30             for candidate in candidates:
       
    31                 if mf(candidate) and candidate not in files:
       
    32                     files.append(candidate)
       
    33                 # python2.4:
       
    34                 # if mf(candidate): files.add(candidate)
       
    35 
       
    36     if not files: # nothing to do
       
    37         return False
       
    38 
       
    39     cid = args['node'][:12]
       
    40     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
       
    41     user = util.shortuser(user)
       
    42     date = util.datestr(date=date, format=util.defaultdateformats[0])
       
    43                                                # %Y-%m-%d %H:%M:%S
       
    44 
       
    45     re_kw = re.compile(r'\$Hg[^$]*?\$')
       
    46 
       
    47     for f in files:
       
    48         text = repo.wfile(f).read()
       
    49         if not util.binary(text):
       
    50             kw = '$Hg: %s,v %s %s %s $' % (
       
    51                     os.path.basename(f), cid, date, user)
       
    52             text, kwct = re_kw.subn(kw, text)
       
    53             if kwct:
       
    54                 ui.note(_('expanding keywords in %s\n' % f))
       
    55                 # backup file?
       
    56                 repo.wfile(f, 'w').write(text)