Show More
@@ -1,191 +1,191 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 (DEPRECATED) |
|
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 __future__ import absolute_import |
|
45 | 45 | |
|
46 | 46 | import re |
|
47 | 47 | from mercurial.i18n import _ |
|
48 | 48 | from mercurial.node import ( |
|
49 | 49 | short, |
|
50 | 50 | ) |
|
51 | 51 | from mercurial import ( |
|
52 | 52 | registrar, |
|
53 | 53 | util, |
|
54 | 54 | ) |
|
55 | 55 | |
|
56 | 56 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
57 | 57 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
58 | 58 | # be specifying the version(s) of Mercurial they are tested with, or |
|
59 | 59 | # leave the attribute unspecified. |
|
60 | 60 | testedwith = 'ships-with-hg-core' |
|
61 | 61 | |
|
62 | 62 | configtable = {} |
|
63 | 63 | configitem = registrar.configitem(configtable) |
|
64 | 64 | |
|
65 | 65 | configitem('win32text', 'warn', |
|
66 | 66 | default=True, |
|
67 | 67 | ) |
|
68 | 68 | |
|
69 | 69 | # regexp for single LF without CR preceding. |
|
70 | 70 | re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE) |
|
71 | 71 | |
|
72 | 72 | newlinestr = {'\r\n': 'CRLF', '\r': 'CR'} |
|
73 | 73 | filterstr = {'\r\n': 'clever', '\r': 'mac'} |
|
74 | 74 | |
|
75 | 75 | def checknewline(s, newline, ui=None, repo=None, filename=None): |
|
76 | 76 | # warn if already has 'newline' in repository. |
|
77 | 77 | # it might cause unexpected eol conversion. |
|
78 | 78 | # see issue 302: |
|
79 | 79 | # https://bz.mercurial-scm.org/302 |
|
80 | 80 | if newline in s and ui and filename and repo: |
|
81 | 81 | ui.warn(_('WARNING: %s already has %s line endings\n' |
|
82 | 82 | 'and does not need EOL conversion by the win32text plugin.\n' |
|
83 | 83 | 'Before your next commit, please reconsider your ' |
|
84 | 84 | 'encode/decode settings in \nMercurial.ini or %s.\n') % |
|
85 | 85 | (filename, newlinestr[newline], repo.vfs.join('hgrc'))) |
|
86 | 86 | |
|
87 | 87 | def dumbdecode(s, cmd, **kwargs): |
|
88 | 88 | checknewline(s, '\r\n', **kwargs) |
|
89 | 89 | # replace single LF to CRLF |
|
90 | 90 | return re_single_lf.sub('\\1\r\n', s) |
|
91 | 91 | |
|
92 | 92 | def dumbencode(s, cmd): |
|
93 | 93 | return s.replace('\r\n', '\n') |
|
94 | 94 | |
|
95 | 95 | def macdumbdecode(s, cmd, **kwargs): |
|
96 | 96 | checknewline(s, '\r', **kwargs) |
|
97 | 97 | return s.replace('\n', '\r') |
|
98 | 98 | |
|
99 | 99 | def macdumbencode(s, cmd): |
|
100 | 100 | return s.replace('\r', '\n') |
|
101 | 101 | |
|
102 | 102 | def cleverdecode(s, cmd, **kwargs): |
|
103 | 103 | if not util.binary(s): |
|
104 | 104 | return dumbdecode(s, cmd, **kwargs) |
|
105 | 105 | return s |
|
106 | 106 | |
|
107 | 107 | def cleverencode(s, cmd): |
|
108 | 108 | if not util.binary(s): |
|
109 | 109 | return dumbencode(s, cmd) |
|
110 | 110 | return s |
|
111 | 111 | |
|
112 | 112 | def macdecode(s, cmd, **kwargs): |
|
113 | 113 | if not util.binary(s): |
|
114 | 114 | return macdumbdecode(s, cmd, **kwargs) |
|
115 | 115 | return s |
|
116 | 116 | |
|
117 | 117 | def macencode(s, cmd): |
|
118 | 118 | if not util.binary(s): |
|
119 | 119 | return macdumbencode(s, cmd) |
|
120 | 120 | return s |
|
121 | 121 | |
|
122 | 122 | _filters = { |
|
123 | 123 | 'dumbdecode:': dumbdecode, |
|
124 | 124 | 'dumbencode:': dumbencode, |
|
125 | 125 | 'cleverdecode:': cleverdecode, |
|
126 | 126 | 'cleverencode:': cleverencode, |
|
127 | 127 | 'macdumbdecode:': macdumbdecode, |
|
128 | 128 | 'macdumbencode:': macdumbencode, |
|
129 | 129 | 'macdecode:': macdecode, |
|
130 | 130 | 'macencode:': macencode, |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | def forbidnewline(ui, repo, hooktype, node, newline, **kwargs): |
|
134 | 134 | halt = False |
|
135 | 135 | seen = set() |
|
136 | 136 | # we try to walk changesets in reverse order from newest to |
|
137 | 137 | # oldest, so that if we see a file multiple times, we take the |
|
138 | 138 | # newest version as canonical. this prevents us from blocking a |
|
139 | 139 | # changegroup that contains an unacceptable commit followed later |
|
140 | 140 | # by a commit that fixes the problem. |
|
141 | 141 | tip = repo['tip'] |
|
142 |
for rev in xrange( |
|
|
142 | for rev in xrange(repo.changelog.tiprev(), repo[node].rev() - 1, -1): | |
|
143 | 143 | c = repo[rev] |
|
144 | 144 | for f in c.files(): |
|
145 | 145 | if f in seen or f not in tip or f not in c: |
|
146 | 146 | continue |
|
147 | 147 | seen.add(f) |
|
148 | 148 | data = c[f].data() |
|
149 | 149 | if not util.binary(data) and newline in data: |
|
150 | 150 | if not halt: |
|
151 | 151 | ui.warn(_('attempt to commit or push text file(s) ' |
|
152 | 152 | 'using %s line endings\n') % |
|
153 | 153 | newlinestr[newline]) |
|
154 | 154 | ui.warn(_('in %s: %s\n') % (short(c.node()), f)) |
|
155 | 155 | halt = True |
|
156 | 156 | if halt and hooktype == 'pretxnchangegroup': |
|
157 | 157 | crlf = newlinestr[newline].lower() |
|
158 | 158 | filter = filterstr[newline] |
|
159 | 159 | ui.warn(_('\nTo prevent this mistake in your local repository,\n' |
|
160 | 160 | 'add to Mercurial.ini or .hg/hgrc:\n' |
|
161 | 161 | '\n' |
|
162 | 162 | '[hooks]\n' |
|
163 | 163 | 'pretxncommit.%s = python:hgext.win32text.forbid%s\n' |
|
164 | 164 | '\n' |
|
165 | 165 | 'and also consider adding:\n' |
|
166 | 166 | '\n' |
|
167 | 167 | '[extensions]\n' |
|
168 | 168 | 'win32text =\n' |
|
169 | 169 | '[encode]\n' |
|
170 | 170 | '** = %sencode:\n' |
|
171 | 171 | '[decode]\n' |
|
172 | 172 | '** = %sdecode:\n') % (crlf, crlf, filter, filter)) |
|
173 | 173 | return halt |
|
174 | 174 | |
|
175 | 175 | def forbidcrlf(ui, repo, hooktype, node, **kwargs): |
|
176 | 176 | return forbidnewline(ui, repo, hooktype, node, '\r\n', **kwargs) |
|
177 | 177 | |
|
178 | 178 | def forbidcr(ui, repo, hooktype, node, **kwargs): |
|
179 | 179 | return forbidnewline(ui, repo, hooktype, node, '\r', **kwargs) |
|
180 | 180 | |
|
181 | 181 | def reposetup(ui, repo): |
|
182 | 182 | if not repo.local(): |
|
183 | 183 | return |
|
184 | 184 | for name, fn in _filters.iteritems(): |
|
185 | 185 | repo.adddatafilter(name, fn) |
|
186 | 186 | |
|
187 | 187 | def extsetup(ui): |
|
188 | 188 | # deprecated config: win32text.warn |
|
189 | 189 | if ui.configbool('win32text', 'warn'): |
|
190 | 190 | ui.warn(_("win32text is deprecated: " |
|
191 | 191 | "https://mercurial-scm.org/wiki/Win32TextExtension\n")) |
General Comments 0
You need to be logged in to leave comments.
Login now