hgkw/kwexpander.py
branchupdatehook
changeset 29 bda83c13da42
parent 25 aa3fbe81e9ad
child 30 bc3bdd329831
equal deleted inserted replaced
28:c7f5821f3513 29:bda83c13da42
     2 
     2 
     3 from mercurial.i18n import _
     3 from mercurial.i18n import _
     4 from mercurial import util
     4 from mercurial import util
     5 import re
     5 import re
     6 
     6 
     7 def expandkw(ui, repo, node, cid, candidates, update=False):
     7 def expandkw(ui, repo, rev, cid, candidates, update=False):
     8     '''Important: returns False on success, True on failure.'''
     8     '''Important: returns False on success, True on failure.'''
     9 
     9 
    10     # name of keyword encode filter:
    10     # name of keyword encode filter:
    11     kwencodefilter = 'hgkwencode'
    11     kwencodefilter = 'hgkwencode'
    12 
    12 
    13     # look for <Dollar>Hg<Dollar>
    13     # update only needs string search for encoded keyword
    14     kwtrigger = '%sHg$' % '$'
    14     # as hgkwencode always runs before
       
    15     kwstr = '%sHg$' % '$'
       
    16 
       
    17     # pretxncommit looks for both encoded and decoded keywords
       
    18     kwpat = r'[$]Hg(: %s,v [a-z0-9]{12} [^$]+? )?\$'
    15 
    19 
    16     def wwritekw(ui, repo, f, text):
    20     def wwritekw(ui, repo, f, text):
    17         '''Writes text with kwupdates keywords to f in working directory.'''
    21         '''Writes text with kwupdates keywords to f in working directory.'''
    18         ui.note(_('expanding keywords in %s\n' % f))
    22         ui.note(_('expanding keywords in %s\n' % f))
    19     #   # backup file (?)
    23     #   # backup file in case of commit (?)
    20     #   absfile = repo.wjoin(f)
    24     #   absfile = repo.wjoin(f)
    21     #   util.copyfile(absfile, absfile+'.kwbak')
    25     #   util.copyfile(absfile, absfile+'.kwbak')
    22         repo.wfile(f, 'w').write(text)
    26         repo.wfile(f, 'w').write(text)
    23 
    27 
    24     # only check files that have hgkwencode assigned as encode filter
    28     # only check files that have hgkwencode assigned as encode filter
    34                 # if mf(candidate): files.add(candidate)
    38                 # if mf(candidate): files.add(candidate)
    35 
    39 
    36     if not files: # nothing to do
    40     if not files: # nothing to do
    37         return False
    41         return False
    38 
    42 
    39     user, date = repo.changelog.read(node)[1:3]
    43     user, date = repo.changelog.read(rev)[1:3]
    40     user = util.shortuser(user)
    44     user = util.shortuser(user)
    41     date = util.datestr(date=date, format=util.defaultdateformats[2])
    45     date = util.datestr(date=date, format=util.defaultdateformats[2])
    42                                                # %Y-%m-%d %H:%M
    46                                                # %Y-%m-%d %H:%M
    43 
    47 
    44     # collect filenames that were changed by hg update
    48     # collect filenames that were changed by hg update
    45     kwupdates = []
    49     kwupdates = []
    46 
    50 
    47     for f in files:
    51     for f in files:
    48 
    52 
    49         text = repo.wfile(f).read()
    53         text = repo.wfile(f).read()
    50         kwrepl = '%sHg: %s,v %s %s %s $' % ('$', f, cid, date, user)
    54         kw = '%sHg: %s,v %s %s %s $' % ('$', f, cid, date, user)
    51 
    55 
    52         if update and text.find(kwtrigger) > -1:
    56         if update and text.find(kwstr) > -1:
    53             text = text.replace(kwtrigger, kwrepl)
    57             text = text.replace(kwstr, kw)
    54             wwritekw(ui, repo, f, text)
    58             wwritekw(ui, repo, f, text)
    55             kwupdates.append(f)
    59             kwupdates.append(f)
    56         
    60         
    57         elif not update:
    61         elif not update:
    58             re_kw = re.compile(r'[$]Hg(: %s,v [a-z0-9]{12} [^$]+? )?\$' % f)
    62             re_kw = re.compile(kwpat % f)
    59             text, kwct = re_kw.subn(kwrepl, text)
    63             text, kwct = re_kw.subn(kw, text)
    60             if kwct:
    64             if kwct:
    61                 wwritekw(ui, repo, f, text)
    65                 wwritekw(ui, repo, f, text)
    62 
    66 
    63     if kwupdates:
    67     if kwupdates:
    64         # cheat hg to believe that updated files were not modified
    68         # cheat hg to believe that updated files were not modified
    65         repo.dirstate.update(kwupdates, 'n')
    69         repo.dirstate.update(kwupdates, 'n')
    66 
       
    67     return False