Provide better context for custom Python encode/decode filters.
While some can function with just some text and an optional command name,
others may want a repository object, a ui object, and a file path.
Use the enhanced information to good effect in win32text.dumbdecode's warning.
--- a/hgext/win32text.py Mon Jan 28 21:39:47 2008 +0100
+++ b/hgext/win32text.py Fri Dec 21 23:21:17 2007 -0500
@@ -30,18 +30,17 @@
# regexp for single LF without CR preceding.
re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
-def dumbdecode(s, cmd):
+def dumbdecode(s, cmd, ui=None, repo=None, filename=None, **kwargs):
# warn if already has CRLF in repository.
# it might cause unexpected eol conversion.
# see issue 302:
# http://www.selenic.com/mercurial/bts/issue302
- if '\r\n' in s:
- u = ui.ui()
- u.warn(_('WARNING: file in repository already has CRLF line ending \n'
- ' which does not need eol conversion by win32text plugin.\n'
- ' Please reconsider encode/decode setting in'
- ' mercurial.ini or .hg/hgrc\n'
- ' before next commit.\n'))
+ if '\r\n' in s and ui and filename and repo:
+ ui.warn(_('WARNING: %s already has CRLF line endings\n'
+ 'and does not need EOL conversion by the win32text plugin.\n'
+ 'Before your next commit, please reconsider your '
+ 'encode/decode settings in \nMercurial.ini or %s.\n') %
+ (filename, repo.join('hgrc')))
# replace single LF to CRLF
return re_single_lf.sub('\\1\r\n', s)
@@ -52,9 +51,9 @@
if '\0' in s: return False
return True
-def cleverdecode(s, cmd):
+def cleverdecode(s, cmd, **kwargs):
if clevertest(s, cmd):
- return dumbdecode(s, cmd)
+ return dumbdecode(s, cmd, **kwargs)
return s
def cleverencode(s, cmd):
--- a/mercurial/localrepo.py Mon Jan 28 21:39:47 2008 +0100
+++ b/mercurial/localrepo.py Fri Dec 21 23:21:17 2007 -0500
@@ -10,7 +10,7 @@
import repo, changegroup
import changelog, dirstate, filelog, manifest, context, weakref
import re, lock, transaction, tempfile, stat, errno, ui
-import os, revlog, time, util, extensions, hook
+import os, revlog, time, util, extensions, hook, inspect
class localrepository(repo.repository):
capabilities = util.set(('lookup', 'changegroupsubset'))
@@ -492,14 +492,18 @@
fn = filterfn
break
if not fn:
- fn = lambda s, c: util.filter(s, c)
+ fn = lambda s, c, **kwargs: util.filter(s, c)
+ # Wrap old filters not supporting keyword arguments
+ if not inspect.getargspec(fn)[2]:
+ oldfn = fn
+ fn = lambda s, c, **kwargs: oldfn(s, c)
l.append((mf, fn, cmd))
self.filterpats[filter] = l
for mf, fn, cmd in self.filterpats[filter]:
if mf(filename):
self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
- data = fn(data, cmd)
+ data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
break
return data
--- a/tests/test-win32text Mon Jan 28 21:39:47 2008 +0100
+++ b/tests/test-win32text Fri Dec 21 23:21:17 2007 -0500
@@ -9,6 +9,11 @@
file(path, 'wb').write(data)
EOF
+cat > print.py <<EOF
+import sys
+print(sys.stdin.read().replace('\n', '<LF>').replace('\r', '<CR>').replace('\0', '<NUL>'))
+EOF
+
hg init
echo '[hooks]' >> .hg/hgrc
echo 'pretxncommit.crlf = python:hgext.win32text.forbidcrlf' >> .hg/hgrc
@@ -62,4 +67,35 @@
hg log -v
echo
-# XXX missing tests for encode/decode hooks
+rm .hg/hgrc
+(echo some; echo text) > f3
+python -c 'file("f4.bat", "wb").write("rem empty\x0D\x0A")'
+hg add f3 f4.bat
+hg ci -m 6 -d'0 0'
+
+python print.py < bin
+python print.py < f3
+python print.py < f4.bat
+echo
+
+echo '[extensions]' >> .hg/hgrc
+echo 'win32text = ' >> .hg/hgrc
+echo '[decode]' >> .hg/hgrc
+echo '** = cleverdecode:' >> .hg/hgrc
+echo '[encode]' >> .hg/hgrc
+echo '** = cleverencode:' >> .hg/hgrc
+cat .hg/hgrc
+echo
+
+rm f3 f4.bat bin
+hg co 2>&1 | python -c 'import sys, os; sys.stdout.write(sys.stdin.read().replace(os.getcwd(), "...."))'
+python print.py < bin
+python print.py < f3
+python print.py < f4.bat
+echo
+
+python -c 'file("f5.sh", "wb").write("# empty\x0D\x0A")'
+hg add f5.sh
+hg ci -m 7 -d'0 0'
+python print.py < f5.sh
+hg cat f5.sh | python print.py
--- a/tests/test-win32text.out Mon Jan 28 21:39:47 2008 +0100
+++ b/tests/test-win32text.out Fri Dec 21 23:21:17 2007 -0500
@@ -155,3 +155,25 @@
+hello<NUL><CR><LF>
+some<LF>text<LF>
+rem empty<CR><LF>
+
+[extensions]
+win32text =
+[decode]
+** = cleverdecode:
+[encode]
+** = cleverencode:
+
+WARNING: f4.bat already has CRLF line endings
+and does not need EOL conversion by the win32text plugin.
+Before your next commit, please reconsider your encode/decode settings in
+Mercurial.ini or ..../.hg/hgrc.
+3 files updated, 0 files merged, 0 files removed, 0 files unresolved
+hello<NUL><CR><LF>
+some<CR><LF>text<CR><LF>
+rem empty<CR><LF>
+
+# empty<CR><LF>
+# empty<LF>