annotate hgext/win32text.py @ 7037:e3f9feb9ab7e

update: mention null revision in help text
author Martin Geisler <mg@daimi.au.dk>
date Wed, 17 Sep 2008 10:24:30 +0200
parents 9c897ffd3637
children ab8038bf5127
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
1 # win32text.py - LF <-> CRLF translation utilities for Windows users
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
2 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
3 # This software may be used and distributed according to the terms
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
4 # of the GNU General Public License, incorporated herein by reference.
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
5 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
6 # To perform automatic newline conversion, use:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
7 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
8 # [extensions]
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
9 # hgext.win32text =
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
10 # [encode]
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
11 # ** = cleverencode:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
12 # [decode]
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
13 # ** = cleverdecode:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
14 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
15 # If not doing conversion, to make sure you do not commit CRLF by accident:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
16 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
17 # [hooks]
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
18 # pretxncommit.crlf = python:hgext.win32text.forbidcrlf
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
19 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
20 # To do the same check on a server to prevent CRLF from being pushed or pulled:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
21 #
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
22 # [hooks]
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
23 # pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
24
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
25 from mercurial.i18n import gettext as _
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 5967
diff changeset
26 from mercurial.node import bin, short
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
27 import re
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
28
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
29 # regexp for single LF without CR preceding.
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
30 re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
31
5967
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
32 def dumbdecode(s, cmd, ui=None, repo=None, filename=None, **kwargs):
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
33 # warn if already has CRLF in repository.
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
34 # it might cause unexpected eol conversion.
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
35 # see issue 302:
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
36 # http://www.selenic.com/mercurial/bts/issue302
5967
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
37 if '\r\n' in s and ui and filename and repo:
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
38 ui.warn(_('WARNING: %s already has CRLF line endings\n'
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
39 'and does not need EOL conversion by the win32text plugin.\n'
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
40 'Before your next commit, please reconsider your '
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
41 'encode/decode settings in \nMercurial.ini or %s.\n') %
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
42 (filename, repo.join('hgrc')))
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
43 # replace single LF to CRLF
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
44 return re_single_lf.sub('\\1\r\n', s)
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
45
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
46 def dumbencode(s, cmd):
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
47 return s.replace('\r\n', '\n')
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
48
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
49 def clevertest(s, cmd):
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
50 if '\0' in s: return False
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
51 return True
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
52
5967
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
53 def cleverdecode(s, cmd, **kwargs):
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
54 if clevertest(s, cmd):
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
55 return dumbdecode(s, cmd, **kwargs)
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
56 return s
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
57
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
58 def cleverencode(s, cmd):
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
59 if clevertest(s, cmd):
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
60 return dumbencode(s, cmd)
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
61 return s
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
62
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
63 _filters = {
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
64 'dumbdecode:': dumbdecode,
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
65 'dumbencode:': dumbencode,
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
66 'cleverdecode:': cleverdecode,
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
67 'cleverencode:': cleverencode,
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
68 }
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
69
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
70 def forbidcrlf(ui, repo, hooktype, node, **kwargs):
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
71 halt = False
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
72 for rev in xrange(repo.changelog.rev(bin(node)), repo.changelog.count()):
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
73 c = repo.changectx(rev)
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
74 for f in c.files():
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
75 if f not in c:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
76 continue
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
77 data = c[f].data()
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
78 if '\0' not in data and '\r\n' in data:
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
79 if not halt:
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
80 ui.warn(_('Attempt to commit or push text file(s) '
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
81 'using CRLF line endings\n'))
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
82 ui.warn(_('in %s: %s\n') % (short(c.node()), f))
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
83 halt = True
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
84 if halt and hooktype == 'pretxnchangegroup':
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
85 ui.warn(_('\nTo prevent this mistake in your local repository,\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
86 'add to Mercurial.ini or .hg/hgrc:\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
87 '\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
88 '[hooks]\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
89 'pretxncommit.crlf = python:hgext.win32text.forbidcrlf\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
90 '\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
91 'and also consider adding:\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
92 '\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
93 '[extensions]\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
94 'hgext.win32text =\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
95 '[encode]\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
96 '** = cleverencode:\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
97 '[decode]\n'
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
98 '** = cleverdecode:\n'))
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
99 return halt
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
100
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
101 def reposetup(ui, repo):
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
102 if not repo.local():
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
103 return
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
104 for name, fn in _filters.iteritems():
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
105 repo.adddatafilter(name, fn)
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
106