hgkw/kwexpander.py
branchupdatehook
changeset 5 85d1f5bf7cfc
child 21 536c1797202d
equal deleted inserted replaced
3:b9f2c0853da3 5:85d1f5bf7cfc
       
     1 # $Hg: kwexpander.py,v$
       
     2 
       
     3 from mercurial.i18n import gettext as _
       
     4 from mercurial.demandload import demandload
       
     5 demandload(globals(), 'mercurial:util')
       
     6 demandload(globals(), 'os.path re sys')
       
     7 
       
     8 # name of keyword encode filter:
       
     9 kwencodefilter = 'hgkwencode'
       
    10 
       
    11 def expandkw(ui, repo, parent1, node, candidates):
       
    12     '''Important: returns False on success, True on failure.'''
       
    13 
       
    14     files = []
       
    15     for pat, cmd in repo.ui.configitems('encode'):
       
    16         if cmd.endswith(kwencodefilter):
       
    17             mf = util.matcher(repo.root, '', [pat], [], [])[1]
       
    18             for candidate in candidates:
       
    19                 if mf(candidate):
       
    20                     files.append(candidate)
       
    21 
       
    22     if not files: # nothing to do
       
    23         return False
       
    24 
       
    25     user, date = repo.changelog.read(parent1)[1:3]
       
    26     user = util.shortuser(user)
       
    27     date = util.datestr(date=date, format=util.defaultdateformats[2])
       
    28                                                  # %Y-%m-%d %H:%M
       
    29     re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
       
    30 
       
    31     for fn in files:
       
    32 
       
    33         data = repo.wopener(fn, 'rb').read()
       
    34         bn = os.path.basename(fn)
       
    35 
       
    36         # check for keywords with incorrect basename
       
    37         # eg. if you forgot to update basename manually after "hg mv"
       
    38         failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn]
       
    39         if failures:
       
    40             failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures]
       
    41             ui.warn(_('%d incorrect basenames in file %s:\n'
       
    42                 '%s\nplease correct to %sHg: %s,v$\n'
       
    43                 % (len(failures), fn, ', '.join(failures), '$', bn)))
       
    44             return True
       
    45 
       
    46         # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
       
    47         re_kw = re.compile(r'([$]Hg: %s,v).*?\$' % bn)
       
    48         data, kwct = re_kw.subn(r'\1 %s %s %s $' % (node, date, user), data)
       
    49 
       
    50         if kwct:
       
    51             # backup file and write with expanded keyword
       
    52             ui.note(_('expanding keywords in %s\n' % fn))
       
    53             util.copyfile(fn, fn+'~')
       
    54             repo.wopener(fn, 'wb').write(data)
       
    55 
       
    56     return False