hgkw/pretxnkw.py
branchdecodefilter
changeset 6 3ee39807daa5
child 8 180d8484503a
equal deleted inserted replaced
4:7e618d820490 6:3ee39807daa5
       
     1 # $Hg: pretxnkw.py,v$
       
     2 
       
     3 from mercurial.i18n import gettext as _
       
     4 from mercurial.demandload import demandload
       
     5 demandload(globals(), 'hgkw:kwutil mercurial:util os.path re')
       
     6 
       
     7 kwencodefilter = 'hgkwencode'
       
     8 
       
     9 def pretxnkw(ui=None, repo=None, hooktype='', **args):
       
    10     '''Important: returns False on success, True on failure.'''
       
    11 
       
    12     node = args['node'][0:12]
       
    13 
       
    14     if not ui or not repo or not node or hooktype != 'pretxncommit':
       
    15         # bail out with error
       
    16         return True
       
    17 
       
    18     modified, added = repo.status()[1:3]
       
    19     candidates = modified + added
       
    20 
       
    21     files = []
       
    22     for pat, cmd in repo.ui.configitems('encode'):
       
    23         if cmd.endswith(kwencodefilter):
       
    24             mf = util.matcher(repo.root, '', [pat], [], [])[1]
       
    25             for candidate in candidates:
       
    26                 if mf(candidate):
       
    27                     files.append(candidate)
       
    28 
       
    29     if not files: # nothing to do
       
    30         return False
       
    31 
       
    32     re_kw = kwutil.rekw()
       
    33     kword = kwutil.mkkw(repo, tip=True, node=node)
       
    34 
       
    35     re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
       
    36 
       
    37     for filename in files:
       
    38 
       
    39         data = repo.wopener(filename, 'rb').read()
       
    40         bn = os.path.basename(filename)
       
    41 
       
    42         # check for keywords with incorrect basename
       
    43         # eg. if you forgot to update basename manually after "hg mv"
       
    44         failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn]
       
    45         if failures:
       
    46             failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures]
       
    47             ui.warn(_('%d incorrect basenames in file %s:\n'
       
    48                 '%s\nplease correct to %sHg: %s,v$\n'
       
    49                 % (len(failures), filename, ', '.join(failures), '$', bn)))
       
    50             return True
       
    51 
       
    52         # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
       
    53         data, kwct = re_kw.subn(kword, data)
       
    54 
       
    55         if kwct:
       
    56             # backup file and write with expanded keyword
       
    57             ui.note(_('expanding keywords in %s\n' % filename))
       
    58             util.copyfile(filename, filename+'~')
       
    59             repo.wopener(filename, 'wb').write(data)
       
    60 
       
    61     return False