hgkw/keyword.py
branch0.9.2compat
changeset 360 ed06223f88fc
parent 359 4ac7e51443fe
child 366 b1105cc0982f
--- a/hgkw/keyword.py	Wed Jan 30 10:38:10 2008 +0100
+++ b/hgkw/keyword.py	Thu Jan 31 14:34:45 2008 +0100
@@ -79,17 +79,25 @@
 like CVS' $Log$, are not supported. A keyword template map
 "Log = {desc}" expands to the first line of the changeset description.
 
-Caveat: "hg import" fails if the patch context contains an active
-        keyword. In that case run "hg kwshrink", and then reimport.
+Caveat: With Mercurial versions prior to 4574925db5c0 "hg import" might
+        cause rejects if the patch context contains an active keyword.
+        In that case run "hg kwshrink", and then reimport.
         Or, better, use bundle/unbundle to share changes.
 '''
 
-from mercurial import commands, cmdutil, context, filelog, localrepo
-from mercurial import patch, revlog, templater, util
+from mercurial import commands, cmdutil, context, filelog
+from mercurial import localrepo, revlog, templater, util
 from mercurial.node import *
 from mercurial.i18n import gettext as _
 import os.path, re, shutil, tempfile, time
 
+try:
+    # avoid spurious rejects if patchfile is available
+    from mercurial.patch import patchfile
+    _patchfile_init = patchfile.__init__
+except ImportError:
+    _patchfile_init = None
+
 # backwards compatibility hacks
 
 def _normal(repo, files):
@@ -549,7 +557,21 @@
             finally:
                 del _wlock, _lock
 
-    kwrepo._kwt = kwtemplater(ui, repo, inc, exc)
+    kwt = kwrepo._kwt = kwtemplater(ui, repo, inc, exc)
+
+    if _patchfile_init:
+
+        def kwpatchfile_init(self, ui, fname, missing=False):
+            '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
+            rejects or conflicts due to expanded keywords in working dir.'''
+            _patchfile_init(self, ui, fname, missing=missing)
+            if kwt.matcher(self.fname):
+                # shrink keywords read from working dir
+                kwshrunk = kwt.shrink(''.join(self.lines))
+                self.lines = kwshrunk.splitlines(True)
+
+        patchfile.__init__ = kwpatchfile_init
+
     repo.__class__ = kwrepo