hgkw/pretxnkw.py
branchextension
changeset 42 ba000e29ecf3
parent 41 e1c539e1282b
child 44 dc6e7d0e607f
equal deleted inserted replaced
41:e1c539e1282b 42:ba000e29ecf3
     1 from mercurial.i18n import _
     1 from mercurial.i18n import _
     2 from mercurial import cmdutil, commands, util
     2 from mercurial import cmdutil, commands, util
     3 import os.path, re, sys
     3 import os.path, re, sys
       
     4 
       
     5 hgkeywords = 'Id|Header|Author|Date|Revision|RCSFile|Source'
     4 
     6 
     5 def pretxnkw(ui, repo, hooktype, **args):
     7 def pretxnkw(ui, repo, hooktype, **args):
     6     '''Collects candidates for keyword expansion on commit
     8     '''Collects candidates for keyword expansion on commit
     7     and expands keywords in working dir.
     9     and expands keywords in working dir.
     8     NOTE: for use in combination with hgext.keyword!'''
    10     NOTE: for use in combination with hgext.keyword!'''
    35                 # if mf(candidate): files.add(candidate)
    37                 # if mf(candidate): files.add(candidate)
    36 
    38 
    37     if not files: # nothing to do
    39     if not files: # nothing to do
    38         return False
    40         return False
    39 
    41 
    40     cid = args['node'][:12]
       
    41     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
    42     user, date = repo.changelog.read(repo.changelog.tip())[1:3]
    42     user = util.shortuser(user)
    43     strdate = util.datestr(date=date)
    43     date = util.datestr(date=date, format=util.defaultdateformats[0])
    44     shortuser = util.shortuser(user)
       
    45     shortdate = util.datestr(date=date, format=util.defaultdateformats[0])
    44                                                # %Y-%m-%d %H:%M:%S
    46                                                # %Y-%m-%d %H:%M:%S
    45 
    47 
    46     re_kw = re.compile(r'\$Hg(: [^$]+?,v [a-z0-9]{12} [^$]+? )?\$')
    48     re_kw = re.compile(r'\$(%s)(: [^$]+? )?\$' % hgkeywords)
    47 
    49 
    48     for f in files:
    50     for f in files:
    49         data = repo.wfile(f).read()
    51         data = repo.wfile(f).read()
    50         if not util.binary(data):
    52         if not util.binary(data):
    51             kw = '$Hg: %s,v %s %s %s $' % (
    53 
    52                     os.path.basename(f), cid, date, user)
    54             def kwexpand(matchobj):
    53             data, kwct = re_kw.subn(kw, data)
    55                 RCSFile = os.path.basename(f)+',v'
       
    56                 Source = os.path.join(repo.root, f)+',v'
       
    57                 Revision = args['node'][:12]
       
    58                 Date = strdate
       
    59                 Author = user
       
    60                 revdateauth = '%s %s %s' % (Revision, shortdate, shortuser)
       
    61                 Header = '%s %s' % (Source, revdateauth)
       
    62                 Id = '%s %s' % (RCSFile, revdateauth)
       
    63                 return '$%s: %s $' % (
       
    64                         matchobj.group(1), eval(matchobj.group(1)))
       
    65 
       
    66             data, kwct = re_kw.subn(kwexpand, data)
    54             if kwct:
    67             if kwct:
    55                 ui.note(_('expanding keywords in %s\n' % f))
    68                 ui.note(_('expanding keywords in %s\n' % f))
    56                 # backup file?
    69                 # backup file?
    57                 repo.wfile(f, 'w').write(data)
    70                 repo.wfile(f, 'w').write(data)