Debian Codegraph @

hgext/win32text.py

raw
1 # win32text.py - LF <-> CRLF/CR translation utilities for Windows/Mac users
2 #
3 # Copyright 2005, 2007-2009 Olivia Mackall <[email protected]> and others
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 '''perform automatic newline conversion (DEPRECATED)
9
10 Deprecation: The win32text extension requires each user to configure
11 the extension again and again for each clone since the configuration
12 is not copied when cloning.
13
14 We have therefore made the ``eol`` as an alternative. The ``eol``
15 uses a version controlled file for its configuration and each clone
16 will therefore use the right settings from the start.
17
18 To perform automatic newline conversion, use::
19
20 [extensions]
21 win32text =
22 [encode]
23 ** = cleverencode:
24 # or ** = macencode:
25
26 [decode]
27 ** = cleverdecode:
28 # or ** = macdecode:
29
30 If not doing conversion, to make sure you do not commit CRLF/CR by accident::
31
32 [hooks]
33 pretxncommit.crlf = python:hgext.win32text.forbidcrlf
34 # or pretxncommit.cr = python:hgext.win32text.forbidcr
35
36 To do the same check on a server to prevent CRLF/CR from being
37 pushed or pulled::
38
39 [hooks]
40 pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf
41 # or pretxnchangegroup.cr = python:hgext.win32text.forbidcr
42 '''
43
44 from __future__ import annotations
45
46 import re
47 from mercurial.i18n import _
48 from mercurial.node import short
49 from mercurial import (
50 cmdutil,
51 extensions,
52 registrar,
53 )
54 from mercurial.utils import stringutil
55
56 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
57 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
58 # be specifying the version(s) of Mercurial they are tested with, or
59 # leave the attribute unspecified.
60 testedwith = b'ships-with-hg-core'
61
62 configtable = {}
63 configitem = registrar.configitem(configtable)
64
65 configitem(
66 b'win32text',
67 b'warn',
68 default=True,
69 )
70
71 # regexp for single LF without CR preceding.
72 re_single_lf = re.compile(b'(^|[^\r])\n', re.MULTILINE)
73
74 newlinestr = {b'\r\n': b'CRLF', b'\r': b'CR'}
75 filterstr = {b'\r\n': b'clever', b'\r': b'mac'}
76
77
78 def checknewline(s, newline, ui=None, repo=None, filename=None):
79 # warn if already has 'newline' in repository.
80 # it might cause unexpected eol conversion.
81 # see issue 302:
82 # https://bz.mercurial-scm.org/302
83 if newline in s and ui and filename and repo:
84 ui.warn(
85 _(
86 b'WARNING: %s already has %s line endings\n'
87 b'and does not need EOL conversion by the win32text plugin.\n'
88 b'Before your next commit, please reconsider your '
89 b'encode/decode settings in \nMercurial.ini or %s.\n'
90 )
91 % (filename, newlinestr[newline], repo.vfs.join(b'hgrc'))
92 )
93
94
95 def dumbdecode(s, cmd, **kwargs):
96 checknewline(s, b'\r\n', **kwargs)
97 # replace single LF to CRLF
98 return re_single_lf.sub(b'\\1\r\n', s)
99
100
101 def dumbencode(s, cmd):
102 return s.replace(b'\r\n', b'\n')
103
104
105 def macdumbdecode(s, cmd, **kwargs):
106 checknewline(s, b'\r', **kwargs)
107 return s.replace(b'\n', b'\r')
108
109
110 def macdumbencode(s, cmd):
111 return s.replace(b'\r', b'\n')
112
113
114 def cleverdecode(s, cmd, **kwargs):
115 if not stringutil.binary(s):
116 return dumbdecode(s, cmd, **kwargs)
117 return s
118
119
120 def cleverencode(s, cmd):
121 if not stringutil.binary(s):
122 return dumbencode(s, cmd)
123 return s
124
125
126 def macdecode(s, cmd, **kwargs):
127 if not stringutil.binary(s):
128 return macdumbdecode(s, cmd, **kwargs)
129 return s
130
131
132 def macencode(s, cmd):
133 if not stringutil.binary(s):
134 return macdumbencode(s, cmd)
135 return s
136
137
138 _filters = {
139 b'dumbdecode:': dumbdecode,
140 b'dumbencode:': dumbencode,
141 b'cleverdecode:': cleverdecode,
142 b'cleverencode:': cleverencode,
143 b'macdumbdecode:': macdumbdecode,
144 b'macdumbencode:': macdumbencode,
145 b'macdecode:': macdecode,
146 b'macencode:': macencode,
147 }
148
149
150 def forbidnewline(ui, repo, hooktype, node, newline, **kwargs):
151 halt = False
152 seen = set()
153 # we try to walk changesets in reverse order from newest to
154 # oldest, so that if we see a file multiple times, we take the
155 # newest version as canonical. this prevents us from blocking a
156 # changegroup that contains an unacceptable commit followed later
157 # by a commit that fixes the problem.
158 tip = repo[b'tip']
159 for rev in range(repo.changelog.tiprev(), repo[node].rev() - 1, -1):
160 c = repo[rev]
161 for f in c.files():
162 if f in seen or f not in tip or f not in c:
163 continue
164 seen.add(f)
165 data = c[f].data()
166 if not stringutil.binary(data) and newline in data:
167 if not halt:
168 ui.warn(
169 _(
170 b'attempt to commit or push text file(s) '
171 b'using %s line endings\n'
172 )
173 % newlinestr[newline]
174 )
175 ui.warn(_(b'in %s: %s\n') % (short(c.node()), f))
176 halt = True
177 if halt and hooktype == b'pretxnchangegroup':
178 crlf = newlinestr[newline].lower()
179 filter = filterstr[newline]
180 ui.warn(
181 _(
182 b'\nTo prevent this mistake in your local repository,\n'
183 b'add to Mercurial.ini or .hg/hgrc:\n'
184 b'\n'
185 b'[hooks]\n'
186 b'pretxncommit.%s = python:hgext.win32text.forbid%s\n'
187 b'\n'
188 b'and also consider adding:\n'
189 b'\n'
190 b'[extensions]\n'
191 b'win32text =\n'
192 b'[encode]\n'
193 b'** = %sencode:\n'
194 b'[decode]\n'
195 b'** = %sdecode:\n'
196 )
197 % (crlf, crlf, filter, filter)
198 )
199 return halt
200
201
202 def forbidcrlf(ui, repo, hooktype, node, **kwargs):
203 return forbidnewline(ui, repo, hooktype, node, b'\r\n', **kwargs)
204
205
206 def forbidcr(ui, repo, hooktype, node, **kwargs):
207 return forbidnewline(ui, repo, hooktype, node, b'\r', **kwargs)
208
209
210 def reposetup(ui, repo):
211 if not repo.local():
212 return
213 for name, fn in _filters.items():
214 repo.adddatafilter(name, fn)
215
216
217 def wrap_revert(orig, repo, ctx, names, uipathfn, actions, *args, **kwargs):
218 # reset dirstate cache for file we touch
219 ds = repo.dirstate
220 for filename in actions[b'revert'][0]:
221 entry = ds.get_entry(filename)
222 if entry is not None:
223 if entry.p1_tracked:
224 # If we revert the file, it is possibly dirty. However,
225 # this extension meddle with the file content and therefore
226 # its size. As a result, we cannot simply call
227 # `dirstate.set_possibly_dirty` as it will not affet the
228 # expected size of the file.
229 #
230 # At least, now, the quirk is properly documented.
231 ds.hacky_extension_update_file(
232 filename,
233 entry.tracked,
234 p1_tracked=entry.p1_tracked,
235 p2_info=entry.p2_info,
236 )
237 return orig(repo, ctx, names, uipathfn, actions, *args, **kwargs)
238
239
240 def extsetup(ui):
241 # deprecated config: win32text.warn
242 if ui.configbool(b'win32text', b'warn'):
243 ui.warn(
244 _(
245 b"win32text is deprecated: "
246 b"https://mercurial-scm.org/wiki/Win32TextExtension\n"
247 )
248 )
249 extensions.wrapfunction(cmdutil, '_performrevert', wrap_revert)