updatehook branch: add kwexpander and updatekw modules updatehook
authorChristian Ebert <blacktrash@gmx.net>
Thu, 14 Dec 2006 08:57:40 +0100
branchupdatehook
changeset 5 85d1f5bf7cfc
parent 3 b9f2c0853da3
child 7 e5f131217f87
updatehook branch: add kwexpander and updatekw modules WARNING: Do not use this branch for production! Update hook changes to working directory are detected by hg. The only /hook/ to escape hg's attention seems to be pretxcommit. And that probably only due to bug. Kept branch for archiving purposes.
hgkw/kwexpander.py
hgkw/updatekw.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgkw/kwexpander.py	Thu Dec 14 08:57:40 2006 +0100
@@ -0,0 +1,56 @@
+# $Hg: kwexpander.py,v$
+
+from mercurial.i18n import gettext as _
+from mercurial.demandload import demandload
+demandload(globals(), 'mercurial:util')
+demandload(globals(), 'os.path re sys')
+
+# name of keyword encode filter:
+kwencodefilter = 'hgkwencode'
+
+def expandkw(ui, repo, parent1, node, candidates):
+    '''Important: returns False on success, True on failure.'''
+
+    files = []
+    for pat, cmd in repo.ui.configitems('encode'):
+        if cmd.endswith(kwencodefilter):
+            mf = util.matcher(repo.root, '', [pat], [], [])[1]
+            for candidate in candidates:
+                if mf(candidate):
+                    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.*?\$')
+
+    for fn in files:
+
+        data = repo.wopener(fn, 'rb').read()
+        bn = os.path.basename(fn)
+
+        # 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
+
+        # 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)
+
+    return False
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgkw/updatekw.py	Thu Dec 14 08:57:40 2006 +0100
@@ -0,0 +1,20 @@
+# $Hg: updatekw.py,v$
+
+from hgkw import kwexpander
+
+def updatekw(ui=None, repo=None, hooktype='', **args):
+    '''Important: returns False on success, True on failure.'''
+
+    if not ui or not repo or hooktype != 'update':
+        # bail out with error
+        return True
+
+    parent1 = repo.dirstate.parents()[0]
+    node = repo.changectx(parent1)
+
+    (modified, added, removed, deleted,
+            unknown, ignored, clean) = repo.status(node1=parent1)
+
+    candidates = modified + added + clean
+
+    return kwexpander.expandkw(ui, repo, parent1, node, candidates)