##// END OF EJS Templates
win32text: lowercase warning message
Martin Geisler -
r16932:7985a9e2 default
parent child Browse files
Show More
@@ -1,172 +1,172 b''
1 1 # win32text.py - LF <-> CRLF/CR translation utilities for Windows/Mac users
2 2 #
3 3 # Copyright 2005, 2007-2009 Matt Mackall <mpm@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 '''perform automatic newline conversion
9 9
10 10 Deprecation: The win32text extension requires each user to configure
11 11 the extension again and again for each clone since the configuration
12 12 is not copied when cloning.
13 13
14 14 We have therefore made the ``eol`` as an alternative. The ``eol``
15 15 uses a version controlled file for its configuration and each clone
16 16 will therefore use the right settings from the start.
17 17
18 18 To perform automatic newline conversion, use::
19 19
20 20 [extensions]
21 21 win32text =
22 22 [encode]
23 23 ** = cleverencode:
24 24 # or ** = macencode:
25 25
26 26 [decode]
27 27 ** = cleverdecode:
28 28 # or ** = macdecode:
29 29
30 30 If not doing conversion, to make sure you do not commit CRLF/CR by accident::
31 31
32 32 [hooks]
33 33 pretxncommit.crlf = python:hgext.win32text.forbidcrlf
34 34 # or pretxncommit.cr = python:hgext.win32text.forbidcr
35 35
36 36 To do the same check on a server to prevent CRLF/CR from being
37 37 pushed or pulled::
38 38
39 39 [hooks]
40 40 pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf
41 41 # or pretxnchangegroup.cr = python:hgext.win32text.forbidcr
42 42 '''
43 43
44 44 from mercurial.i18n import _
45 45 from mercurial.node import short
46 46 from mercurial import util
47 47 import re
48 48
49 49 testedwith = 'internal'
50 50
51 51 # regexp for single LF without CR preceding.
52 52 re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
53 53
54 54 newlinestr = {'\r\n': 'CRLF', '\r': 'CR'}
55 55 filterstr = {'\r\n': 'clever', '\r': 'mac'}
56 56
57 57 def checknewline(s, newline, ui=None, repo=None, filename=None):
58 58 # warn if already has 'newline' in repository.
59 59 # it might cause unexpected eol conversion.
60 60 # see issue 302:
61 61 # http://mercurial.selenic.com/bts/issue302
62 62 if newline in s and ui and filename and repo:
63 63 ui.warn(_('WARNING: %s already has %s line endings\n'
64 64 'and does not need EOL conversion by the win32text plugin.\n'
65 65 'Before your next commit, please reconsider your '
66 66 'encode/decode settings in \nMercurial.ini or %s.\n') %
67 67 (filename, newlinestr[newline], repo.join('hgrc')))
68 68
69 69 def dumbdecode(s, cmd, **kwargs):
70 70 checknewline(s, '\r\n', **kwargs)
71 71 # replace single LF to CRLF
72 72 return re_single_lf.sub('\\1\r\n', s)
73 73
74 74 def dumbencode(s, cmd):
75 75 return s.replace('\r\n', '\n')
76 76
77 77 def macdumbdecode(s, cmd, **kwargs):
78 78 checknewline(s, '\r', **kwargs)
79 79 return s.replace('\n', '\r')
80 80
81 81 def macdumbencode(s, cmd):
82 82 return s.replace('\r', '\n')
83 83
84 84 def cleverdecode(s, cmd, **kwargs):
85 85 if not util.binary(s):
86 86 return dumbdecode(s, cmd, **kwargs)
87 87 return s
88 88
89 89 def cleverencode(s, cmd):
90 90 if not util.binary(s):
91 91 return dumbencode(s, cmd)
92 92 return s
93 93
94 94 def macdecode(s, cmd, **kwargs):
95 95 if not util.binary(s):
96 96 return macdumbdecode(s, cmd, **kwargs)
97 97 return s
98 98
99 99 def macencode(s, cmd):
100 100 if not util.binary(s):
101 101 return macdumbencode(s, cmd)
102 102 return s
103 103
104 104 _filters = {
105 105 'dumbdecode:': dumbdecode,
106 106 'dumbencode:': dumbencode,
107 107 'cleverdecode:': cleverdecode,
108 108 'cleverencode:': cleverencode,
109 109 'macdumbdecode:': macdumbdecode,
110 110 'macdumbencode:': macdumbencode,
111 111 'macdecode:': macdecode,
112 112 'macencode:': macencode,
113 113 }
114 114
115 115 def forbidnewline(ui, repo, hooktype, node, newline, **kwargs):
116 116 halt = False
117 117 seen = set()
118 118 # we try to walk changesets in reverse order from newest to
119 119 # oldest, so that if we see a file multiple times, we take the
120 120 # newest version as canonical. this prevents us from blocking a
121 121 # changegroup that contains an unacceptable commit followed later
122 122 # by a commit that fixes the problem.
123 123 tip = repo['tip']
124 124 for rev in xrange(len(repo)-1, repo[node].rev()-1, -1):
125 125 c = repo[rev]
126 126 for f in c.files():
127 127 if f in seen or f not in tip or f not in c:
128 128 continue
129 129 seen.add(f)
130 130 data = c[f].data()
131 131 if not util.binary(data) and newline in data:
132 132 if not halt:
133 ui.warn(_('Attempt to commit or push text file(s) '
133 ui.warn(_('attempt to commit or push text file(s) '
134 134 'using %s line endings\n') %
135 135 newlinestr[newline])
136 136 ui.warn(_('in %s: %s\n') % (short(c.node()), f))
137 137 halt = True
138 138 if halt and hooktype == 'pretxnchangegroup':
139 139 crlf = newlinestr[newline].lower()
140 140 filter = filterstr[newline]
141 141 ui.warn(_('\nTo prevent this mistake in your local repository,\n'
142 142 'add to Mercurial.ini or .hg/hgrc:\n'
143 143 '\n'
144 144 '[hooks]\n'
145 145 'pretxncommit.%s = python:hgext.win32text.forbid%s\n'
146 146 '\n'
147 147 'and also consider adding:\n'
148 148 '\n'
149 149 '[extensions]\n'
150 150 'win32text =\n'
151 151 '[encode]\n'
152 152 '** = %sencode:\n'
153 153 '[decode]\n'
154 154 '** = %sdecode:\n') % (crlf, crlf, filter, filter))
155 155 return halt
156 156
157 157 def forbidcrlf(ui, repo, hooktype, node, **kwargs):
158 158 return forbidnewline(ui, repo, hooktype, node, '\r\n', **kwargs)
159 159
160 160 def forbidcr(ui, repo, hooktype, node, **kwargs):
161 161 return forbidnewline(ui, repo, hooktype, node, '\r', **kwargs)
162 162
163 163 def reposetup(ui, repo):
164 164 if not repo.local():
165 165 return
166 166 for name, fn in _filters.iteritems():
167 167 repo.adddatafilter(name, fn)
168 168
169 169 def extsetup(ui):
170 170 if ui.configbool('win32text', 'warn', True):
171 171 ui.warn(_("win32text is deprecated: "
172 172 "http://mercurial.selenic.com/wiki/Win32TextExtension\n"))
@@ -1,38 +1,38 b''
1 1
2 2 $ cat > unix2mac.py <<EOF
3 3 > import sys
4 4 >
5 5 > for path in sys.argv[1:]:
6 6 > data = file(path, 'rb').read()
7 7 > data = data.replace('\n', '\r')
8 8 > file(path, 'wb').write(data)
9 9 > EOF
10 10 $ cat > print.py <<EOF
11 11 > import sys
12 12 > print(sys.stdin.read().replace('\n', '<LF>').replace('\r', '<CR>').replace('\0', '<NUL>'))
13 13 > EOF
14 14 $ hg init
15 15 $ echo '[hooks]' >> .hg/hgrc
16 16 $ echo 'pretxncommit.cr = python:hgext.win32text.forbidcr' >> .hg/hgrc
17 17 $ echo 'pretxnchangegroup.cr = python:hgext.win32text.forbidcr' >> .hg/hgrc
18 18 $ cat .hg/hgrc
19 19 [hooks]
20 20 pretxncommit.cr = python:hgext.win32text.forbidcr
21 21 pretxnchangegroup.cr = python:hgext.win32text.forbidcr
22 22
23 23 $ echo hello > f
24 24 $ hg add f
25 25 $ hg ci -m 1
26 26
27 27 $ python unix2mac.py f
28 28 $ hg ci -m 2
29 Attempt to commit or push text file(s) using CR line endings
29 attempt to commit or push text file(s) using CR line endings
30 30 in dea860dc51ec: f
31 31 transaction abort!
32 32 rollback completed
33 33 abort: pretxncommit.cr hook failed
34 34 [255]
35 35 $ hg cat f | python print.py
36 36 hello<LF>
37 37 $ cat f | python print.py
38 38 hello<CR>
@@ -1,426 +1,426 b''
1 1
2 2 $ hg init t
3 3 $ cd t
4 4 $ cat > unix2dos.py <<EOF
5 5 > import sys
6 6 >
7 7 > for path in sys.argv[1:]:
8 8 > data = file(path, 'rb').read()
9 9 > data = data.replace('\n', '\r\n')
10 10 > file(path, 'wb').write(data)
11 11 > EOF
12 12 $ echo '[hooks]' >> .hg/hgrc
13 13 $ echo 'pretxncommit.crlf = python:hgext.win32text.forbidcrlf' >> .hg/hgrc
14 14 $ echo 'pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf' >> .hg/hgrc
15 15 $ cat .hg/hgrc
16 16 [hooks]
17 17 pretxncommit.crlf = python:hgext.win32text.forbidcrlf
18 18 pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf
19 19
20 20 $ echo hello > f
21 21 $ hg add f
22 22
23 23 commit should succeed
24 24
25 25 $ hg ci -m 1
26 26
27 27 $ hg clone . ../zoz
28 28 updating to branch default
29 29 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
30 30 $ cp .hg/hgrc ../zoz/.hg
31 31 $ python unix2dos.py f
32 32
33 33 commit should fail
34 34
35 35 $ hg ci -m 2.1
36 Attempt to commit or push text file(s) using CRLF line endings
36 attempt to commit or push text file(s) using CRLF line endings
37 37 in f583ea08d42a: f
38 38 transaction abort!
39 39 rollback completed
40 40 abort: pretxncommit.crlf hook failed
41 41 [255]
42 42
43 43 $ mv .hg/hgrc .hg/hgrc.bak
44 44
45 45 commits should succeed
46 46
47 47 $ hg ci -m 2
48 48 $ hg cp f g
49 49 $ hg ci -m 2.2
50 50
51 51 push should fail
52 52
53 53 $ hg push ../zoz
54 54 pushing to ../zoz
55 55 searching for changes
56 56 adding changesets
57 57 adding manifests
58 58 adding file changes
59 59 added 2 changesets with 2 changes to 2 files
60 Attempt to commit or push text file(s) using CRLF line endings
60 attempt to commit or push text file(s) using CRLF line endings
61 61 in bc2d09796734: g
62 62 in b1aa5cde7ff4: f
63 63
64 64 To prevent this mistake in your local repository,
65 65 add to Mercurial.ini or .hg/hgrc:
66 66
67 67 [hooks]
68 68 pretxncommit.crlf = python:hgext.win32text.forbidcrlf
69 69
70 70 and also consider adding:
71 71
72 72 [extensions]
73 73 win32text =
74 74 [encode]
75 75 ** = cleverencode:
76 76 [decode]
77 77 ** = cleverdecode:
78 78 transaction abort!
79 79 rollback completed
80 80 abort: pretxnchangegroup.crlf hook failed
81 81 [255]
82 82
83 83 $ mv .hg/hgrc.bak .hg/hgrc
84 84 $ echo hello > f
85 85 $ hg rm g
86 86
87 87 commit should succeed
88 88
89 89 $ hg ci -m 2.3
90 90
91 91 push should succeed
92 92
93 93 $ hg push ../zoz
94 94 pushing to ../zoz
95 95 searching for changes
96 96 adding changesets
97 97 adding manifests
98 98 adding file changes
99 99 added 3 changesets with 3 changes to 2 files
100 100
101 101 and now for something completely different
102 102
103 103 $ mkdir d
104 104 $ echo hello > d/f2
105 105 $ python unix2dos.py d/f2
106 106 $ hg add d/f2
107 107 $ hg ci -m 3
108 Attempt to commit or push text file(s) using CRLF line endings
108 attempt to commit or push text file(s) using CRLF line endings
109 109 in 053ba1a3035a: d/f2
110 110 transaction abort!
111 111 rollback completed
112 112 abort: pretxncommit.crlf hook failed
113 113 [255]
114 114 $ hg revert -a
115 115 forgetting d/f2 (glob)
116 116 $ rm d/f2
117 117
118 118 $ hg rem f
119 119 $ hg ci -m 4
120 120
121 121 $ python -c 'file("bin", "wb").write("hello\x00\x0D\x0A")'
122 122 $ hg add bin
123 123 $ hg ci -m 5
124 124 $ hg log -v
125 125 changeset: 5:f0b1c8d75fce
126 126 tag: tip
127 127 user: test
128 128 date: Thu Jan 01 00:00:00 1970 +0000
129 129 files: bin
130 130 description:
131 131 5
132 132
133 133
134 134 changeset: 4:77796dbcd4ad
135 135 user: test
136 136 date: Thu Jan 01 00:00:00 1970 +0000
137 137 files: f
138 138 description:
139 139 4
140 140
141 141
142 142 changeset: 3:7c1b5430b350
143 143 user: test
144 144 date: Thu Jan 01 00:00:00 1970 +0000
145 145 files: f g
146 146 description:
147 147 2.3
148 148
149 149
150 150 changeset: 2:bc2d09796734
151 151 user: test
152 152 date: Thu Jan 01 00:00:00 1970 +0000
153 153 files: g
154 154 description:
155 155 2.2
156 156
157 157
158 158 changeset: 1:b1aa5cde7ff4
159 159 user: test
160 160 date: Thu Jan 01 00:00:00 1970 +0000
161 161 files: f
162 162 description:
163 163 2
164 164
165 165
166 166 changeset: 0:fcf06d5c4e1d
167 167 user: test
168 168 date: Thu Jan 01 00:00:00 1970 +0000
169 169 files: f
170 170 description:
171 171 1
172 172
173 173
174 174 $ hg clone . dupe
175 175 updating to branch default
176 176 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
177 177
178 178 $ for x in a b c d; do echo content > dupe/$x; done
179 179 $ hg -R dupe add
180 180 adding dupe/a (glob)
181 181 adding dupe/b (glob)
182 182 adding dupe/c (glob)
183 183 adding dupe/d (glob)
184 184 $ python unix2dos.py dupe/b dupe/c dupe/d
185 185 $ hg -R dupe ci -m a dupe/a
186 186 $ hg -R dupe ci -m b/c dupe/[bc]
187 187 $ hg -R dupe ci -m d dupe/d
188 188 $ hg -R dupe log -v
189 189 changeset: 8:67ac5962ab43
190 190 tag: tip
191 191 user: test
192 192 date: Thu Jan 01 00:00:00 1970 +0000
193 193 files: d
194 194 description:
195 195 d
196 196
197 197
198 198 changeset: 7:68c127d1834e
199 199 user: test
200 200 date: Thu Jan 01 00:00:00 1970 +0000
201 201 files: b c
202 202 description:
203 203 b/c
204 204
205 205
206 206 changeset: 6:adbf8bf7f31d
207 207 user: test
208 208 date: Thu Jan 01 00:00:00 1970 +0000
209 209 files: a
210 210 description:
211 211 a
212 212
213 213
214 214 changeset: 5:f0b1c8d75fce
215 215 user: test
216 216 date: Thu Jan 01 00:00:00 1970 +0000
217 217 files: bin
218 218 description:
219 219 5
220 220
221 221
222 222 changeset: 4:77796dbcd4ad
223 223 user: test
224 224 date: Thu Jan 01 00:00:00 1970 +0000
225 225 files: f
226 226 description:
227 227 4
228 228
229 229
230 230 changeset: 3:7c1b5430b350
231 231 user: test
232 232 date: Thu Jan 01 00:00:00 1970 +0000
233 233 files: f g
234 234 description:
235 235 2.3
236 236
237 237
238 238 changeset: 2:bc2d09796734
239 239 user: test
240 240 date: Thu Jan 01 00:00:00 1970 +0000
241 241 files: g
242 242 description:
243 243 2.2
244 244
245 245
246 246 changeset: 1:b1aa5cde7ff4
247 247 user: test
248 248 date: Thu Jan 01 00:00:00 1970 +0000
249 249 files: f
250 250 description:
251 251 2
252 252
253 253
254 254 changeset: 0:fcf06d5c4e1d
255 255 user: test
256 256 date: Thu Jan 01 00:00:00 1970 +0000
257 257 files: f
258 258 description:
259 259 1
260 260
261 261
262 262 $ hg pull dupe
263 263 pulling from dupe
264 264 searching for changes
265 265 adding changesets
266 266 adding manifests
267 267 adding file changes
268 268 added 3 changesets with 4 changes to 4 files
269 Attempt to commit or push text file(s) using CRLF line endings
269 attempt to commit or push text file(s) using CRLF line endings
270 270 in 67ac5962ab43: d
271 271 in 68c127d1834e: b
272 272 in 68c127d1834e: c
273 273
274 274 To prevent this mistake in your local repository,
275 275 add to Mercurial.ini or .hg/hgrc:
276 276
277 277 [hooks]
278 278 pretxncommit.crlf = python:hgext.win32text.forbidcrlf
279 279
280 280 and also consider adding:
281 281
282 282 [extensions]
283 283 win32text =
284 284 [encode]
285 285 ** = cleverencode:
286 286 [decode]
287 287 ** = cleverdecode:
288 288 transaction abort!
289 289 rollback completed
290 290 abort: pretxnchangegroup.crlf hook failed
291 291 [255]
292 292
293 293 $ hg log -v
294 294 changeset: 5:f0b1c8d75fce
295 295 tag: tip
296 296 user: test
297 297 date: Thu Jan 01 00:00:00 1970 +0000
298 298 files: bin
299 299 description:
300 300 5
301 301
302 302
303 303 changeset: 4:77796dbcd4ad
304 304 user: test
305 305 date: Thu Jan 01 00:00:00 1970 +0000
306 306 files: f
307 307 description:
308 308 4
309 309
310 310
311 311 changeset: 3:7c1b5430b350
312 312 user: test
313 313 date: Thu Jan 01 00:00:00 1970 +0000
314 314 files: f g
315 315 description:
316 316 2.3
317 317
318 318
319 319 changeset: 2:bc2d09796734
320 320 user: test
321 321 date: Thu Jan 01 00:00:00 1970 +0000
322 322 files: g
323 323 description:
324 324 2.2
325 325
326 326
327 327 changeset: 1:b1aa5cde7ff4
328 328 user: test
329 329 date: Thu Jan 01 00:00:00 1970 +0000
330 330 files: f
331 331 description:
332 332 2
333 333
334 334
335 335 changeset: 0:fcf06d5c4e1d
336 336 user: test
337 337 date: Thu Jan 01 00:00:00 1970 +0000
338 338 files: f
339 339 description:
340 340 1
341 341
342 342
343 343 $ rm .hg/hgrc
344 344 $ (echo some; echo text) > f3
345 345 $ python -c 'file("f4.bat", "wb").write("rem empty\x0D\x0A")'
346 346 $ hg add f3 f4.bat
347 347 $ hg ci -m 6
348 348 $ cat bin
349 349 hello\x00\r (esc)
350 350 $ cat f3
351 351 some
352 352 text
353 353 $ cat f4.bat
354 354 rem empty\r (esc)
355 355
356 356 $ echo '[extensions]' >> .hg/hgrc
357 357 $ echo 'win32text = ' >> .hg/hgrc
358 358 $ echo '[decode]' >> .hg/hgrc
359 359 $ echo '** = cleverdecode:' >> .hg/hgrc
360 360 $ echo '[encode]' >> .hg/hgrc
361 361 $ echo '** = cleverencode:' >> .hg/hgrc
362 362 $ cat .hg/hgrc
363 363 [extensions]
364 364 win32text =
365 365 [decode]
366 366 ** = cleverdecode:
367 367 [encode]
368 368 ** = cleverencode:
369 369
370 370 Trigger deprecation warning:
371 371
372 372 $ hg id -t
373 373 win32text is deprecated: http://mercurial.selenic.com/wiki/Win32TextExtension
374 374 tip
375 375
376 376 Disable warning:
377 377
378 378 $ echo '[win32text]' >> .hg/hgrc
379 379 $ echo 'warn = no' >> .hg/hgrc
380 380 $ hg id -t
381 381 tip
382 382
383 383 $ rm f3 f4.bat bin
384 384 $ hg co -C
385 385 WARNING: f4.bat already has CRLF line endings
386 386 and does not need EOL conversion by the win32text plugin.
387 387 Before your next commit, please reconsider your encode/decode settings in
388 388 Mercurial.ini or $TESTTMP/t/.hg/hgrc. (glob)
389 389 3 files updated, 0 files merged, 0 files removed, 0 files unresolved
390 390 $ cat bin
391 391 hello\x00\r (esc)
392 392 $ cat f3
393 393 some\r (esc)
394 394 text\r (esc)
395 395 $ cat f4.bat
396 396 rem empty\r (esc)
397 397
398 398 $ python -c 'file("f5.sh", "wb").write("# empty\x0D\x0A")'
399 399 $ hg add f5.sh
400 400 $ hg ci -m 7
401 401 $ cat f5.sh
402 402 # empty\r (esc)
403 403 $ hg cat f5.sh
404 404 # empty
405 405 $ echo '% just linefeed' > linefeed
406 406 $ hg ci -qAm 8 linefeed
407 407 $ cat linefeed
408 408 % just linefeed
409 409 $ hg cat linefeed
410 410 % just linefeed
411 411 $ hg st -q
412 412 $ hg revert -a linefeed
413 413 no changes needed to linefeed
414 414 $ cat linefeed
415 415 % just linefeed
416 416 $ hg st -q
417 417 $ echo modified >> linefeed
418 418 $ hg st -q
419 419 M linefeed
420 420 $ hg revert -a
421 421 reverting linefeed
422 422 $ hg st -q
423 423 $ cat linefeed
424 424 % just linefeed\r (esc)
425 425
426 426 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now