Standalone version for keyword.py extension
authorChristian Ebert <blacktrash@gmx.net>
Tue, 19 Dec 2006 15:57:03 +0100
branchextension
changeset 37 3dc31476c148
parent 36 b3ace8cc5a33
child 38 6a830bed4af1
Standalone version for keyword.py Check "expand" patterns in the [keyword] config section. Removed keyword in file itself to simplify patterns. Revert (again) to basename.
hgkw/pretxnkw.py
--- a/hgkw/pretxnkw.py	Tue Dec 19 15:41:45 2006 +0100
+++ b/hgkw/pretxnkw.py	Tue Dec 19 15:57:03 2006 +0100
@@ -1,8 +1,6 @@
-# $Hg$
-
-import kwexpander
-from mercurial import cmdutil, commands
-import sys
+from mercurial.i18n import _
+from mercurial import cmdutil, commands, util
+import os.path, re, sys
 
 def pretxnkw(ui, repo, hooktype, **args):
     '''Collects candidates for keyword expansion on commit
@@ -20,8 +18,39 @@
     modified, added = repo.status(files=files, match=match)[:2]
     candidates = modified + added
 
-    if candidates:
-        r = repo.changelog.tip()
-        n = args['node'][:12]
-        # TODO: check whether we need parent1&2 like in updatekw
-        return kwexpander.expandkw(ui, repo, r, n, candidates, update=False)
+    if not candidates:
+        return False
+
+    # only check files that have hgkwencode assigned as encode filter
+    files = []
+    # python2.4: files = set()
+    for pat, opt in repo.ui.configitems('keyword'):
+        if opt == 'expand':
+            mf = util.matcher(repo.root, '', [pat], [], [])[1]
+            for candidate in candidates:
+                if mf(candidate) and candidate not in files:
+                    files.append(candidate)
+                # python2.4:
+                # if mf(candidate): files.add(candidate)
+
+    if not files: # nothing to do
+        return False
+
+    cid = args['node'][:12]
+    user, date = repo.changelog.read(repo.changelog.tip())[1:3]
+    user = util.shortuser(user)
+    date = util.datestr(date=date, format=util.defaultdateformats[0])
+                                               # %Y-%m-%d %H:%M:%S
+
+    re_kw = re.compile(r'\$Hg[^$]*?\$')
+
+    for f in files:
+        text = repo.wfile(f).read()
+        if not util.binary(text):
+            kw = '$Hg: %s,v %s %s %s $' % (
+                    os.path.basename(f), cid, date, user)
+            text, kwct = re_kw.subn(kw, text)
+            if kwct:
+                ui.note(_('expanding keywords in %s\n' % f))
+                # backup file?
+                repo.wfile(f, 'w').write(text)