hgkw/kwexpander.py
branchupdatehook
changeset 21 536c1797202d
parent 5 85d1f5bf7cfc
child 23 9fdf507badfc
--- a/hgkw/kwexpander.py	Sat Dec 16 15:42:27 2006 +0100
+++ b/hgkw/kwexpander.py	Sat Dec 16 15:53:45 2006 +0100
@@ -1,56 +1,68 @@
-# $Hg: kwexpander.py,v$
+# $Hg$
 
-from mercurial.i18n import gettext as _
-from mercurial.demandload import demandload
-demandload(globals(), 'mercurial:util')
-demandload(globals(), 'os.path re sys')
+from mercurial import demandimport
+demandimport.enable()
+
+from mercurial.i18n import _
+import mercurial.util, re
 
 # name of keyword encode filter:
 kwencodefilter = 'hgkwencode'
+# look for <Dollar>Hg<Dollar>
+kwtrigger = '%sHg$' % '$'
 
-def expandkw(ui, repo, parent1, node, candidates):
+def wwritekw(ui, repo, f, text):
+    '''Writes text with kwupdates keywords to f in working directory.'''
+    ui.note(_('expanding keywords in %s\n' % f))
+#   # backup file (?)
+#   absfile = repo.wjoin(f)
+#   mercurial.util.copyfile(absfile, absfile+'.kwbak')
+    repo.wfile(f, 'w').write(text)
+
+def expandkw(ui, repo, node, cid, candidates, update=False):
     '''Important: returns False on success, True on failure.'''
 
+    # only check files that have hgkwencode assigned as encode filter
     files = []
     for pat, cmd in repo.ui.configitems('encode'):
         if cmd.endswith(kwencodefilter):
-            mf = util.matcher(repo.root, '', [pat], [], [])[1]
+            mf = mercurial.util.matcher(repo.root, '', [pat], [], [])[1]
             for candidate in candidates:
                 if mf(candidate):
+                    # check again that there really are no duplicates
+                    # if candidate not in files ???
                     files.append(candidate)
 
     if not files: # nothing to do
         return False
 
-    user, date = repo.changelog.read(parent1)[1:3]
-    user = util.shortuser(user)
-    date = util.datestr(date=date, format=util.defaultdateformats[2])
-                                                 # %Y-%m-%d %H:%M
-    re_kwcheck = re.compile(r'[$]Hg: (.*?),v.*?\$')
+    user, date = repo.changelog.read(node)[1:3]
+    user = mercurial.util.shortuser(user)
+    date = mercurial.util.datestr(date=date,
+            format=mercurial.util.defaultdateformats[2])
+                                  # %Y-%m-%d %H:%M
 
-    for fn in files:
+    # collect filenames that were changed by hg update
+    kwupdates = []
 
-        data = repo.wopener(fn, 'rb').read()
-        bn = os.path.basename(fn)
+    for f in files:
+
+        text = repo.wfile(f).read()
+        kwrepl = '%sHg: %s,v %s %s %s $' % ('$', f, cid, date, user)
 
-        # check for keywords with incorrect basename
-        # eg. if you forgot to update basename manually after "hg mv"
-        failures = [m for m in map(str, re_kwcheck.findall(data)) if m != bn]
-        if failures:
-            failures = ['%sHg: %s,v$' % ('$', nobn) for nobn in failures]
-            ui.warn(_('%d incorrect basenames in file %s:\n'
-                '%s\nplease correct to %sHg: %s,v$\n'
-                % (len(failures), fn, ', '.join(failures), '$', bn)))
-            return True
+        if update and text.find(kwtrigger) > -1:
+            text = text.replace(kwtrigger, kwrepl)
+            wwritekw(ui, repo, f, text)
+            kwupdates.append(f)
+        
+        elif not update:
+            re_kw = re.compile(r'[$]Hg(: %s,v [a-z0-9]{12} [^$]+? )?\$' % f)
+            text, kwct = re_kw.subn(kwrepl, text)
+            if kwct:
+                wwritekw(ui, repo, f, text)
 
-        # substitute <Dollar>(Hg|Id): <basename>,v.*<Dollar>
-        re_kw = re.compile(r'([$]Hg: %s,v).*?\$' % bn)
-        data, kwct = re_kw.subn(r'\1 %s %s %s $' % (node, date, user), data)
-
-        if kwct:
-            # backup file and write with expanded keyword
-            ui.note(_('expanding keywords in %s\n' % fn))
-            util.copyfile(fn, fn+'~')
-            repo.wopener(fn, 'wb').write(data)
+    if kwupdates:
+        # cheat hg to believe that updated files were not modified
+        repo.dirstate.update(kwupdates, 'n')
 
     return False