##// END OF EJS Templates
win32text: give deprecation warning...
Steve Borho -
r12837:c82056f2 stable
parent child Browse files
Show More
@@ -1,166 +1,170 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 # regexp for single LF without CR preceding.
50 50 re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
51 51
52 52 newlinestr = {'\r\n': 'CRLF', '\r': 'CR'}
53 53 filterstr = {'\r\n': 'clever', '\r': 'mac'}
54 54
55 55 def checknewline(s, newline, ui=None, repo=None, filename=None):
56 56 # warn if already has 'newline' in repository.
57 57 # it might cause unexpected eol conversion.
58 58 # see issue 302:
59 59 # http://mercurial.selenic.com/bts/issue302
60 60 if newline in s and ui and filename and repo:
61 61 ui.warn(_('WARNING: %s already has %s line endings\n'
62 62 'and does not need EOL conversion by the win32text plugin.\n'
63 63 'Before your next commit, please reconsider your '
64 64 'encode/decode settings in \nMercurial.ini or %s.\n') %
65 65 (filename, newlinestr[newline], repo.join('hgrc')))
66 66
67 67 def dumbdecode(s, cmd, **kwargs):
68 68 checknewline(s, '\r\n', **kwargs)
69 69 # replace single LF to CRLF
70 70 return re_single_lf.sub('\\1\r\n', s)
71 71
72 72 def dumbencode(s, cmd):
73 73 return s.replace('\r\n', '\n')
74 74
75 75 def macdumbdecode(s, cmd, **kwargs):
76 76 checknewline(s, '\r', **kwargs)
77 77 return s.replace('\n', '\r')
78 78
79 79 def macdumbencode(s, cmd):
80 80 return s.replace('\r', '\n')
81 81
82 82 def cleverdecode(s, cmd, **kwargs):
83 83 if not util.binary(s):
84 84 return dumbdecode(s, cmd, **kwargs)
85 85 return s
86 86
87 87 def cleverencode(s, cmd):
88 88 if not util.binary(s):
89 89 return dumbencode(s, cmd)
90 90 return s
91 91
92 92 def macdecode(s, cmd, **kwargs):
93 93 if not util.binary(s):
94 94 return macdumbdecode(s, cmd, **kwargs)
95 95 return s
96 96
97 97 def macencode(s, cmd):
98 98 if not util.binary(s):
99 99 return macdumbencode(s, cmd)
100 100 return s
101 101
102 102 _filters = {
103 103 'dumbdecode:': dumbdecode,
104 104 'dumbencode:': dumbencode,
105 105 'cleverdecode:': cleverdecode,
106 106 'cleverencode:': cleverencode,
107 107 'macdumbdecode:': macdumbdecode,
108 108 'macdumbencode:': macdumbencode,
109 109 'macdecode:': macdecode,
110 110 'macencode:': macencode,
111 111 }
112 112
113 113 def forbidnewline(ui, repo, hooktype, node, newline, **kwargs):
114 114 halt = False
115 115 seen = set()
116 116 # we try to walk changesets in reverse order from newest to
117 117 # oldest, so that if we see a file multiple times, we take the
118 118 # newest version as canonical. this prevents us from blocking a
119 119 # changegroup that contains an unacceptable commit followed later
120 120 # by a commit that fixes the problem.
121 121 tip = repo['tip']
122 122 for rev in xrange(len(repo)-1, repo[node].rev()-1, -1):
123 123 c = repo[rev]
124 124 for f in c.files():
125 125 if f in seen or f not in tip or f not in c:
126 126 continue
127 127 seen.add(f)
128 128 data = c[f].data()
129 129 if not util.binary(data) and newline in data:
130 130 if not halt:
131 131 ui.warn(_('Attempt to commit or push text file(s) '
132 132 'using %s line endings\n') %
133 133 newlinestr[newline])
134 134 ui.warn(_('in %s: %s\n') % (short(c.node()), f))
135 135 halt = True
136 136 if halt and hooktype == 'pretxnchangegroup':
137 137 crlf = newlinestr[newline].lower()
138 138 filter = filterstr[newline]
139 139 ui.warn(_('\nTo prevent this mistake in your local repository,\n'
140 140 'add to Mercurial.ini or .hg/hgrc:\n'
141 141 '\n'
142 142 '[hooks]\n'
143 143 'pretxncommit.%s = python:hgext.win32text.forbid%s\n'
144 144 '\n'
145 145 'and also consider adding:\n'
146 146 '\n'
147 147 '[extensions]\n'
148 148 'win32text =\n'
149 149 '[encode]\n'
150 150 '** = %sencode:\n'
151 151 '[decode]\n'
152 152 '** = %sdecode:\n') % (crlf, crlf, filter, filter))
153 153 return halt
154 154
155 155 def forbidcrlf(ui, repo, hooktype, node, **kwargs):
156 156 return forbidnewline(ui, repo, hooktype, node, '\r\n', **kwargs)
157 157
158 158 def forbidcr(ui, repo, hooktype, node, **kwargs):
159 159 return forbidnewline(ui, repo, hooktype, node, '\r', **kwargs)
160 160
161 161 def reposetup(ui, repo):
162 162 if not repo.local():
163 163 return
164 164 for name, fn in _filters.iteritems():
165 165 repo.adddatafilter(name, fn)
166 166
167 def extsetup(ui):
168 if ui.configbool('win32text', 'warn', True):
169 ui.warn(_("win32text is deprecated: "
170 "http://mercurial.selenic.com/wiki/Win32TextExtension\n"))
General Comments 0
You need to be logged in to leave comments. Login now