diff --git a/hgext/win32text.py b/hgext/win32text.py --- a/hgext/win32text.py +++ b/hgext/win32text.py @@ -30,18 +30,17 @@ import re # 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 @@ def clevertest(s, cmd): 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): diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -10,7 +10,7 @@ from i18n import _ 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 @@ class localrepository(repo.repository): 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 diff --git a/tests/test-win32text b/tests/test-win32text --- a/tests/test-win32text +++ b/tests/test-win32text @@ -9,6 +9,11 @@ for path in sys.argv[1:]: file(path, 'wb').write(data) EOF +cat > print.py <').replace('\r', '').replace('\0', '')) +EOF + hg init echo '[hooks]' >> .hg/hgrc echo 'pretxncommit.crlf = python:hgext.win32text.forbidcrlf' >> .hg/hgrc @@ -62,4 +67,35 @@ echo 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 diff --git a/tests/test-win32text.out b/tests/test-win32text.out --- a/tests/test-win32text.out +++ b/tests/test-win32text.out @@ -155,3 +155,25 @@ 1 +hello +sometext +rem empty + +[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 +sometext +rem empty + +# empty +# empty