##// END OF EJS Templates
Provide better context for custom Python encode/decode filters....
Jesse Glick -
r5967:f8ad3b76 default
parent child Browse files
Show More
@@ -30,18 +30,17 b' import re'
30 30 # regexp for single LF without CR preceding.
31 31 re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
32 32
33 def dumbdecode(s, cmd):
33 def dumbdecode(s, cmd, ui=None, repo=None, filename=None, **kwargs):
34 34 # warn if already has CRLF in repository.
35 35 # it might cause unexpected eol conversion.
36 36 # see issue 302:
37 37 # http://www.selenic.com/mercurial/bts/issue302
38 if '\r\n' in s:
39 u = ui.ui()
40 u.warn(_('WARNING: file in repository already has CRLF line ending \n'
41 ' which does not need eol conversion by win32text plugin.\n'
42 ' Please reconsider encode/decode setting in'
43 ' mercurial.ini or .hg/hgrc\n'
44 ' before next commit.\n'))
38 if '\r\n' in s and ui and filename and repo:
39 ui.warn(_('WARNING: %s already has CRLF line endings\n'
40 'and does not need EOL conversion by the win32text plugin.\n'
41 'Before your next commit, please reconsider your '
42 'encode/decode settings in \nMercurial.ini or %s.\n') %
43 (filename, repo.join('hgrc')))
45 44 # replace single LF to CRLF
46 45 return re_single_lf.sub('\\1\r\n', s)
47 46
@@ -52,9 +51,9 b' def clevertest(s, cmd):'
52 51 if '\0' in s: return False
53 52 return True
54 53
55 def cleverdecode(s, cmd):
54 def cleverdecode(s, cmd, **kwargs):
56 55 if clevertest(s, cmd):
57 return dumbdecode(s, cmd)
56 return dumbdecode(s, cmd, **kwargs)
58 57 return s
59 58
60 59 def cleverencode(s, cmd):
@@ -10,7 +10,7 b' from i18n import _'
10 10 import repo, changegroup
11 11 import changelog, dirstate, filelog, manifest, context, weakref
12 12 import re, lock, transaction, tempfile, stat, errno, ui
13 import os, revlog, time, util, extensions, hook
13 import os, revlog, time, util, extensions, hook, inspect
14 14
15 15 class localrepository(repo.repository):
16 16 capabilities = util.set(('lookup', 'changegroupsubset'))
@@ -492,14 +492,18 b' class localrepository(repo.repository):'
492 492 fn = filterfn
493 493 break
494 494 if not fn:
495 fn = lambda s, c: util.filter(s, c)
495 fn = lambda s, c, **kwargs: util.filter(s, c)
496 # Wrap old filters not supporting keyword arguments
497 if not inspect.getargspec(fn)[2]:
498 oldfn = fn
499 fn = lambda s, c, **kwargs: oldfn(s, c)
496 500 l.append((mf, fn, cmd))
497 501 self.filterpats[filter] = l
498 502
499 503 for mf, fn, cmd in self.filterpats[filter]:
500 504 if mf(filename):
501 505 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
502 data = fn(data, cmd)
506 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
503 507 break
504 508
505 509 return data
@@ -9,6 +9,11 b' for path in sys.argv[1:]:'
9 9 file(path, 'wb').write(data)
10 10 EOF
11 11
12 cat > print.py <<EOF
13 import sys
14 print(sys.stdin.read().replace('\n', '<LF>').replace('\r', '<CR>').replace('\0', '<NUL>'))
15 EOF
16
12 17 hg init
13 18 echo '[hooks]' >> .hg/hgrc
14 19 echo 'pretxncommit.crlf = python:hgext.win32text.forbidcrlf' >> .hg/hgrc
@@ -62,4 +67,35 b' echo'
62 67 hg log -v
63 68 echo
64 69
65 # XXX missing tests for encode/decode hooks
70 rm .hg/hgrc
71 (echo some; echo text) > f3
72 python -c 'file("f4.bat", "wb").write("rem empty\x0D\x0A")'
73 hg add f3 f4.bat
74 hg ci -m 6 -d'0 0'
75
76 python print.py < bin
77 python print.py < f3
78 python print.py < f4.bat
79 echo
80
81 echo '[extensions]' >> .hg/hgrc
82 echo 'win32text = ' >> .hg/hgrc
83 echo '[decode]' >> .hg/hgrc
84 echo '** = cleverdecode:' >> .hg/hgrc
85 echo '[encode]' >> .hg/hgrc
86 echo '** = cleverencode:' >> .hg/hgrc
87 cat .hg/hgrc
88 echo
89
90 rm f3 f4.bat bin
91 hg co 2>&1 | python -c 'import sys, os; sys.stdout.write(sys.stdin.read().replace(os.getcwd(), "...."))'
92 python print.py < bin
93 python print.py < f3
94 python print.py < f4.bat
95 echo
96
97 python -c 'file("f5.sh", "wb").write("# empty\x0D\x0A")'
98 hg add f5.sh
99 hg ci -m 7 -d'0 0'
100 python print.py < f5.sh
101 hg cat f5.sh | python print.py
@@ -155,3 +155,25 b' 1'
155 155
156 156
157 157
158 hello<NUL><CR><LF>
159 some<LF>text<LF>
160 rem empty<CR><LF>
161
162 [extensions]
163 win32text =
164 [decode]
165 ** = cleverdecode:
166 [encode]
167 ** = cleverencode:
168
169 WARNING: f4.bat already has CRLF line endings
170 and does not need EOL conversion by the win32text plugin.
171 Before your next commit, please reconsider your encode/decode settings in
172 Mercurial.ini or ..../.hg/hgrc.
173 3 files updated, 0 files merged, 0 files removed, 0 files unresolved
174 hello<NUL><CR><LF>
175 some<CR><LF>text<CR><LF>
176 rem empty<CR><LF>
177
178 # empty<CR><LF>
179 # empty<LF>
General Comments 0
You need to be logged in to leave comments. Login now