##// END OF EJS Templates
parselist: move the function from config to stringutil...
marmoute -
r47960:b0e92313 default
parent child Browse files
Show More
@@ -1,350 +1,260 b''
1 # config.py - configuration parsing for Mercurial
1 # config.py - configuration parsing for Mercurial
2 #
2 #
3 # Copyright 2009 Olivia Mackall <olivia@selenic.com> and others
3 # Copyright 2009 Olivia Mackall <olivia@selenic.com> and others
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import os
11 import os
12
12
13 from .i18n import _
13 from .i18n import _
14 from .pycompat import getattr
14 from .pycompat import getattr
15 from . import (
15 from . import (
16 encoding,
16 encoding,
17 error,
17 error,
18 pycompat,
18 pycompat,
19 util,
19 util,
20 )
20 )
21
21
22
22
23 class config(object):
23 class config(object):
24 def __init__(self, data=None):
24 def __init__(self, data=None):
25 self._current_source_level = 0
25 self._current_source_level = 0
26 self._data = {}
26 self._data = {}
27 self._unset = []
27 self._unset = []
28 if data:
28 if data:
29 for k in data._data:
29 for k in data._data:
30 self._data[k] = data[k].copy()
30 self._data[k] = data[k].copy()
31 self._current_source_level = data._current_source_level + 1
31 self._current_source_level = data._current_source_level + 1
32
32
33 def new_source(self):
33 def new_source(self):
34 """increment the source counter
34 """increment the source counter
35
35
36 This is used to define source priority when reading"""
36 This is used to define source priority when reading"""
37 self._current_source_level += 1
37 self._current_source_level += 1
38
38
39 def copy(self):
39 def copy(self):
40 return config(self)
40 return config(self)
41
41
42 def __contains__(self, section):
42 def __contains__(self, section):
43 return section in self._data
43 return section in self._data
44
44
45 def hasitem(self, section, item):
45 def hasitem(self, section, item):
46 return item in self._data.get(section, {})
46 return item in self._data.get(section, {})
47
47
48 def __getitem__(self, section):
48 def __getitem__(self, section):
49 return self._data.get(section, {})
49 return self._data.get(section, {})
50
50
51 def __iter__(self):
51 def __iter__(self):
52 for d in self.sections():
52 for d in self.sections():
53 yield d
53 yield d
54
54
55 def update(self, src):
55 def update(self, src):
56 current_level = self._current_source_level
56 current_level = self._current_source_level
57 current_level += 1
57 current_level += 1
58 max_level = self._current_source_level
58 max_level = self._current_source_level
59 for s, n in src._unset:
59 for s, n in src._unset:
60 ds = self._data.get(s, None)
60 ds = self._data.get(s, None)
61 if ds is not None and n in ds:
61 if ds is not None and n in ds:
62 self._data[s] = ds.preparewrite()
62 self._data[s] = ds.preparewrite()
63 del self._data[s][n]
63 del self._data[s][n]
64 for s in src:
64 for s in src:
65 ds = self._data.get(s, None)
65 ds = self._data.get(s, None)
66 if ds:
66 if ds:
67 self._data[s] = ds.preparewrite()
67 self._data[s] = ds.preparewrite()
68 else:
68 else:
69 self._data[s] = util.cowsortdict()
69 self._data[s] = util.cowsortdict()
70 for k, v in src._data[s].items():
70 for k, v in src._data[s].items():
71 value, source, level = v
71 value, source, level = v
72 level += current_level
72 level += current_level
73 max_level = max(level, current_level)
73 max_level = max(level, current_level)
74 self._data[s][k] = (value, source, level)
74 self._data[s][k] = (value, source, level)
75 self._current_source_level = max_level
75 self._current_source_level = max_level
76
76
77 def _get(self, section, item):
77 def _get(self, section, item):
78 return self._data.get(section, {}).get(item)
78 return self._data.get(section, {}).get(item)
79
79
80 def get(self, section, item, default=None):
80 def get(self, section, item, default=None):
81 result = self._get(section, item)
81 result = self._get(section, item)
82 if result is None:
82 if result is None:
83 return default
83 return default
84 return result[0]
84 return result[0]
85
85
86 def backup(self, section, key):
86 def backup(self, section, key):
87 """return a tuple allowing restore to reinstall a previous value
87 """return a tuple allowing restore to reinstall a previous value
88
88
89 The main reason we need it is because it handles the "no data" case.
89 The main reason we need it is because it handles the "no data" case.
90 """
90 """
91 try:
91 try:
92 item = self._data[section][key]
92 item = self._data[section][key]
93 except KeyError:
93 except KeyError:
94 return (section, key)
94 return (section, key)
95 else:
95 else:
96 return (section, key) + item
96 return (section, key) + item
97
97
98 def source(self, section, item):
98 def source(self, section, item):
99 result = self._get(section, item)
99 result = self._get(section, item)
100 if result is None:
100 if result is None:
101 return b""
101 return b""
102 return result[1]
102 return result[1]
103
103
104 def level(self, section, item):
104 def level(self, section, item):
105 result = self._get(section, item)
105 result = self._get(section, item)
106 if result is None:
106 if result is None:
107 return None
107 return None
108 return result[2]
108 return result[2]
109
109
110 def sections(self):
110 def sections(self):
111 return sorted(self._data.keys())
111 return sorted(self._data.keys())
112
112
113 def items(self, section):
113 def items(self, section):
114 items = pycompat.iteritems(self._data.get(section, {}))
114 items = pycompat.iteritems(self._data.get(section, {}))
115 return [(k, v[0]) for (k, v) in items]
115 return [(k, v[0]) for (k, v) in items]
116
116
117 def set(self, section, item, value, source=b""):
117 def set(self, section, item, value, source=b""):
118 if pycompat.ispy3:
118 if pycompat.ispy3:
119 assert not isinstance(
119 assert not isinstance(
120 section, str
120 section, str
121 ), b'config section may not be unicode strings on Python 3'
121 ), b'config section may not be unicode strings on Python 3'
122 assert not isinstance(
122 assert not isinstance(
123 item, str
123 item, str
124 ), b'config item may not be unicode strings on Python 3'
124 ), b'config item may not be unicode strings on Python 3'
125 assert not isinstance(
125 assert not isinstance(
126 value, str
126 value, str
127 ), b'config values may not be unicode strings on Python 3'
127 ), b'config values may not be unicode strings on Python 3'
128 if section not in self:
128 if section not in self:
129 self._data[section] = util.cowsortdict()
129 self._data[section] = util.cowsortdict()
130 else:
130 else:
131 self._data[section] = self._data[section].preparewrite()
131 self._data[section] = self._data[section].preparewrite()
132 self._data[section][item] = (value, source, self._current_source_level)
132 self._data[section][item] = (value, source, self._current_source_level)
133
133
134 def alter(self, section, key, new_value):
134 def alter(self, section, key, new_value):
135 """alter a value without altering its source or level
135 """alter a value without altering its source or level
136
136
137 This method is meant to be used by `ui.fixconfig` only."""
137 This method is meant to be used by `ui.fixconfig` only."""
138 item = self._data[section][key]
138 item = self._data[section][key]
139 size = len(item)
139 size = len(item)
140 new_item = (new_value,) + item[1:]
140 new_item = (new_value,) + item[1:]
141 assert len(new_item) == size
141 assert len(new_item) == size
142 self._data[section][key] = new_item
142 self._data[section][key] = new_item
143
143
144 def restore(self, data):
144 def restore(self, data):
145 """restore data returned by self.backup"""
145 """restore data returned by self.backup"""
146 if len(data) != 2:
146 if len(data) != 2:
147 # restore old data
147 # restore old data
148 section, key = data[:2]
148 section, key = data[:2]
149 item = data[2:]
149 item = data[2:]
150 self._data[section] = self._data[section].preparewrite()
150 self._data[section] = self._data[section].preparewrite()
151 self._data[section][key] = item
151 self._data[section][key] = item
152 else:
152 else:
153 # no data before, remove everything
153 # no data before, remove everything
154 section, item = data
154 section, item = data
155 if section in self._data:
155 if section in self._data:
156 self._data[section].pop(item, None)
156 self._data[section].pop(item, None)
157
157
158 def parse(self, src, data, sections=None, remap=None, include=None):
158 def parse(self, src, data, sections=None, remap=None, include=None):
159 sectionre = util.re.compile(br'\[([^\[]+)\]')
159 sectionre = util.re.compile(br'\[([^\[]+)\]')
160 itemre = util.re.compile(br'([^=\s][^=]*?)\s*=\s*(.*\S|)')
160 itemre = util.re.compile(br'([^=\s][^=]*?)\s*=\s*(.*\S|)')
161 contre = util.re.compile(br'\s+(\S|\S.*\S)\s*$')
161 contre = util.re.compile(br'\s+(\S|\S.*\S)\s*$')
162 emptyre = util.re.compile(br'(;|#|\s*$)')
162 emptyre = util.re.compile(br'(;|#|\s*$)')
163 commentre = util.re.compile(br'(;|#)')
163 commentre = util.re.compile(br'(;|#)')
164 unsetre = util.re.compile(br'%unset\s+(\S+)')
164 unsetre = util.re.compile(br'%unset\s+(\S+)')
165 includere = util.re.compile(br'%include\s+(\S|\S.*\S)\s*$')
165 includere = util.re.compile(br'%include\s+(\S|\S.*\S)\s*$')
166 section = b""
166 section = b""
167 item = None
167 item = None
168 line = 0
168 line = 0
169 cont = False
169 cont = False
170
170
171 if remap:
171 if remap:
172 section = remap.get(section, section)
172 section = remap.get(section, section)
173
173
174 for l in data.splitlines(True):
174 for l in data.splitlines(True):
175 line += 1
175 line += 1
176 if line == 1 and l.startswith(b'\xef\xbb\xbf'):
176 if line == 1 and l.startswith(b'\xef\xbb\xbf'):
177 # Someone set us up the BOM
177 # Someone set us up the BOM
178 l = l[3:]
178 l = l[3:]
179 if cont:
179 if cont:
180 if commentre.match(l):
180 if commentre.match(l):
181 continue
181 continue
182 m = contre.match(l)
182 m = contre.match(l)
183 if m:
183 if m:
184 if sections and section not in sections:
184 if sections and section not in sections:
185 continue
185 continue
186 v = self.get(section, item) + b"\n" + m.group(1)
186 v = self.get(section, item) + b"\n" + m.group(1)
187 self.set(section, item, v, b"%s:%d" % (src, line))
187 self.set(section, item, v, b"%s:%d" % (src, line))
188 continue
188 continue
189 item = None
189 item = None
190 cont = False
190 cont = False
191 m = includere.match(l)
191 m = includere.match(l)
192
192
193 if m and include:
193 if m and include:
194 expanded = util.expandpath(m.group(1))
194 expanded = util.expandpath(m.group(1))
195 try:
195 try:
196 include(expanded, remap=remap, sections=sections)
196 include(expanded, remap=remap, sections=sections)
197 except IOError as inst:
197 except IOError as inst:
198 if inst.errno != errno.ENOENT:
198 if inst.errno != errno.ENOENT:
199 raise error.ConfigError(
199 raise error.ConfigError(
200 _(b"cannot include %s (%s)")
200 _(b"cannot include %s (%s)")
201 % (expanded, encoding.strtolocal(inst.strerror)),
201 % (expanded, encoding.strtolocal(inst.strerror)),
202 b"%s:%d" % (src, line),
202 b"%s:%d" % (src, line),
203 )
203 )
204 continue
204 continue
205 if emptyre.match(l):
205 if emptyre.match(l):
206 continue
206 continue
207 m = sectionre.match(l)
207 m = sectionre.match(l)
208 if m:
208 if m:
209 section = m.group(1)
209 section = m.group(1)
210 if remap:
210 if remap:
211 section = remap.get(section, section)
211 section = remap.get(section, section)
212 if section not in self:
212 if section not in self:
213 self._data[section] = util.cowsortdict()
213 self._data[section] = util.cowsortdict()
214 continue
214 continue
215 m = itemre.match(l)
215 m = itemre.match(l)
216 if m:
216 if m:
217 item = m.group(1)
217 item = m.group(1)
218 cont = True
218 cont = True
219 if sections and section not in sections:
219 if sections and section not in sections:
220 continue
220 continue
221 self.set(section, item, m.group(2), b"%s:%d" % (src, line))
221 self.set(section, item, m.group(2), b"%s:%d" % (src, line))
222 continue
222 continue
223 m = unsetre.match(l)
223 m = unsetre.match(l)
224 if m:
224 if m:
225 name = m.group(1)
225 name = m.group(1)
226 if sections and section not in sections:
226 if sections and section not in sections:
227 continue
227 continue
228 if self.get(section, name) is not None:
228 if self.get(section, name) is not None:
229 self._data[section] = self._data[section].preparewrite()
229 self._data[section] = self._data[section].preparewrite()
230 del self._data[section][name]
230 del self._data[section][name]
231 self._unset.append((section, name))
231 self._unset.append((section, name))
232 continue
232 continue
233
233
234 message = l.rstrip()
234 message = l.rstrip()
235 if l.startswith(b' '):
235 if l.startswith(b' '):
236 message = b"unexpected leading whitespace: %s" % message
236 message = b"unexpected leading whitespace: %s" % message
237 raise error.ConfigError(message, (b"%s:%d" % (src, line)))
237 raise error.ConfigError(message, (b"%s:%d" % (src, line)))
238
238
239 def read(self, path, fp=None, sections=None, remap=None):
239 def read(self, path, fp=None, sections=None, remap=None):
240 self.new_source()
240 self.new_source()
241 if not fp:
241 if not fp:
242 fp = util.posixfile(path, b'rb')
242 fp = util.posixfile(path, b'rb')
243 assert (
243 assert (
244 getattr(fp, 'mode', 'rb') == 'rb'
244 getattr(fp, 'mode', 'rb') == 'rb'
245 ), b'config files must be opened in binary mode, got fp=%r mode=%r' % (
245 ), b'config files must be opened in binary mode, got fp=%r mode=%r' % (
246 fp,
246 fp,
247 fp.mode,
247 fp.mode,
248 )
248 )
249
249
250 dir = os.path.dirname(path)
250 dir = os.path.dirname(path)
251
251
252 def include(rel, remap, sections):
252 def include(rel, remap, sections):
253 abs = os.path.normpath(os.path.join(dir, rel))
253 abs = os.path.normpath(os.path.join(dir, rel))
254 self.read(abs, remap=remap, sections=sections)
254 self.read(abs, remap=remap, sections=sections)
255 # anything after the include has a higher level
255 # anything after the include has a higher level
256 self.new_source()
256 self.new_source()
257
257
258 self.parse(
258 self.parse(
259 path, fp.read(), sections=sections, remap=remap, include=include
259 path, fp.read(), sections=sections, remap=remap, include=include
260 )
260 )
261
262
263 def parselist(value):
264 """parse a configuration value as a list of comma/space separated strings
265
266 >>> parselist(b'this,is "a small" ,test')
267 ['this', 'is', 'a small', 'test']
268 """
269
270 def _parse_plain(parts, s, offset):
271 whitespace = False
272 while offset < len(s) and (
273 s[offset : offset + 1].isspace() or s[offset : offset + 1] == b','
274 ):
275 whitespace = True
276 offset += 1
277 if offset >= len(s):
278 return None, parts, offset
279 if whitespace:
280 parts.append(b'')
281 if s[offset : offset + 1] == b'"' and not parts[-1]:
282 return _parse_quote, parts, offset + 1
283 elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\':
284 parts[-1] = parts[-1][:-1] + s[offset : offset + 1]
285 return _parse_plain, parts, offset + 1
286 parts[-1] += s[offset : offset + 1]
287 return _parse_plain, parts, offset + 1
288
289 def _parse_quote(parts, s, offset):
290 if offset < len(s) and s[offset : offset + 1] == b'"': # ""
291 parts.append(b'')
292 offset += 1
293 while offset < len(s) and (
294 s[offset : offset + 1].isspace()
295 or s[offset : offset + 1] == b','
296 ):
297 offset += 1
298 return _parse_plain, parts, offset
299
300 while offset < len(s) and s[offset : offset + 1] != b'"':
301 if (
302 s[offset : offset + 1] == b'\\'
303 and offset + 1 < len(s)
304 and s[offset + 1 : offset + 2] == b'"'
305 ):
306 offset += 1
307 parts[-1] += b'"'
308 else:
309 parts[-1] += s[offset : offset + 1]
310 offset += 1
311
312 if offset >= len(s):
313 real_parts = _configlist(parts[-1])
314 if not real_parts:
315 parts[-1] = b'"'
316 else:
317 real_parts[0] = b'"' + real_parts[0]
318 parts = parts[:-1]
319 parts.extend(real_parts)
320 return None, parts, offset
321
322 offset += 1
323 while offset < len(s) and s[offset : offset + 1] in [b' ', b',']:
324 offset += 1
325
326 if offset < len(s):
327 if offset + 1 == len(s) and s[offset : offset + 1] == b'"':
328 parts[-1] += b'"'
329 offset += 1
330 else:
331 parts.append(b'')
332 else:
333 return None, parts, offset
334
335 return _parse_plain, parts, offset
336
337 def _configlist(s):
338 s = s.rstrip(b' ,')
339 if not s:
340 return []
341 parser, parts, offset = _parse_plain, [b''], 0
342 while parser:
343 parser, parts, offset = parser(parts, s, offset)
344 return parts
345
346 if value is not None and isinstance(value, bytes):
347 result = _configlist(value.lstrip(b' ,\n'))
348 else:
349 result = value
350 return result or []
@@ -1,2230 +1,2230 b''
1 # ui.py - user interface bits for mercurial
1 # ui.py - user interface bits for mercurial
2 #
2 #
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import collections
10 import collections
11 import contextlib
11 import contextlib
12 import datetime
12 import datetime
13 import errno
13 import errno
14 import getpass
14 import getpass
15 import inspect
15 import inspect
16 import os
16 import os
17 import re
17 import re
18 import signal
18 import signal
19 import socket
19 import socket
20 import subprocess
20 import subprocess
21 import sys
21 import sys
22 import traceback
22 import traceback
23
23
24 from .i18n import _
24 from .i18n import _
25 from .node import hex
25 from .node import hex
26 from .pycompat import (
26 from .pycompat import (
27 getattr,
27 getattr,
28 open,
28 open,
29 )
29 )
30
30
31 from . import (
31 from . import (
32 color,
32 color,
33 config,
33 config,
34 configitems,
34 configitems,
35 encoding,
35 encoding,
36 error,
36 error,
37 formatter,
37 formatter,
38 loggingutil,
38 loggingutil,
39 progress,
39 progress,
40 pycompat,
40 pycompat,
41 rcutil,
41 rcutil,
42 scmutil,
42 scmutil,
43 util,
43 util,
44 )
44 )
45 from .utils import (
45 from .utils import (
46 dateutil,
46 dateutil,
47 procutil,
47 procutil,
48 resourceutil,
48 resourceutil,
49 stringutil,
49 stringutil,
50 urlutil,
50 urlutil,
51 )
51 )
52
52
53 urlreq = util.urlreq
53 urlreq = util.urlreq
54
54
55 # for use with str.translate(None, _keepalnum), to keep just alphanumerics
55 # for use with str.translate(None, _keepalnum), to keep just alphanumerics
56 _keepalnum = b''.join(
56 _keepalnum = b''.join(
57 c for c in map(pycompat.bytechr, range(256)) if not c.isalnum()
57 c for c in map(pycompat.bytechr, range(256)) if not c.isalnum()
58 )
58 )
59
59
60 # The config knobs that will be altered (if unset) by ui.tweakdefaults.
60 # The config knobs that will be altered (if unset) by ui.tweakdefaults.
61 tweakrc = b"""
61 tweakrc = b"""
62 [ui]
62 [ui]
63 # The rollback command is dangerous. As a rule, don't use it.
63 # The rollback command is dangerous. As a rule, don't use it.
64 rollback = False
64 rollback = False
65 # Make `hg status` report copy information
65 # Make `hg status` report copy information
66 statuscopies = yes
66 statuscopies = yes
67 # Prefer curses UIs when available. Revert to plain-text with `text`.
67 # Prefer curses UIs when available. Revert to plain-text with `text`.
68 interface = curses
68 interface = curses
69 # Make compatible commands emit cwd-relative paths by default.
69 # Make compatible commands emit cwd-relative paths by default.
70 relative-paths = yes
70 relative-paths = yes
71
71
72 [commands]
72 [commands]
73 # Grep working directory by default.
73 # Grep working directory by default.
74 grep.all-files = True
74 grep.all-files = True
75 # Refuse to perform an `hg update` that would cause a file content merge
75 # Refuse to perform an `hg update` that would cause a file content merge
76 update.check = noconflict
76 update.check = noconflict
77 # Show conflicts information in `hg status`
77 # Show conflicts information in `hg status`
78 status.verbose = True
78 status.verbose = True
79 # Make `hg resolve` with no action (like `-m`) fail instead of re-merging.
79 # Make `hg resolve` with no action (like `-m`) fail instead of re-merging.
80 resolve.explicit-re-merge = True
80 resolve.explicit-re-merge = True
81
81
82 [diff]
82 [diff]
83 git = 1
83 git = 1
84 showfunc = 1
84 showfunc = 1
85 word-diff = 1
85 word-diff = 1
86 """
86 """
87
87
88 samplehgrcs = {
88 samplehgrcs = {
89 b'user': b"""# example user config (see 'hg help config' for more info)
89 b'user': b"""# example user config (see 'hg help config' for more info)
90 [ui]
90 [ui]
91 # name and email, e.g.
91 # name and email, e.g.
92 # username = Jane Doe <jdoe@example.com>
92 # username = Jane Doe <jdoe@example.com>
93 username =
93 username =
94
94
95 # We recommend enabling tweakdefaults to get slight improvements to
95 # We recommend enabling tweakdefaults to get slight improvements to
96 # the UI over time. Make sure to set HGPLAIN in the environment when
96 # the UI over time. Make sure to set HGPLAIN in the environment when
97 # writing scripts!
97 # writing scripts!
98 # tweakdefaults = True
98 # tweakdefaults = True
99
99
100 # uncomment to disable color in command output
100 # uncomment to disable color in command output
101 # (see 'hg help color' for details)
101 # (see 'hg help color' for details)
102 # color = never
102 # color = never
103
103
104 # uncomment to disable command output pagination
104 # uncomment to disable command output pagination
105 # (see 'hg help pager' for details)
105 # (see 'hg help pager' for details)
106 # paginate = never
106 # paginate = never
107
107
108 [extensions]
108 [extensions]
109 # uncomment the lines below to enable some popular extensions
109 # uncomment the lines below to enable some popular extensions
110 # (see 'hg help extensions' for more info)
110 # (see 'hg help extensions' for more info)
111 #
111 #
112 # histedit =
112 # histedit =
113 # rebase =
113 # rebase =
114 # uncommit =
114 # uncommit =
115 """,
115 """,
116 b'cloned': b"""# example repository config (see 'hg help config' for more info)
116 b'cloned': b"""# example repository config (see 'hg help config' for more info)
117 [paths]
117 [paths]
118 default = %s
118 default = %s
119
119
120 # path aliases to other clones of this repo in URLs or filesystem paths
120 # path aliases to other clones of this repo in URLs or filesystem paths
121 # (see 'hg help config.paths' for more info)
121 # (see 'hg help config.paths' for more info)
122 #
122 #
123 # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork
123 # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork
124 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
124 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
125 # my-clone = /home/jdoe/jdoes-clone
125 # my-clone = /home/jdoe/jdoes-clone
126
126
127 [ui]
127 [ui]
128 # name and email (local to this repository, optional), e.g.
128 # name and email (local to this repository, optional), e.g.
129 # username = Jane Doe <jdoe@example.com>
129 # username = Jane Doe <jdoe@example.com>
130 """,
130 """,
131 b'local': b"""# example repository config (see 'hg help config' for more info)
131 b'local': b"""# example repository config (see 'hg help config' for more info)
132 [paths]
132 [paths]
133 # path aliases to other clones of this repo in URLs or filesystem paths
133 # path aliases to other clones of this repo in URLs or filesystem paths
134 # (see 'hg help config.paths' for more info)
134 # (see 'hg help config.paths' for more info)
135 #
135 #
136 # default = http://example.com/hg/example-repo
136 # default = http://example.com/hg/example-repo
137 # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork
137 # default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork
138 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
138 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
139 # my-clone = /home/jdoe/jdoes-clone
139 # my-clone = /home/jdoe/jdoes-clone
140
140
141 [ui]
141 [ui]
142 # name and email (local to this repository, optional), e.g.
142 # name and email (local to this repository, optional), e.g.
143 # username = Jane Doe <jdoe@example.com>
143 # username = Jane Doe <jdoe@example.com>
144 """,
144 """,
145 b'global': b"""# example system-wide hg config (see 'hg help config' for more info)
145 b'global': b"""# example system-wide hg config (see 'hg help config' for more info)
146
146
147 [ui]
147 [ui]
148 # uncomment to disable color in command output
148 # uncomment to disable color in command output
149 # (see 'hg help color' for details)
149 # (see 'hg help color' for details)
150 # color = never
150 # color = never
151
151
152 # uncomment to disable command output pagination
152 # uncomment to disable command output pagination
153 # (see 'hg help pager' for details)
153 # (see 'hg help pager' for details)
154 # paginate = never
154 # paginate = never
155
155
156 [extensions]
156 [extensions]
157 # uncomment the lines below to enable some popular extensions
157 # uncomment the lines below to enable some popular extensions
158 # (see 'hg help extensions' for more info)
158 # (see 'hg help extensions' for more info)
159 #
159 #
160 # blackbox =
160 # blackbox =
161 # churn =
161 # churn =
162 """,
162 """,
163 }
163 }
164
164
165
165
166 def _maybestrurl(maybebytes):
166 def _maybestrurl(maybebytes):
167 return pycompat.rapply(pycompat.strurl, maybebytes)
167 return pycompat.rapply(pycompat.strurl, maybebytes)
168
168
169
169
170 def _maybebytesurl(maybestr):
170 def _maybebytesurl(maybestr):
171 return pycompat.rapply(pycompat.bytesurl, maybestr)
171 return pycompat.rapply(pycompat.bytesurl, maybestr)
172
172
173
173
174 class httppasswordmgrdbproxy(object):
174 class httppasswordmgrdbproxy(object):
175 """Delays loading urllib2 until it's needed."""
175 """Delays loading urllib2 until it's needed."""
176
176
177 def __init__(self):
177 def __init__(self):
178 self._mgr = None
178 self._mgr = None
179
179
180 def _get_mgr(self):
180 def _get_mgr(self):
181 if self._mgr is None:
181 if self._mgr is None:
182 self._mgr = urlreq.httppasswordmgrwithdefaultrealm()
182 self._mgr = urlreq.httppasswordmgrwithdefaultrealm()
183 return self._mgr
183 return self._mgr
184
184
185 def add_password(self, realm, uris, user, passwd):
185 def add_password(self, realm, uris, user, passwd):
186 return self._get_mgr().add_password(
186 return self._get_mgr().add_password(
187 _maybestrurl(realm),
187 _maybestrurl(realm),
188 _maybestrurl(uris),
188 _maybestrurl(uris),
189 _maybestrurl(user),
189 _maybestrurl(user),
190 _maybestrurl(passwd),
190 _maybestrurl(passwd),
191 )
191 )
192
192
193 def find_user_password(self, realm, uri):
193 def find_user_password(self, realm, uri):
194 mgr = self._get_mgr()
194 mgr = self._get_mgr()
195 return _maybebytesurl(
195 return _maybebytesurl(
196 mgr.find_user_password(_maybestrurl(realm), _maybestrurl(uri))
196 mgr.find_user_password(_maybestrurl(realm), _maybestrurl(uri))
197 )
197 )
198
198
199
199
200 def _catchterm(*args):
200 def _catchterm(*args):
201 raise error.SignalInterrupt
201 raise error.SignalInterrupt
202
202
203
203
204 # unique object used to detect no default value has been provided when
204 # unique object used to detect no default value has been provided when
205 # retrieving configuration value.
205 # retrieving configuration value.
206 _unset = object()
206 _unset = object()
207
207
208 # _reqexithandlers: callbacks run at the end of a request
208 # _reqexithandlers: callbacks run at the end of a request
209 _reqexithandlers = []
209 _reqexithandlers = []
210
210
211
211
212 class ui(object):
212 class ui(object):
213 def __init__(self, src=None):
213 def __init__(self, src=None):
214 """Create a fresh new ui object if no src given
214 """Create a fresh new ui object if no src given
215
215
216 Use uimod.ui.load() to create a ui which knows global and user configs.
216 Use uimod.ui.load() to create a ui which knows global and user configs.
217 In most cases, you should use ui.copy() to create a copy of an existing
217 In most cases, you should use ui.copy() to create a copy of an existing
218 ui object.
218 ui object.
219 """
219 """
220 # _buffers: used for temporary capture of output
220 # _buffers: used for temporary capture of output
221 self._buffers = []
221 self._buffers = []
222 # 3-tuple describing how each buffer in the stack behaves.
222 # 3-tuple describing how each buffer in the stack behaves.
223 # Values are (capture stderr, capture subprocesses, apply labels).
223 # Values are (capture stderr, capture subprocesses, apply labels).
224 self._bufferstates = []
224 self._bufferstates = []
225 # When a buffer is active, defines whether we are expanding labels.
225 # When a buffer is active, defines whether we are expanding labels.
226 # This exists to prevent an extra list lookup.
226 # This exists to prevent an extra list lookup.
227 self._bufferapplylabels = None
227 self._bufferapplylabels = None
228 self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
228 self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
229 self._reportuntrusted = True
229 self._reportuntrusted = True
230 self._knownconfig = configitems.coreitems
230 self._knownconfig = configitems.coreitems
231 self._ocfg = config.config() # overlay
231 self._ocfg = config.config() # overlay
232 self._tcfg = config.config() # trusted
232 self._tcfg = config.config() # trusted
233 self._ucfg = config.config() # untrusted
233 self._ucfg = config.config() # untrusted
234 self._trustusers = set()
234 self._trustusers = set()
235 self._trustgroups = set()
235 self._trustgroups = set()
236 self.callhooks = True
236 self.callhooks = True
237 # Insecure server connections requested.
237 # Insecure server connections requested.
238 self.insecureconnections = False
238 self.insecureconnections = False
239 # Blocked time
239 # Blocked time
240 self.logblockedtimes = False
240 self.logblockedtimes = False
241 # color mode: see mercurial/color.py for possible value
241 # color mode: see mercurial/color.py for possible value
242 self._colormode = None
242 self._colormode = None
243 self._terminfoparams = {}
243 self._terminfoparams = {}
244 self._styles = {}
244 self._styles = {}
245 self._uninterruptible = False
245 self._uninterruptible = False
246 self.showtimestamp = False
246 self.showtimestamp = False
247
247
248 if src:
248 if src:
249 self._fout = src._fout
249 self._fout = src._fout
250 self._ferr = src._ferr
250 self._ferr = src._ferr
251 self._fin = src._fin
251 self._fin = src._fin
252 self._fmsg = src._fmsg
252 self._fmsg = src._fmsg
253 self._fmsgout = src._fmsgout
253 self._fmsgout = src._fmsgout
254 self._fmsgerr = src._fmsgerr
254 self._fmsgerr = src._fmsgerr
255 self._finoutredirected = src._finoutredirected
255 self._finoutredirected = src._finoutredirected
256 self._loggers = src._loggers.copy()
256 self._loggers = src._loggers.copy()
257 self.pageractive = src.pageractive
257 self.pageractive = src.pageractive
258 self._disablepager = src._disablepager
258 self._disablepager = src._disablepager
259 self._tweaked = src._tweaked
259 self._tweaked = src._tweaked
260
260
261 self._tcfg = src._tcfg.copy()
261 self._tcfg = src._tcfg.copy()
262 self._ucfg = src._ucfg.copy()
262 self._ucfg = src._ucfg.copy()
263 self._ocfg = src._ocfg.copy()
263 self._ocfg = src._ocfg.copy()
264 self._trustusers = src._trustusers.copy()
264 self._trustusers = src._trustusers.copy()
265 self._trustgroups = src._trustgroups.copy()
265 self._trustgroups = src._trustgroups.copy()
266 self.environ = src.environ
266 self.environ = src.environ
267 self.callhooks = src.callhooks
267 self.callhooks = src.callhooks
268 self.insecureconnections = src.insecureconnections
268 self.insecureconnections = src.insecureconnections
269 self._colormode = src._colormode
269 self._colormode = src._colormode
270 self._terminfoparams = src._terminfoparams.copy()
270 self._terminfoparams = src._terminfoparams.copy()
271 self._styles = src._styles.copy()
271 self._styles = src._styles.copy()
272
272
273 self.fixconfig()
273 self.fixconfig()
274
274
275 self.httppasswordmgrdb = src.httppasswordmgrdb
275 self.httppasswordmgrdb = src.httppasswordmgrdb
276 self._blockedtimes = src._blockedtimes
276 self._blockedtimes = src._blockedtimes
277 else:
277 else:
278 self._fout = procutil.stdout
278 self._fout = procutil.stdout
279 self._ferr = procutil.stderr
279 self._ferr = procutil.stderr
280 self._fin = procutil.stdin
280 self._fin = procutil.stdin
281 self._fmsg = None
281 self._fmsg = None
282 self._fmsgout = self.fout # configurable
282 self._fmsgout = self.fout # configurable
283 self._fmsgerr = self.ferr # configurable
283 self._fmsgerr = self.ferr # configurable
284 self._finoutredirected = False
284 self._finoutredirected = False
285 self._loggers = {}
285 self._loggers = {}
286 self.pageractive = False
286 self.pageractive = False
287 self._disablepager = False
287 self._disablepager = False
288 self._tweaked = False
288 self._tweaked = False
289
289
290 # shared read-only environment
290 # shared read-only environment
291 self.environ = encoding.environ
291 self.environ = encoding.environ
292
292
293 self.httppasswordmgrdb = httppasswordmgrdbproxy()
293 self.httppasswordmgrdb = httppasswordmgrdbproxy()
294 self._blockedtimes = collections.defaultdict(int)
294 self._blockedtimes = collections.defaultdict(int)
295
295
296 allowed = self.configlist(b'experimental', b'exportableenviron')
296 allowed = self.configlist(b'experimental', b'exportableenviron')
297 if b'*' in allowed:
297 if b'*' in allowed:
298 self._exportableenviron = self.environ
298 self._exportableenviron = self.environ
299 else:
299 else:
300 self._exportableenviron = {}
300 self._exportableenviron = {}
301 for k in allowed:
301 for k in allowed:
302 if k in self.environ:
302 if k in self.environ:
303 self._exportableenviron[k] = self.environ[k]
303 self._exportableenviron[k] = self.environ[k]
304
304
305 def _new_source(self):
305 def _new_source(self):
306 self._ocfg.new_source()
306 self._ocfg.new_source()
307 self._tcfg.new_source()
307 self._tcfg.new_source()
308 self._ucfg.new_source()
308 self._ucfg.new_source()
309
309
310 @classmethod
310 @classmethod
311 def load(cls):
311 def load(cls):
312 """Create a ui and load global and user configs"""
312 """Create a ui and load global and user configs"""
313 u = cls()
313 u = cls()
314 # we always trust global config files and environment variables
314 # we always trust global config files and environment variables
315 for t, f in rcutil.rccomponents():
315 for t, f in rcutil.rccomponents():
316 if t == b'path':
316 if t == b'path':
317 u.readconfig(f, trust=True)
317 u.readconfig(f, trust=True)
318 elif t == b'resource':
318 elif t == b'resource':
319 u.read_resource_config(f, trust=True)
319 u.read_resource_config(f, trust=True)
320 elif t == b'items':
320 elif t == b'items':
321 u._new_source()
321 u._new_source()
322 sections = set()
322 sections = set()
323 for section, name, value, source in f:
323 for section, name, value, source in f:
324 # do not set u._ocfg
324 # do not set u._ocfg
325 # XXX clean this up once immutable config object is a thing
325 # XXX clean this up once immutable config object is a thing
326 u._tcfg.set(section, name, value, source)
326 u._tcfg.set(section, name, value, source)
327 u._ucfg.set(section, name, value, source)
327 u._ucfg.set(section, name, value, source)
328 sections.add(section)
328 sections.add(section)
329 for section in sections:
329 for section in sections:
330 u.fixconfig(section=section)
330 u.fixconfig(section=section)
331 else:
331 else:
332 raise error.ProgrammingError(b'unknown rctype: %s' % t)
332 raise error.ProgrammingError(b'unknown rctype: %s' % t)
333 u._maybetweakdefaults()
333 u._maybetweakdefaults()
334 u._new_source() # anything after that is a different level
334 u._new_source() # anything after that is a different level
335 return u
335 return u
336
336
337 def _maybetweakdefaults(self):
337 def _maybetweakdefaults(self):
338 if not self.configbool(b'ui', b'tweakdefaults'):
338 if not self.configbool(b'ui', b'tweakdefaults'):
339 return
339 return
340 if self._tweaked or self.plain(b'tweakdefaults'):
340 if self._tweaked or self.plain(b'tweakdefaults'):
341 return
341 return
342
342
343 # Note: it is SUPER IMPORTANT that you set self._tweaked to
343 # Note: it is SUPER IMPORTANT that you set self._tweaked to
344 # True *before* any calls to setconfig(), otherwise you'll get
344 # True *before* any calls to setconfig(), otherwise you'll get
345 # infinite recursion between setconfig and this method.
345 # infinite recursion between setconfig and this method.
346 #
346 #
347 # TODO: We should extract an inner method in setconfig() to
347 # TODO: We should extract an inner method in setconfig() to
348 # avoid this weirdness.
348 # avoid this weirdness.
349 self._tweaked = True
349 self._tweaked = True
350 tmpcfg = config.config()
350 tmpcfg = config.config()
351 tmpcfg.parse(b'<tweakdefaults>', tweakrc)
351 tmpcfg.parse(b'<tweakdefaults>', tweakrc)
352 for section in tmpcfg:
352 for section in tmpcfg:
353 for name, value in tmpcfg.items(section):
353 for name, value in tmpcfg.items(section):
354 if not self.hasconfig(section, name):
354 if not self.hasconfig(section, name):
355 self.setconfig(section, name, value, b"<tweakdefaults>")
355 self.setconfig(section, name, value, b"<tweakdefaults>")
356
356
357 def copy(self):
357 def copy(self):
358 return self.__class__(self)
358 return self.__class__(self)
359
359
360 def resetstate(self):
360 def resetstate(self):
361 """Clear internal state that shouldn't persist across commands"""
361 """Clear internal state that shouldn't persist across commands"""
362 if self._progbar:
362 if self._progbar:
363 self._progbar.resetstate() # reset last-print time of progress bar
363 self._progbar.resetstate() # reset last-print time of progress bar
364 self.httppasswordmgrdb = httppasswordmgrdbproxy()
364 self.httppasswordmgrdb = httppasswordmgrdbproxy()
365
365
366 @contextlib.contextmanager
366 @contextlib.contextmanager
367 def timeblockedsection(self, key):
367 def timeblockedsection(self, key):
368 # this is open-coded below - search for timeblockedsection to find them
368 # this is open-coded below - search for timeblockedsection to find them
369 starttime = util.timer()
369 starttime = util.timer()
370 try:
370 try:
371 yield
371 yield
372 finally:
372 finally:
373 self._blockedtimes[key + b'_blocked'] += (
373 self._blockedtimes[key + b'_blocked'] += (
374 util.timer() - starttime
374 util.timer() - starttime
375 ) * 1000
375 ) * 1000
376
376
377 @contextlib.contextmanager
377 @contextlib.contextmanager
378 def uninterruptible(self):
378 def uninterruptible(self):
379 """Mark an operation as unsafe.
379 """Mark an operation as unsafe.
380
380
381 Most operations on a repository are safe to interrupt, but a
381 Most operations on a repository are safe to interrupt, but a
382 few are risky (for example repair.strip). This context manager
382 few are risky (for example repair.strip). This context manager
383 lets you advise Mercurial that something risky is happening so
383 lets you advise Mercurial that something risky is happening so
384 that control-C etc can be blocked if desired.
384 that control-C etc can be blocked if desired.
385 """
385 """
386 enabled = self.configbool(b'experimental', b'nointerrupt')
386 enabled = self.configbool(b'experimental', b'nointerrupt')
387 if enabled and self.configbool(
387 if enabled and self.configbool(
388 b'experimental', b'nointerrupt-interactiveonly'
388 b'experimental', b'nointerrupt-interactiveonly'
389 ):
389 ):
390 enabled = self.interactive()
390 enabled = self.interactive()
391 if self._uninterruptible or not enabled:
391 if self._uninterruptible or not enabled:
392 # if nointerrupt support is turned off, the process isn't
392 # if nointerrupt support is turned off, the process isn't
393 # interactive, or we're already in an uninterruptible
393 # interactive, or we're already in an uninterruptible
394 # block, do nothing.
394 # block, do nothing.
395 yield
395 yield
396 return
396 return
397
397
398 def warn():
398 def warn():
399 self.warn(_(b"shutting down cleanly\n"))
399 self.warn(_(b"shutting down cleanly\n"))
400 self.warn(
400 self.warn(
401 _(b"press ^C again to terminate immediately (dangerous)\n")
401 _(b"press ^C again to terminate immediately (dangerous)\n")
402 )
402 )
403 return True
403 return True
404
404
405 with procutil.uninterruptible(warn):
405 with procutil.uninterruptible(warn):
406 try:
406 try:
407 self._uninterruptible = True
407 self._uninterruptible = True
408 yield
408 yield
409 finally:
409 finally:
410 self._uninterruptible = False
410 self._uninterruptible = False
411
411
412 def formatter(self, topic, opts):
412 def formatter(self, topic, opts):
413 return formatter.formatter(self, self, topic, opts)
413 return formatter.formatter(self, self, topic, opts)
414
414
415 def _trusted(self, fp, f):
415 def _trusted(self, fp, f):
416 st = util.fstat(fp)
416 st = util.fstat(fp)
417 if util.isowner(st):
417 if util.isowner(st):
418 return True
418 return True
419
419
420 tusers, tgroups = self._trustusers, self._trustgroups
420 tusers, tgroups = self._trustusers, self._trustgroups
421 if b'*' in tusers or b'*' in tgroups:
421 if b'*' in tusers or b'*' in tgroups:
422 return True
422 return True
423
423
424 user = util.username(st.st_uid)
424 user = util.username(st.st_uid)
425 group = util.groupname(st.st_gid)
425 group = util.groupname(st.st_gid)
426 if user in tusers or group in tgroups or user == util.username():
426 if user in tusers or group in tgroups or user == util.username():
427 return True
427 return True
428
428
429 if self._reportuntrusted:
429 if self._reportuntrusted:
430 self.warn(
430 self.warn(
431 _(
431 _(
432 b'not trusting file %s from untrusted '
432 b'not trusting file %s from untrusted '
433 b'user %s, group %s\n'
433 b'user %s, group %s\n'
434 )
434 )
435 % (f, user, group)
435 % (f, user, group)
436 )
436 )
437 return False
437 return False
438
438
439 def read_resource_config(
439 def read_resource_config(
440 self, name, root=None, trust=False, sections=None, remap=None
440 self, name, root=None, trust=False, sections=None, remap=None
441 ):
441 ):
442 try:
442 try:
443 fp = resourceutil.open_resource(name[0], name[1])
443 fp = resourceutil.open_resource(name[0], name[1])
444 except IOError:
444 except IOError:
445 if not sections: # ignore unless we were looking for something
445 if not sections: # ignore unless we were looking for something
446 return
446 return
447 raise
447 raise
448
448
449 self._readconfig(
449 self._readconfig(
450 b'resource:%s.%s' % name, fp, root, trust, sections, remap
450 b'resource:%s.%s' % name, fp, root, trust, sections, remap
451 )
451 )
452
452
453 def readconfig(
453 def readconfig(
454 self, filename, root=None, trust=False, sections=None, remap=None
454 self, filename, root=None, trust=False, sections=None, remap=None
455 ):
455 ):
456 try:
456 try:
457 fp = open(filename, 'rb')
457 fp = open(filename, 'rb')
458 except IOError:
458 except IOError:
459 if not sections: # ignore unless we were looking for something
459 if not sections: # ignore unless we were looking for something
460 return
460 return
461 raise
461 raise
462
462
463 self._readconfig(filename, fp, root, trust, sections, remap)
463 self._readconfig(filename, fp, root, trust, sections, remap)
464
464
465 def _readconfig(
465 def _readconfig(
466 self, filename, fp, root=None, trust=False, sections=None, remap=None
466 self, filename, fp, root=None, trust=False, sections=None, remap=None
467 ):
467 ):
468 with fp:
468 with fp:
469 cfg = config.config()
469 cfg = config.config()
470 trusted = sections or trust or self._trusted(fp, filename)
470 trusted = sections or trust or self._trusted(fp, filename)
471
471
472 try:
472 try:
473 cfg.read(filename, fp, sections=sections, remap=remap)
473 cfg.read(filename, fp, sections=sections, remap=remap)
474 except error.ConfigError as inst:
474 except error.ConfigError as inst:
475 if trusted:
475 if trusted:
476 raise
476 raise
477 self.warn(
477 self.warn(
478 _(b'ignored %s: %s\n') % (inst.location, inst.message)
478 _(b'ignored %s: %s\n') % (inst.location, inst.message)
479 )
479 )
480
480
481 self._applyconfig(cfg, trusted, root)
481 self._applyconfig(cfg, trusted, root)
482
482
483 def applyconfig(self, configitems, source=b"", root=None):
483 def applyconfig(self, configitems, source=b"", root=None):
484 """Add configitems from a non-file source. Unlike with ``setconfig()``,
484 """Add configitems from a non-file source. Unlike with ``setconfig()``,
485 they can be overridden by subsequent config file reads. The items are
485 they can be overridden by subsequent config file reads. The items are
486 in the same format as ``configoverride()``, namely a dict of the
486 in the same format as ``configoverride()``, namely a dict of the
487 following structures: {(section, name) : value}
487 following structures: {(section, name) : value}
488
488
489 Typically this is used by extensions that inject themselves into the
489 Typically this is used by extensions that inject themselves into the
490 config file load procedure by monkeypatching ``localrepo.loadhgrc()``.
490 config file load procedure by monkeypatching ``localrepo.loadhgrc()``.
491 """
491 """
492 cfg = config.config()
492 cfg = config.config()
493
493
494 for (section, name), value in configitems.items():
494 for (section, name), value in configitems.items():
495 cfg.set(section, name, value, source)
495 cfg.set(section, name, value, source)
496
496
497 self._applyconfig(cfg, True, root)
497 self._applyconfig(cfg, True, root)
498
498
499 def _applyconfig(self, cfg, trusted, root):
499 def _applyconfig(self, cfg, trusted, root):
500 if self.plain():
500 if self.plain():
501 for k in (
501 for k in (
502 b'debug',
502 b'debug',
503 b'fallbackencoding',
503 b'fallbackencoding',
504 b'quiet',
504 b'quiet',
505 b'slash',
505 b'slash',
506 b'logtemplate',
506 b'logtemplate',
507 b'message-output',
507 b'message-output',
508 b'statuscopies',
508 b'statuscopies',
509 b'style',
509 b'style',
510 b'traceback',
510 b'traceback',
511 b'verbose',
511 b'verbose',
512 ):
512 ):
513 if k in cfg[b'ui']:
513 if k in cfg[b'ui']:
514 del cfg[b'ui'][k]
514 del cfg[b'ui'][k]
515 for k, v in cfg.items(b'defaults'):
515 for k, v in cfg.items(b'defaults'):
516 del cfg[b'defaults'][k]
516 del cfg[b'defaults'][k]
517 for k, v in cfg.items(b'commands'):
517 for k, v in cfg.items(b'commands'):
518 del cfg[b'commands'][k]
518 del cfg[b'commands'][k]
519 for k, v in cfg.items(b'command-templates'):
519 for k, v in cfg.items(b'command-templates'):
520 del cfg[b'command-templates'][k]
520 del cfg[b'command-templates'][k]
521 # Don't remove aliases from the configuration if in the exceptionlist
521 # Don't remove aliases from the configuration if in the exceptionlist
522 if self.plain(b'alias'):
522 if self.plain(b'alias'):
523 for k, v in cfg.items(b'alias'):
523 for k, v in cfg.items(b'alias'):
524 del cfg[b'alias'][k]
524 del cfg[b'alias'][k]
525 if self.plain(b'revsetalias'):
525 if self.plain(b'revsetalias'):
526 for k, v in cfg.items(b'revsetalias'):
526 for k, v in cfg.items(b'revsetalias'):
527 del cfg[b'revsetalias'][k]
527 del cfg[b'revsetalias'][k]
528 if self.plain(b'templatealias'):
528 if self.plain(b'templatealias'):
529 for k, v in cfg.items(b'templatealias'):
529 for k, v in cfg.items(b'templatealias'):
530 del cfg[b'templatealias'][k]
530 del cfg[b'templatealias'][k]
531
531
532 if trusted:
532 if trusted:
533 self._tcfg.update(cfg)
533 self._tcfg.update(cfg)
534 self._tcfg.update(self._ocfg)
534 self._tcfg.update(self._ocfg)
535 self._ucfg.update(cfg)
535 self._ucfg.update(cfg)
536 self._ucfg.update(self._ocfg)
536 self._ucfg.update(self._ocfg)
537
537
538 if root is None:
538 if root is None:
539 root = os.path.expanduser(b'~')
539 root = os.path.expanduser(b'~')
540 self.fixconfig(root=root)
540 self.fixconfig(root=root)
541
541
542 def fixconfig(self, root=None, section=None):
542 def fixconfig(self, root=None, section=None):
543 if section in (None, b'paths'):
543 if section in (None, b'paths'):
544 # expand vars and ~
544 # expand vars and ~
545 # translate paths relative to root (or home) into absolute paths
545 # translate paths relative to root (or home) into absolute paths
546 root = root or encoding.getcwd()
546 root = root or encoding.getcwd()
547 for c in self._tcfg, self._ucfg, self._ocfg:
547 for c in self._tcfg, self._ucfg, self._ocfg:
548 for n, p in c.items(b'paths'):
548 for n, p in c.items(b'paths'):
549 # Ignore sub-options.
549 # Ignore sub-options.
550 if b':' in n:
550 if b':' in n:
551 continue
551 continue
552 if not p:
552 if not p:
553 continue
553 continue
554 if b'%%' in p:
554 if b'%%' in p:
555 s = self.configsource(b'paths', n) or b'none'
555 s = self.configsource(b'paths', n) or b'none'
556 self.warn(
556 self.warn(
557 _(b"(deprecated '%%' in path %s=%s from %s)\n")
557 _(b"(deprecated '%%' in path %s=%s from %s)\n")
558 % (n, p, s)
558 % (n, p, s)
559 )
559 )
560 p = p.replace(b'%%', b'%')
560 p = p.replace(b'%%', b'%')
561 p = util.expandpath(p)
561 p = util.expandpath(p)
562 if not urlutil.hasscheme(p) and not os.path.isabs(p):
562 if not urlutil.hasscheme(p) and not os.path.isabs(p):
563 p = os.path.normpath(os.path.join(root, p))
563 p = os.path.normpath(os.path.join(root, p))
564 c.alter(b"paths", n, p)
564 c.alter(b"paths", n, p)
565
565
566 if section in (None, b'ui'):
566 if section in (None, b'ui'):
567 # update ui options
567 # update ui options
568 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
568 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
569 self.debugflag = self.configbool(b'ui', b'debug')
569 self.debugflag = self.configbool(b'ui', b'debug')
570 self.verbose = self.debugflag or self.configbool(b'ui', b'verbose')
570 self.verbose = self.debugflag or self.configbool(b'ui', b'verbose')
571 self.quiet = not self.debugflag and self.configbool(b'ui', b'quiet')
571 self.quiet = not self.debugflag and self.configbool(b'ui', b'quiet')
572 if self.verbose and self.quiet:
572 if self.verbose and self.quiet:
573 self.quiet = self.verbose = False
573 self.quiet = self.verbose = False
574 self._reportuntrusted = self.debugflag or self.configbool(
574 self._reportuntrusted = self.debugflag or self.configbool(
575 b"ui", b"report_untrusted"
575 b"ui", b"report_untrusted"
576 )
576 )
577 self.showtimestamp = self.configbool(b'ui', b'timestamp-output')
577 self.showtimestamp = self.configbool(b'ui', b'timestamp-output')
578 self.tracebackflag = self.configbool(b'ui', b'traceback')
578 self.tracebackflag = self.configbool(b'ui', b'traceback')
579 self.logblockedtimes = self.configbool(b'ui', b'logblockedtimes')
579 self.logblockedtimes = self.configbool(b'ui', b'logblockedtimes')
580
580
581 if section in (None, b'trusted'):
581 if section in (None, b'trusted'):
582 # update trust information
582 # update trust information
583 self._trustusers.update(self.configlist(b'trusted', b'users'))
583 self._trustusers.update(self.configlist(b'trusted', b'users'))
584 self._trustgroups.update(self.configlist(b'trusted', b'groups'))
584 self._trustgroups.update(self.configlist(b'trusted', b'groups'))
585
585
586 if section in (None, b'devel', b'ui') and self.debugflag:
586 if section in (None, b'devel', b'ui') and self.debugflag:
587 tracked = set()
587 tracked = set()
588 if self.configbool(b'devel', b'debug.extensions'):
588 if self.configbool(b'devel', b'debug.extensions'):
589 tracked.add(b'extension')
589 tracked.add(b'extension')
590 if tracked:
590 if tracked:
591 logger = loggingutil.fileobjectlogger(self._ferr, tracked)
591 logger = loggingutil.fileobjectlogger(self._ferr, tracked)
592 self.setlogger(b'debug', logger)
592 self.setlogger(b'debug', logger)
593
593
594 def backupconfig(self, section, item):
594 def backupconfig(self, section, item):
595 return (
595 return (
596 self._ocfg.backup(section, item),
596 self._ocfg.backup(section, item),
597 self._tcfg.backup(section, item),
597 self._tcfg.backup(section, item),
598 self._ucfg.backup(section, item),
598 self._ucfg.backup(section, item),
599 )
599 )
600
600
601 def restoreconfig(self, data):
601 def restoreconfig(self, data):
602 self._ocfg.restore(data[0])
602 self._ocfg.restore(data[0])
603 self._tcfg.restore(data[1])
603 self._tcfg.restore(data[1])
604 self._ucfg.restore(data[2])
604 self._ucfg.restore(data[2])
605
605
606 def setconfig(self, section, name, value, source=b''):
606 def setconfig(self, section, name, value, source=b''):
607 for cfg in (self._ocfg, self._tcfg, self._ucfg):
607 for cfg in (self._ocfg, self._tcfg, self._ucfg):
608 cfg.set(section, name, value, source)
608 cfg.set(section, name, value, source)
609 self.fixconfig(section=section)
609 self.fixconfig(section=section)
610 self._maybetweakdefaults()
610 self._maybetweakdefaults()
611
611
612 def _data(self, untrusted):
612 def _data(self, untrusted):
613 return untrusted and self._ucfg or self._tcfg
613 return untrusted and self._ucfg or self._tcfg
614
614
615 def configsource(self, section, name, untrusted=False):
615 def configsource(self, section, name, untrusted=False):
616 return self._data(untrusted).source(section, name)
616 return self._data(untrusted).source(section, name)
617
617
618 def config(self, section, name, default=_unset, untrusted=False):
618 def config(self, section, name, default=_unset, untrusted=False):
619 """return the plain string version of a config"""
619 """return the plain string version of a config"""
620 value = self._config(
620 value = self._config(
621 section, name, default=default, untrusted=untrusted
621 section, name, default=default, untrusted=untrusted
622 )
622 )
623 if value is _unset:
623 if value is _unset:
624 return None
624 return None
625 return value
625 return value
626
626
627 def _config(self, section, name, default=_unset, untrusted=False):
627 def _config(self, section, name, default=_unset, untrusted=False):
628 value = itemdefault = default
628 value = itemdefault = default
629 item = self._knownconfig.get(section, {}).get(name)
629 item = self._knownconfig.get(section, {}).get(name)
630 alternates = [(section, name)]
630 alternates = [(section, name)]
631
631
632 if item is not None:
632 if item is not None:
633 alternates.extend(item.alias)
633 alternates.extend(item.alias)
634 if callable(item.default):
634 if callable(item.default):
635 itemdefault = item.default()
635 itemdefault = item.default()
636 else:
636 else:
637 itemdefault = item.default
637 itemdefault = item.default
638 else:
638 else:
639 msg = b"accessing unregistered config item: '%s.%s'"
639 msg = b"accessing unregistered config item: '%s.%s'"
640 msg %= (section, name)
640 msg %= (section, name)
641 self.develwarn(msg, 2, b'warn-config-unknown')
641 self.develwarn(msg, 2, b'warn-config-unknown')
642
642
643 if default is _unset:
643 if default is _unset:
644 if item is None:
644 if item is None:
645 value = default
645 value = default
646 elif item.default is configitems.dynamicdefault:
646 elif item.default is configitems.dynamicdefault:
647 value = None
647 value = None
648 msg = b"config item requires an explicit default value: '%s.%s'"
648 msg = b"config item requires an explicit default value: '%s.%s'"
649 msg %= (section, name)
649 msg %= (section, name)
650 self.develwarn(msg, 2, b'warn-config-default')
650 self.develwarn(msg, 2, b'warn-config-default')
651 else:
651 else:
652 value = itemdefault
652 value = itemdefault
653 elif (
653 elif (
654 item is not None
654 item is not None
655 and item.default is not configitems.dynamicdefault
655 and item.default is not configitems.dynamicdefault
656 and default != itemdefault
656 and default != itemdefault
657 ):
657 ):
658 msg = (
658 msg = (
659 b"specifying a mismatched default value for a registered "
659 b"specifying a mismatched default value for a registered "
660 b"config item: '%s.%s' '%s'"
660 b"config item: '%s.%s' '%s'"
661 )
661 )
662 msg %= (section, name, pycompat.bytestr(default))
662 msg %= (section, name, pycompat.bytestr(default))
663 self.develwarn(msg, 2, b'warn-config-default')
663 self.develwarn(msg, 2, b'warn-config-default')
664
664
665 candidates = []
665 candidates = []
666 config = self._data(untrusted)
666 config = self._data(untrusted)
667 for s, n in alternates:
667 for s, n in alternates:
668 candidate = config.get(s, n, None)
668 candidate = config.get(s, n, None)
669 if candidate is not None:
669 if candidate is not None:
670 candidates.append((s, n, candidate))
670 candidates.append((s, n, candidate))
671 if candidates:
671 if candidates:
672
672
673 def level(x):
673 def level(x):
674 return config.level(x[0], x[1])
674 return config.level(x[0], x[1])
675
675
676 value = max(candidates, key=level)[2]
676 value = max(candidates, key=level)[2]
677
677
678 if self.debugflag and not untrusted and self._reportuntrusted:
678 if self.debugflag and not untrusted and self._reportuntrusted:
679 for s, n in alternates:
679 for s, n in alternates:
680 uvalue = self._ucfg.get(s, n)
680 uvalue = self._ucfg.get(s, n)
681 if uvalue is not None and uvalue != value:
681 if uvalue is not None and uvalue != value:
682 self.debug(
682 self.debug(
683 b"ignoring untrusted configuration option "
683 b"ignoring untrusted configuration option "
684 b"%s.%s = %s\n" % (s, n, uvalue)
684 b"%s.%s = %s\n" % (s, n, uvalue)
685 )
685 )
686 return value
686 return value
687
687
688 def config_default(self, section, name):
688 def config_default(self, section, name):
689 """return the default value for a config option
689 """return the default value for a config option
690
690
691 The default is returned "raw", for example if it is a callable, the
691 The default is returned "raw", for example if it is a callable, the
692 callable was not called.
692 callable was not called.
693 """
693 """
694 item = self._knownconfig.get(section, {}).get(name)
694 item = self._knownconfig.get(section, {}).get(name)
695
695
696 if item is None:
696 if item is None:
697 raise KeyError((section, name))
697 raise KeyError((section, name))
698 return item.default
698 return item.default
699
699
700 def configsuboptions(self, section, name, default=_unset, untrusted=False):
700 def configsuboptions(self, section, name, default=_unset, untrusted=False):
701 """Get a config option and all sub-options.
701 """Get a config option and all sub-options.
702
702
703 Some config options have sub-options that are declared with the
703 Some config options have sub-options that are declared with the
704 format "key:opt = value". This method is used to return the main
704 format "key:opt = value". This method is used to return the main
705 option and all its declared sub-options.
705 option and all its declared sub-options.
706
706
707 Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
707 Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
708 is a dict of defined sub-options where keys and values are strings.
708 is a dict of defined sub-options where keys and values are strings.
709 """
709 """
710 main = self.config(section, name, default, untrusted=untrusted)
710 main = self.config(section, name, default, untrusted=untrusted)
711 data = self._data(untrusted)
711 data = self._data(untrusted)
712 sub = {}
712 sub = {}
713 prefix = b'%s:' % name
713 prefix = b'%s:' % name
714 for k, v in data.items(section):
714 for k, v in data.items(section):
715 if k.startswith(prefix):
715 if k.startswith(prefix):
716 sub[k[len(prefix) :]] = v
716 sub[k[len(prefix) :]] = v
717
717
718 if self.debugflag and not untrusted and self._reportuntrusted:
718 if self.debugflag and not untrusted and self._reportuntrusted:
719 for k, v in sub.items():
719 for k, v in sub.items():
720 uvalue = self._ucfg.get(section, b'%s:%s' % (name, k))
720 uvalue = self._ucfg.get(section, b'%s:%s' % (name, k))
721 if uvalue is not None and uvalue != v:
721 if uvalue is not None and uvalue != v:
722 self.debug(
722 self.debug(
723 b'ignoring untrusted configuration option '
723 b'ignoring untrusted configuration option '
724 b'%s:%s.%s = %s\n' % (section, name, k, uvalue)
724 b'%s:%s.%s = %s\n' % (section, name, k, uvalue)
725 )
725 )
726
726
727 return main, sub
727 return main, sub
728
728
729 def configpath(self, section, name, default=_unset, untrusted=False):
729 def configpath(self, section, name, default=_unset, untrusted=False):
730 """get a path config item, expanded relative to repo root or config
730 """get a path config item, expanded relative to repo root or config
731 file"""
731 file"""
732 v = self.config(section, name, default, untrusted)
732 v = self.config(section, name, default, untrusted)
733 if v is None:
733 if v is None:
734 return None
734 return None
735 if not os.path.isabs(v) or b"://" not in v:
735 if not os.path.isabs(v) or b"://" not in v:
736 src = self.configsource(section, name, untrusted)
736 src = self.configsource(section, name, untrusted)
737 if b':' in src:
737 if b':' in src:
738 base = os.path.dirname(src.rsplit(b':')[0])
738 base = os.path.dirname(src.rsplit(b':')[0])
739 v = os.path.join(base, os.path.expanduser(v))
739 v = os.path.join(base, os.path.expanduser(v))
740 return v
740 return v
741
741
742 def configbool(self, section, name, default=_unset, untrusted=False):
742 def configbool(self, section, name, default=_unset, untrusted=False):
743 """parse a configuration element as a boolean
743 """parse a configuration element as a boolean
744
744
745 >>> u = ui(); s = b'foo'
745 >>> u = ui(); s = b'foo'
746 >>> u.setconfig(s, b'true', b'yes')
746 >>> u.setconfig(s, b'true', b'yes')
747 >>> u.configbool(s, b'true')
747 >>> u.configbool(s, b'true')
748 True
748 True
749 >>> u.setconfig(s, b'false', b'no')
749 >>> u.setconfig(s, b'false', b'no')
750 >>> u.configbool(s, b'false')
750 >>> u.configbool(s, b'false')
751 False
751 False
752 >>> u.configbool(s, b'unknown')
752 >>> u.configbool(s, b'unknown')
753 False
753 False
754 >>> u.configbool(s, b'unknown', True)
754 >>> u.configbool(s, b'unknown', True)
755 True
755 True
756 >>> u.setconfig(s, b'invalid', b'somevalue')
756 >>> u.setconfig(s, b'invalid', b'somevalue')
757 >>> u.configbool(s, b'invalid')
757 >>> u.configbool(s, b'invalid')
758 Traceback (most recent call last):
758 Traceback (most recent call last):
759 ...
759 ...
760 ConfigError: foo.invalid is not a boolean ('somevalue')
760 ConfigError: foo.invalid is not a boolean ('somevalue')
761 """
761 """
762
762
763 v = self._config(section, name, default, untrusted=untrusted)
763 v = self._config(section, name, default, untrusted=untrusted)
764 if v is None:
764 if v is None:
765 return v
765 return v
766 if v is _unset:
766 if v is _unset:
767 if default is _unset:
767 if default is _unset:
768 return False
768 return False
769 return default
769 return default
770 if isinstance(v, bool):
770 if isinstance(v, bool):
771 return v
771 return v
772 b = stringutil.parsebool(v)
772 b = stringutil.parsebool(v)
773 if b is None:
773 if b is None:
774 raise error.ConfigError(
774 raise error.ConfigError(
775 _(b"%s.%s is not a boolean ('%s')") % (section, name, v)
775 _(b"%s.%s is not a boolean ('%s')") % (section, name, v)
776 )
776 )
777 return b
777 return b
778
778
779 def configwith(
779 def configwith(
780 self, convert, section, name, default=_unset, desc=None, untrusted=False
780 self, convert, section, name, default=_unset, desc=None, untrusted=False
781 ):
781 ):
782 """parse a configuration element with a conversion function
782 """parse a configuration element with a conversion function
783
783
784 >>> u = ui(); s = b'foo'
784 >>> u = ui(); s = b'foo'
785 >>> u.setconfig(s, b'float1', b'42')
785 >>> u.setconfig(s, b'float1', b'42')
786 >>> u.configwith(float, s, b'float1')
786 >>> u.configwith(float, s, b'float1')
787 42.0
787 42.0
788 >>> u.setconfig(s, b'float2', b'-4.25')
788 >>> u.setconfig(s, b'float2', b'-4.25')
789 >>> u.configwith(float, s, b'float2')
789 >>> u.configwith(float, s, b'float2')
790 -4.25
790 -4.25
791 >>> u.configwith(float, s, b'unknown', 7)
791 >>> u.configwith(float, s, b'unknown', 7)
792 7.0
792 7.0
793 >>> u.setconfig(s, b'invalid', b'somevalue')
793 >>> u.setconfig(s, b'invalid', b'somevalue')
794 >>> u.configwith(float, s, b'invalid')
794 >>> u.configwith(float, s, b'invalid')
795 Traceback (most recent call last):
795 Traceback (most recent call last):
796 ...
796 ...
797 ConfigError: foo.invalid is not a valid float ('somevalue')
797 ConfigError: foo.invalid is not a valid float ('somevalue')
798 >>> u.configwith(float, s, b'invalid', desc=b'womble')
798 >>> u.configwith(float, s, b'invalid', desc=b'womble')
799 Traceback (most recent call last):
799 Traceback (most recent call last):
800 ...
800 ...
801 ConfigError: foo.invalid is not a valid womble ('somevalue')
801 ConfigError: foo.invalid is not a valid womble ('somevalue')
802 """
802 """
803
803
804 v = self.config(section, name, default, untrusted)
804 v = self.config(section, name, default, untrusted)
805 if v is None:
805 if v is None:
806 return v # do not attempt to convert None
806 return v # do not attempt to convert None
807 try:
807 try:
808 return convert(v)
808 return convert(v)
809 except (ValueError, error.ParseError):
809 except (ValueError, error.ParseError):
810 if desc is None:
810 if desc is None:
811 desc = pycompat.sysbytes(convert.__name__)
811 desc = pycompat.sysbytes(convert.__name__)
812 raise error.ConfigError(
812 raise error.ConfigError(
813 _(b"%s.%s is not a valid %s ('%s')") % (section, name, desc, v)
813 _(b"%s.%s is not a valid %s ('%s')") % (section, name, desc, v)
814 )
814 )
815
815
816 def configint(self, section, name, default=_unset, untrusted=False):
816 def configint(self, section, name, default=_unset, untrusted=False):
817 """parse a configuration element as an integer
817 """parse a configuration element as an integer
818
818
819 >>> u = ui(); s = b'foo'
819 >>> u = ui(); s = b'foo'
820 >>> u.setconfig(s, b'int1', b'42')
820 >>> u.setconfig(s, b'int1', b'42')
821 >>> u.configint(s, b'int1')
821 >>> u.configint(s, b'int1')
822 42
822 42
823 >>> u.setconfig(s, b'int2', b'-42')
823 >>> u.setconfig(s, b'int2', b'-42')
824 >>> u.configint(s, b'int2')
824 >>> u.configint(s, b'int2')
825 -42
825 -42
826 >>> u.configint(s, b'unknown', 7)
826 >>> u.configint(s, b'unknown', 7)
827 7
827 7
828 >>> u.setconfig(s, b'invalid', b'somevalue')
828 >>> u.setconfig(s, b'invalid', b'somevalue')
829 >>> u.configint(s, b'invalid')
829 >>> u.configint(s, b'invalid')
830 Traceback (most recent call last):
830 Traceback (most recent call last):
831 ...
831 ...
832 ConfigError: foo.invalid is not a valid integer ('somevalue')
832 ConfigError: foo.invalid is not a valid integer ('somevalue')
833 """
833 """
834
834
835 return self.configwith(
835 return self.configwith(
836 int, section, name, default, b'integer', untrusted
836 int, section, name, default, b'integer', untrusted
837 )
837 )
838
838
839 def configbytes(self, section, name, default=_unset, untrusted=False):
839 def configbytes(self, section, name, default=_unset, untrusted=False):
840 """parse a configuration element as a quantity in bytes
840 """parse a configuration element as a quantity in bytes
841
841
842 Units can be specified as b (bytes), k or kb (kilobytes), m or
842 Units can be specified as b (bytes), k or kb (kilobytes), m or
843 mb (megabytes), g or gb (gigabytes).
843 mb (megabytes), g or gb (gigabytes).
844
844
845 >>> u = ui(); s = b'foo'
845 >>> u = ui(); s = b'foo'
846 >>> u.setconfig(s, b'val1', b'42')
846 >>> u.setconfig(s, b'val1', b'42')
847 >>> u.configbytes(s, b'val1')
847 >>> u.configbytes(s, b'val1')
848 42
848 42
849 >>> u.setconfig(s, b'val2', b'42.5 kb')
849 >>> u.setconfig(s, b'val2', b'42.5 kb')
850 >>> u.configbytes(s, b'val2')
850 >>> u.configbytes(s, b'val2')
851 43520
851 43520
852 >>> u.configbytes(s, b'unknown', b'7 MB')
852 >>> u.configbytes(s, b'unknown', b'7 MB')
853 7340032
853 7340032
854 >>> u.setconfig(s, b'invalid', b'somevalue')
854 >>> u.setconfig(s, b'invalid', b'somevalue')
855 >>> u.configbytes(s, b'invalid')
855 >>> u.configbytes(s, b'invalid')
856 Traceback (most recent call last):
856 Traceback (most recent call last):
857 ...
857 ...
858 ConfigError: foo.invalid is not a byte quantity ('somevalue')
858 ConfigError: foo.invalid is not a byte quantity ('somevalue')
859 """
859 """
860
860
861 value = self._config(section, name, default, untrusted)
861 value = self._config(section, name, default, untrusted)
862 if value is _unset:
862 if value is _unset:
863 if default is _unset:
863 if default is _unset:
864 default = 0
864 default = 0
865 value = default
865 value = default
866 if not isinstance(value, bytes):
866 if not isinstance(value, bytes):
867 return value
867 return value
868 try:
868 try:
869 return util.sizetoint(value)
869 return util.sizetoint(value)
870 except error.ParseError:
870 except error.ParseError:
871 raise error.ConfigError(
871 raise error.ConfigError(
872 _(b"%s.%s is not a byte quantity ('%s')")
872 _(b"%s.%s is not a byte quantity ('%s')")
873 % (section, name, value)
873 % (section, name, value)
874 )
874 )
875
875
876 def configlist(self, section, name, default=_unset, untrusted=False):
876 def configlist(self, section, name, default=_unset, untrusted=False):
877 """parse a configuration element as a list of comma/space separated
877 """parse a configuration element as a list of comma/space separated
878 strings
878 strings
879
879
880 >>> u = ui(); s = b'foo'
880 >>> u = ui(); s = b'foo'
881 >>> u.setconfig(s, b'list1', b'this,is "a small" ,test')
881 >>> u.setconfig(s, b'list1', b'this,is "a small" ,test')
882 >>> u.configlist(s, b'list1')
882 >>> u.configlist(s, b'list1')
883 ['this', 'is', 'a small', 'test']
883 ['this', 'is', 'a small', 'test']
884 >>> u.setconfig(s, b'list2', b'this, is "a small" , test ')
884 >>> u.setconfig(s, b'list2', b'this, is "a small" , test ')
885 >>> u.configlist(s, b'list2')
885 >>> u.configlist(s, b'list2')
886 ['this', 'is', 'a small', 'test']
886 ['this', 'is', 'a small', 'test']
887 """
887 """
888 # default is not always a list
888 # default is not always a list
889 v = self.configwith(
889 v = self.configwith(
890 config.parselist, section, name, default, b'list', untrusted
890 stringutil.parselist, section, name, default, b'list', untrusted
891 )
891 )
892 if isinstance(v, bytes):
892 if isinstance(v, bytes):
893 return config.parselist(v)
893 return stringutil.parselist(v)
894 elif v is None:
894 elif v is None:
895 return []
895 return []
896 return v
896 return v
897
897
898 def configdate(self, section, name, default=_unset, untrusted=False):
898 def configdate(self, section, name, default=_unset, untrusted=False):
899 """parse a configuration element as a tuple of ints
899 """parse a configuration element as a tuple of ints
900
900
901 >>> u = ui(); s = b'foo'
901 >>> u = ui(); s = b'foo'
902 >>> u.setconfig(s, b'date', b'0 0')
902 >>> u.setconfig(s, b'date', b'0 0')
903 >>> u.configdate(s, b'date')
903 >>> u.configdate(s, b'date')
904 (0, 0)
904 (0, 0)
905 """
905 """
906 if self.config(section, name, default, untrusted):
906 if self.config(section, name, default, untrusted):
907 return self.configwith(
907 return self.configwith(
908 dateutil.parsedate, section, name, default, b'date', untrusted
908 dateutil.parsedate, section, name, default, b'date', untrusted
909 )
909 )
910 if default is _unset:
910 if default is _unset:
911 return None
911 return None
912 return default
912 return default
913
913
914 def configdefault(self, section, name):
914 def configdefault(self, section, name):
915 """returns the default value of the config item"""
915 """returns the default value of the config item"""
916 item = self._knownconfig.get(section, {}).get(name)
916 item = self._knownconfig.get(section, {}).get(name)
917 itemdefault = None
917 itemdefault = None
918 if item is not None:
918 if item is not None:
919 if callable(item.default):
919 if callable(item.default):
920 itemdefault = item.default()
920 itemdefault = item.default()
921 else:
921 else:
922 itemdefault = item.default
922 itemdefault = item.default
923 return itemdefault
923 return itemdefault
924
924
925 def hasconfig(self, section, name, untrusted=False):
925 def hasconfig(self, section, name, untrusted=False):
926 return self._data(untrusted).hasitem(section, name)
926 return self._data(untrusted).hasitem(section, name)
927
927
928 def has_section(self, section, untrusted=False):
928 def has_section(self, section, untrusted=False):
929 '''tell whether section exists in config.'''
929 '''tell whether section exists in config.'''
930 return section in self._data(untrusted)
930 return section in self._data(untrusted)
931
931
932 def configitems(self, section, untrusted=False, ignoresub=False):
932 def configitems(self, section, untrusted=False, ignoresub=False):
933 items = self._data(untrusted).items(section)
933 items = self._data(untrusted).items(section)
934 if ignoresub:
934 if ignoresub:
935 items = [i for i in items if b':' not in i[0]]
935 items = [i for i in items if b':' not in i[0]]
936 if self.debugflag and not untrusted and self._reportuntrusted:
936 if self.debugflag and not untrusted and self._reportuntrusted:
937 for k, v in self._ucfg.items(section):
937 for k, v in self._ucfg.items(section):
938 if self._tcfg.get(section, k) != v:
938 if self._tcfg.get(section, k) != v:
939 self.debug(
939 self.debug(
940 b"ignoring untrusted configuration option "
940 b"ignoring untrusted configuration option "
941 b"%s.%s = %s\n" % (section, k, v)
941 b"%s.%s = %s\n" % (section, k, v)
942 )
942 )
943 return items
943 return items
944
944
945 def walkconfig(self, untrusted=False):
945 def walkconfig(self, untrusted=False):
946 cfg = self._data(untrusted)
946 cfg = self._data(untrusted)
947 for section in cfg.sections():
947 for section in cfg.sections():
948 for name, value in self.configitems(section, untrusted):
948 for name, value in self.configitems(section, untrusted):
949 yield section, name, value
949 yield section, name, value
950
950
951 def plain(self, feature=None):
951 def plain(self, feature=None):
952 """is plain mode active?
952 """is plain mode active?
953
953
954 Plain mode means that all configuration variables which affect
954 Plain mode means that all configuration variables which affect
955 the behavior and output of Mercurial should be
955 the behavior and output of Mercurial should be
956 ignored. Additionally, the output should be stable,
956 ignored. Additionally, the output should be stable,
957 reproducible and suitable for use in scripts or applications.
957 reproducible and suitable for use in scripts or applications.
958
958
959 The only way to trigger plain mode is by setting either the
959 The only way to trigger plain mode is by setting either the
960 `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
960 `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
961
961
962 The return value can either be
962 The return value can either be
963 - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
963 - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
964 - False if feature is disabled by default and not included in HGPLAIN
964 - False if feature is disabled by default and not included in HGPLAIN
965 - True otherwise
965 - True otherwise
966 """
966 """
967 if (
967 if (
968 b'HGPLAIN' not in encoding.environ
968 b'HGPLAIN' not in encoding.environ
969 and b'HGPLAINEXCEPT' not in encoding.environ
969 and b'HGPLAINEXCEPT' not in encoding.environ
970 ):
970 ):
971 return False
971 return False
972 exceptions = (
972 exceptions = (
973 encoding.environ.get(b'HGPLAINEXCEPT', b'').strip().split(b',')
973 encoding.environ.get(b'HGPLAINEXCEPT', b'').strip().split(b',')
974 )
974 )
975 # TODO: add support for HGPLAIN=+feature,-feature syntax
975 # TODO: add support for HGPLAIN=+feature,-feature syntax
976 if b'+strictflags' not in encoding.environ.get(b'HGPLAIN', b'').split(
976 if b'+strictflags' not in encoding.environ.get(b'HGPLAIN', b'').split(
977 b','
977 b','
978 ):
978 ):
979 exceptions.append(b'strictflags')
979 exceptions.append(b'strictflags')
980 if feature and exceptions:
980 if feature and exceptions:
981 return feature not in exceptions
981 return feature not in exceptions
982 return True
982 return True
983
983
984 def username(self, acceptempty=False):
984 def username(self, acceptempty=False):
985 """Return default username to be used in commits.
985 """Return default username to be used in commits.
986
986
987 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
987 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
988 and stop searching if one of these is set.
988 and stop searching if one of these is set.
989 If not found and acceptempty is True, returns None.
989 If not found and acceptempty is True, returns None.
990 If not found and ui.askusername is True, ask the user, else use
990 If not found and ui.askusername is True, ask the user, else use
991 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
991 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
992 If no username could be found, raise an Abort error.
992 If no username could be found, raise an Abort error.
993 """
993 """
994 user = encoding.environ.get(b"HGUSER")
994 user = encoding.environ.get(b"HGUSER")
995 if user is None:
995 if user is None:
996 user = self.config(b"ui", b"username")
996 user = self.config(b"ui", b"username")
997 if user is not None:
997 if user is not None:
998 user = os.path.expandvars(user)
998 user = os.path.expandvars(user)
999 if user is None:
999 if user is None:
1000 user = encoding.environ.get(b"EMAIL")
1000 user = encoding.environ.get(b"EMAIL")
1001 if user is None and acceptempty:
1001 if user is None and acceptempty:
1002 return user
1002 return user
1003 if user is None and self.configbool(b"ui", b"askusername"):
1003 if user is None and self.configbool(b"ui", b"askusername"):
1004 user = self.prompt(_(b"enter a commit username:"), default=None)
1004 user = self.prompt(_(b"enter a commit username:"), default=None)
1005 if user is None and not self.interactive():
1005 if user is None and not self.interactive():
1006 try:
1006 try:
1007 user = b'%s@%s' % (
1007 user = b'%s@%s' % (
1008 procutil.getuser(),
1008 procutil.getuser(),
1009 encoding.strtolocal(socket.getfqdn()),
1009 encoding.strtolocal(socket.getfqdn()),
1010 )
1010 )
1011 self.warn(_(b"no username found, using '%s' instead\n") % user)
1011 self.warn(_(b"no username found, using '%s' instead\n") % user)
1012 except KeyError:
1012 except KeyError:
1013 pass
1013 pass
1014 if not user:
1014 if not user:
1015 raise error.Abort(
1015 raise error.Abort(
1016 _(b'no username supplied'),
1016 _(b'no username supplied'),
1017 hint=_(b"use 'hg config --edit' " b'to set your username'),
1017 hint=_(b"use 'hg config --edit' " b'to set your username'),
1018 )
1018 )
1019 if b"\n" in user:
1019 if b"\n" in user:
1020 raise error.Abort(
1020 raise error.Abort(
1021 _(b"username %r contains a newline\n") % pycompat.bytestr(user)
1021 _(b"username %r contains a newline\n") % pycompat.bytestr(user)
1022 )
1022 )
1023 return user
1023 return user
1024
1024
1025 def shortuser(self, user):
1025 def shortuser(self, user):
1026 """Return a short representation of a user name or email address."""
1026 """Return a short representation of a user name or email address."""
1027 if not self.verbose:
1027 if not self.verbose:
1028 user = stringutil.shortuser(user)
1028 user = stringutil.shortuser(user)
1029 return user
1029 return user
1030
1030
1031 def expandpath(self, loc, default=None):
1031 def expandpath(self, loc, default=None):
1032 """Return repository location relative to cwd or from [paths]"""
1032 """Return repository location relative to cwd or from [paths]"""
1033 msg = b'ui.expandpath is deprecated, use `get_*` functions from urlutil'
1033 msg = b'ui.expandpath is deprecated, use `get_*` functions from urlutil'
1034 self.deprecwarn(msg, b'6.0')
1034 self.deprecwarn(msg, b'6.0')
1035 try:
1035 try:
1036 p = self.getpath(loc)
1036 p = self.getpath(loc)
1037 if p:
1037 if p:
1038 return p.rawloc
1038 return p.rawloc
1039 except error.RepoError:
1039 except error.RepoError:
1040 pass
1040 pass
1041
1041
1042 if default:
1042 if default:
1043 try:
1043 try:
1044 p = self.getpath(default)
1044 p = self.getpath(default)
1045 if p:
1045 if p:
1046 return p.rawloc
1046 return p.rawloc
1047 except error.RepoError:
1047 except error.RepoError:
1048 pass
1048 pass
1049
1049
1050 return loc
1050 return loc
1051
1051
1052 @util.propertycache
1052 @util.propertycache
1053 def paths(self):
1053 def paths(self):
1054 return urlutil.paths(self)
1054 return urlutil.paths(self)
1055
1055
1056 def getpath(self, *args, **kwargs):
1056 def getpath(self, *args, **kwargs):
1057 """see paths.getpath for details
1057 """see paths.getpath for details
1058
1058
1059 This method exist as `getpath` need a ui for potential warning message.
1059 This method exist as `getpath` need a ui for potential warning message.
1060 """
1060 """
1061 msg = b'ui.getpath is deprecated, use `get_*` functions from urlutil'
1061 msg = b'ui.getpath is deprecated, use `get_*` functions from urlutil'
1062 self.deprecwarn(msg, '6.0')
1062 self.deprecwarn(msg, '6.0')
1063 return self.paths.getpath(self, *args, **kwargs)
1063 return self.paths.getpath(self, *args, **kwargs)
1064
1064
1065 @property
1065 @property
1066 def fout(self):
1066 def fout(self):
1067 return self._fout
1067 return self._fout
1068
1068
1069 @fout.setter
1069 @fout.setter
1070 def fout(self, f):
1070 def fout(self, f):
1071 self._fout = f
1071 self._fout = f
1072 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1072 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1073
1073
1074 @property
1074 @property
1075 def ferr(self):
1075 def ferr(self):
1076 return self._ferr
1076 return self._ferr
1077
1077
1078 @ferr.setter
1078 @ferr.setter
1079 def ferr(self, f):
1079 def ferr(self, f):
1080 self._ferr = f
1080 self._ferr = f
1081 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1081 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1082
1082
1083 @property
1083 @property
1084 def fin(self):
1084 def fin(self):
1085 return self._fin
1085 return self._fin
1086
1086
1087 @fin.setter
1087 @fin.setter
1088 def fin(self, f):
1088 def fin(self, f):
1089 self._fin = f
1089 self._fin = f
1090
1090
1091 @property
1091 @property
1092 def fmsg(self):
1092 def fmsg(self):
1093 """Stream dedicated for status/error messages; may be None if
1093 """Stream dedicated for status/error messages; may be None if
1094 fout/ferr are used"""
1094 fout/ferr are used"""
1095 return self._fmsg
1095 return self._fmsg
1096
1096
1097 @fmsg.setter
1097 @fmsg.setter
1098 def fmsg(self, f):
1098 def fmsg(self, f):
1099 self._fmsg = f
1099 self._fmsg = f
1100 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1100 self._fmsgout, self._fmsgerr = _selectmsgdests(self)
1101
1101
1102 def pushbuffer(self, error=False, subproc=False, labeled=False):
1102 def pushbuffer(self, error=False, subproc=False, labeled=False):
1103 """install a buffer to capture standard output of the ui object
1103 """install a buffer to capture standard output of the ui object
1104
1104
1105 If error is True, the error output will be captured too.
1105 If error is True, the error output will be captured too.
1106
1106
1107 If subproc is True, output from subprocesses (typically hooks) will be
1107 If subproc is True, output from subprocesses (typically hooks) will be
1108 captured too.
1108 captured too.
1109
1109
1110 If labeled is True, any labels associated with buffered
1110 If labeled is True, any labels associated with buffered
1111 output will be handled. By default, this has no effect
1111 output will be handled. By default, this has no effect
1112 on the output returned, but extensions and GUI tools may
1112 on the output returned, but extensions and GUI tools may
1113 handle this argument and returned styled output. If output
1113 handle this argument and returned styled output. If output
1114 is being buffered so it can be captured and parsed or
1114 is being buffered so it can be captured and parsed or
1115 processed, labeled should not be set to True.
1115 processed, labeled should not be set to True.
1116 """
1116 """
1117 self._buffers.append([])
1117 self._buffers.append([])
1118 self._bufferstates.append((error, subproc, labeled))
1118 self._bufferstates.append((error, subproc, labeled))
1119 self._bufferapplylabels = labeled
1119 self._bufferapplylabels = labeled
1120
1120
1121 def popbuffer(self):
1121 def popbuffer(self):
1122 '''pop the last buffer and return the buffered output'''
1122 '''pop the last buffer and return the buffered output'''
1123 self._bufferstates.pop()
1123 self._bufferstates.pop()
1124 if self._bufferstates:
1124 if self._bufferstates:
1125 self._bufferapplylabels = self._bufferstates[-1][2]
1125 self._bufferapplylabels = self._bufferstates[-1][2]
1126 else:
1126 else:
1127 self._bufferapplylabels = None
1127 self._bufferapplylabels = None
1128
1128
1129 return b"".join(self._buffers.pop())
1129 return b"".join(self._buffers.pop())
1130
1130
1131 def _isbuffered(self, dest):
1131 def _isbuffered(self, dest):
1132 if dest is self._fout:
1132 if dest is self._fout:
1133 return bool(self._buffers)
1133 return bool(self._buffers)
1134 if dest is self._ferr:
1134 if dest is self._ferr:
1135 return bool(self._bufferstates and self._bufferstates[-1][0])
1135 return bool(self._bufferstates and self._bufferstates[-1][0])
1136 return False
1136 return False
1137
1137
1138 def canwritewithoutlabels(self):
1138 def canwritewithoutlabels(self):
1139 '''check if write skips the label'''
1139 '''check if write skips the label'''
1140 if self._buffers and not self._bufferapplylabels:
1140 if self._buffers and not self._bufferapplylabels:
1141 return True
1141 return True
1142 return self._colormode is None
1142 return self._colormode is None
1143
1143
1144 def canbatchlabeledwrites(self):
1144 def canbatchlabeledwrites(self):
1145 '''check if write calls with labels are batchable'''
1145 '''check if write calls with labels are batchable'''
1146 # Windows color printing is special, see ``write``.
1146 # Windows color printing is special, see ``write``.
1147 return self._colormode != b'win32'
1147 return self._colormode != b'win32'
1148
1148
1149 def write(self, *args, **opts):
1149 def write(self, *args, **opts):
1150 """write args to output
1150 """write args to output
1151
1151
1152 By default, this method simply writes to the buffer or stdout.
1152 By default, this method simply writes to the buffer or stdout.
1153 Color mode can be set on the UI class to have the output decorated
1153 Color mode can be set on the UI class to have the output decorated
1154 with color modifier before being written to stdout.
1154 with color modifier before being written to stdout.
1155
1155
1156 The color used is controlled by an optional keyword argument, "label".
1156 The color used is controlled by an optional keyword argument, "label".
1157 This should be a string containing label names separated by space.
1157 This should be a string containing label names separated by space.
1158 Label names take the form of "topic.type". For example, ui.debug()
1158 Label names take the form of "topic.type". For example, ui.debug()
1159 issues a label of "ui.debug".
1159 issues a label of "ui.debug".
1160
1160
1161 Progress reports via stderr are normally cleared before writing as
1161 Progress reports via stderr are normally cleared before writing as
1162 stdout and stderr go to the same terminal. This can be skipped with
1162 stdout and stderr go to the same terminal. This can be skipped with
1163 the optional keyword argument "keepprogressbar". The progress bar
1163 the optional keyword argument "keepprogressbar". The progress bar
1164 will continue to occupy a partial line on stderr in that case.
1164 will continue to occupy a partial line on stderr in that case.
1165 This functionality is intended when Mercurial acts as data source
1165 This functionality is intended when Mercurial acts as data source
1166 in a pipe.
1166 in a pipe.
1167
1167
1168 When labeling output for a specific command, a label of
1168 When labeling output for a specific command, a label of
1169 "cmdname.type" is recommended. For example, status issues
1169 "cmdname.type" is recommended. For example, status issues
1170 a label of "status.modified" for modified files.
1170 a label of "status.modified" for modified files.
1171 """
1171 """
1172 dest = self._fout
1172 dest = self._fout
1173
1173
1174 # inlined _write() for speed
1174 # inlined _write() for speed
1175 if self._buffers:
1175 if self._buffers:
1176 label = opts.get('label', b'')
1176 label = opts.get('label', b'')
1177 if label and self._bufferapplylabels:
1177 if label and self._bufferapplylabels:
1178 self._buffers[-1].extend(self.label(a, label) for a in args)
1178 self._buffers[-1].extend(self.label(a, label) for a in args)
1179 else:
1179 else:
1180 self._buffers[-1].extend(args)
1180 self._buffers[-1].extend(args)
1181 return
1181 return
1182
1182
1183 # inlined _writenobuf() for speed
1183 # inlined _writenobuf() for speed
1184 if not opts.get('keepprogressbar', False):
1184 if not opts.get('keepprogressbar', False):
1185 self._progclear()
1185 self._progclear()
1186 msg = b''.join(args)
1186 msg = b''.join(args)
1187
1187
1188 # opencode timeblockedsection because this is a critical path
1188 # opencode timeblockedsection because this is a critical path
1189 starttime = util.timer()
1189 starttime = util.timer()
1190 try:
1190 try:
1191 if self._colormode == b'win32':
1191 if self._colormode == b'win32':
1192 # windows color printing is its own can of crab, defer to
1192 # windows color printing is its own can of crab, defer to
1193 # the color module and that is it.
1193 # the color module and that is it.
1194 color.win32print(self, dest.write, msg, **opts)
1194 color.win32print(self, dest.write, msg, **opts)
1195 else:
1195 else:
1196 if self._colormode is not None:
1196 if self._colormode is not None:
1197 label = opts.get('label', b'')
1197 label = opts.get('label', b'')
1198 msg = self.label(msg, label)
1198 msg = self.label(msg, label)
1199 dest.write(msg)
1199 dest.write(msg)
1200 except IOError as err:
1200 except IOError as err:
1201 raise error.StdioError(err)
1201 raise error.StdioError(err)
1202 finally:
1202 finally:
1203 self._blockedtimes[b'stdio_blocked'] += (
1203 self._blockedtimes[b'stdio_blocked'] += (
1204 util.timer() - starttime
1204 util.timer() - starttime
1205 ) * 1000
1205 ) * 1000
1206
1206
1207 def write_err(self, *args, **opts):
1207 def write_err(self, *args, **opts):
1208 self._write(self._ferr, *args, **opts)
1208 self._write(self._ferr, *args, **opts)
1209
1209
1210 def _write(self, dest, *args, **opts):
1210 def _write(self, dest, *args, **opts):
1211 # update write() as well if you touch this code
1211 # update write() as well if you touch this code
1212 if self._isbuffered(dest):
1212 if self._isbuffered(dest):
1213 label = opts.get('label', b'')
1213 label = opts.get('label', b'')
1214 if label and self._bufferapplylabels:
1214 if label and self._bufferapplylabels:
1215 self._buffers[-1].extend(self.label(a, label) for a in args)
1215 self._buffers[-1].extend(self.label(a, label) for a in args)
1216 else:
1216 else:
1217 self._buffers[-1].extend(args)
1217 self._buffers[-1].extend(args)
1218 else:
1218 else:
1219 self._writenobuf(dest, *args, **opts)
1219 self._writenobuf(dest, *args, **opts)
1220
1220
1221 def _writenobuf(self, dest, *args, **opts):
1221 def _writenobuf(self, dest, *args, **opts):
1222 # update write() as well if you touch this code
1222 # update write() as well if you touch this code
1223 if not opts.get('keepprogressbar', False):
1223 if not opts.get('keepprogressbar', False):
1224 self._progclear()
1224 self._progclear()
1225 msg = b''.join(args)
1225 msg = b''.join(args)
1226
1226
1227 # opencode timeblockedsection because this is a critical path
1227 # opencode timeblockedsection because this is a critical path
1228 starttime = util.timer()
1228 starttime = util.timer()
1229 try:
1229 try:
1230 if dest is self._ferr and not getattr(self._fout, 'closed', False):
1230 if dest is self._ferr and not getattr(self._fout, 'closed', False):
1231 self._fout.flush()
1231 self._fout.flush()
1232 if getattr(dest, 'structured', False):
1232 if getattr(dest, 'structured', False):
1233 # channel for machine-readable output with metadata, where
1233 # channel for machine-readable output with metadata, where
1234 # no extra colorization is necessary.
1234 # no extra colorization is necessary.
1235 dest.write(msg, **opts)
1235 dest.write(msg, **opts)
1236 elif self._colormode == b'win32':
1236 elif self._colormode == b'win32':
1237 # windows color printing is its own can of crab, defer to
1237 # windows color printing is its own can of crab, defer to
1238 # the color module and that is it.
1238 # the color module and that is it.
1239 color.win32print(self, dest.write, msg, **opts)
1239 color.win32print(self, dest.write, msg, **opts)
1240 else:
1240 else:
1241 if self._colormode is not None:
1241 if self._colormode is not None:
1242 label = opts.get('label', b'')
1242 label = opts.get('label', b'')
1243 msg = self.label(msg, label)
1243 msg = self.label(msg, label)
1244 dest.write(msg)
1244 dest.write(msg)
1245 # stderr may be buffered under win32 when redirected to files,
1245 # stderr may be buffered under win32 when redirected to files,
1246 # including stdout.
1246 # including stdout.
1247 if dest is self._ferr and not getattr(dest, 'closed', False):
1247 if dest is self._ferr and not getattr(dest, 'closed', False):
1248 dest.flush()
1248 dest.flush()
1249 except IOError as err:
1249 except IOError as err:
1250 if dest is self._ferr and err.errno in (
1250 if dest is self._ferr and err.errno in (
1251 errno.EPIPE,
1251 errno.EPIPE,
1252 errno.EIO,
1252 errno.EIO,
1253 errno.EBADF,
1253 errno.EBADF,
1254 ):
1254 ):
1255 # no way to report the error, so ignore it
1255 # no way to report the error, so ignore it
1256 return
1256 return
1257 raise error.StdioError(err)
1257 raise error.StdioError(err)
1258 finally:
1258 finally:
1259 self._blockedtimes[b'stdio_blocked'] += (
1259 self._blockedtimes[b'stdio_blocked'] += (
1260 util.timer() - starttime
1260 util.timer() - starttime
1261 ) * 1000
1261 ) * 1000
1262
1262
1263 def _writemsg(self, dest, *args, **opts):
1263 def _writemsg(self, dest, *args, **opts):
1264 timestamp = self.showtimestamp and opts.get('type') in {
1264 timestamp = self.showtimestamp and opts.get('type') in {
1265 b'debug',
1265 b'debug',
1266 b'error',
1266 b'error',
1267 b'note',
1267 b'note',
1268 b'status',
1268 b'status',
1269 b'warning',
1269 b'warning',
1270 }
1270 }
1271 if timestamp:
1271 if timestamp:
1272 args = (
1272 args = (
1273 b'[%s] '
1273 b'[%s] '
1274 % pycompat.bytestr(datetime.datetime.now().isoformat()),
1274 % pycompat.bytestr(datetime.datetime.now().isoformat()),
1275 ) + args
1275 ) + args
1276 _writemsgwith(self._write, dest, *args, **opts)
1276 _writemsgwith(self._write, dest, *args, **opts)
1277 if timestamp:
1277 if timestamp:
1278 dest.flush()
1278 dest.flush()
1279
1279
1280 def _writemsgnobuf(self, dest, *args, **opts):
1280 def _writemsgnobuf(self, dest, *args, **opts):
1281 _writemsgwith(self._writenobuf, dest, *args, **opts)
1281 _writemsgwith(self._writenobuf, dest, *args, **opts)
1282
1282
1283 def flush(self):
1283 def flush(self):
1284 # opencode timeblockedsection because this is a critical path
1284 # opencode timeblockedsection because this is a critical path
1285 starttime = util.timer()
1285 starttime = util.timer()
1286 try:
1286 try:
1287 try:
1287 try:
1288 self._fout.flush()
1288 self._fout.flush()
1289 except IOError as err:
1289 except IOError as err:
1290 if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
1290 if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
1291 raise error.StdioError(err)
1291 raise error.StdioError(err)
1292 finally:
1292 finally:
1293 try:
1293 try:
1294 self._ferr.flush()
1294 self._ferr.flush()
1295 except IOError as err:
1295 except IOError as err:
1296 if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
1296 if err.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
1297 raise error.StdioError(err)
1297 raise error.StdioError(err)
1298 finally:
1298 finally:
1299 self._blockedtimes[b'stdio_blocked'] += (
1299 self._blockedtimes[b'stdio_blocked'] += (
1300 util.timer() - starttime
1300 util.timer() - starttime
1301 ) * 1000
1301 ) * 1000
1302
1302
1303 def _isatty(self, fh):
1303 def _isatty(self, fh):
1304 if self.configbool(b'ui', b'nontty'):
1304 if self.configbool(b'ui', b'nontty'):
1305 return False
1305 return False
1306 return procutil.isatty(fh)
1306 return procutil.isatty(fh)
1307
1307
1308 def protectfinout(self):
1308 def protectfinout(self):
1309 """Duplicate ui streams and redirect original if they are stdio
1309 """Duplicate ui streams and redirect original if they are stdio
1310
1310
1311 Returns (fin, fout) which point to the original ui fds, but may be
1311 Returns (fin, fout) which point to the original ui fds, but may be
1312 copy of them. The returned streams can be considered "owned" in that
1312 copy of them. The returned streams can be considered "owned" in that
1313 print(), exec(), etc. never reach to them.
1313 print(), exec(), etc. never reach to them.
1314 """
1314 """
1315 if self._finoutredirected:
1315 if self._finoutredirected:
1316 # if already redirected, protectstdio() would just create another
1316 # if already redirected, protectstdio() would just create another
1317 # nullfd pair, which is equivalent to returning self._fin/_fout.
1317 # nullfd pair, which is equivalent to returning self._fin/_fout.
1318 return self._fin, self._fout
1318 return self._fin, self._fout
1319 fin, fout = procutil.protectstdio(self._fin, self._fout)
1319 fin, fout = procutil.protectstdio(self._fin, self._fout)
1320 self._finoutredirected = (fin, fout) != (self._fin, self._fout)
1320 self._finoutredirected = (fin, fout) != (self._fin, self._fout)
1321 return fin, fout
1321 return fin, fout
1322
1322
1323 def restorefinout(self, fin, fout):
1323 def restorefinout(self, fin, fout):
1324 """Restore ui streams from possibly duplicated (fin, fout)"""
1324 """Restore ui streams from possibly duplicated (fin, fout)"""
1325 if (fin, fout) == (self._fin, self._fout):
1325 if (fin, fout) == (self._fin, self._fout):
1326 return
1326 return
1327 procutil.restorestdio(self._fin, self._fout, fin, fout)
1327 procutil.restorestdio(self._fin, self._fout, fin, fout)
1328 # protectfinout() won't create more than one duplicated streams,
1328 # protectfinout() won't create more than one duplicated streams,
1329 # so we can just turn the redirection flag off.
1329 # so we can just turn the redirection flag off.
1330 self._finoutredirected = False
1330 self._finoutredirected = False
1331
1331
1332 @contextlib.contextmanager
1332 @contextlib.contextmanager
1333 def protectedfinout(self):
1333 def protectedfinout(self):
1334 """Run code block with protected standard streams"""
1334 """Run code block with protected standard streams"""
1335 fin, fout = self.protectfinout()
1335 fin, fout = self.protectfinout()
1336 try:
1336 try:
1337 yield fin, fout
1337 yield fin, fout
1338 finally:
1338 finally:
1339 self.restorefinout(fin, fout)
1339 self.restorefinout(fin, fout)
1340
1340
1341 def disablepager(self):
1341 def disablepager(self):
1342 self._disablepager = True
1342 self._disablepager = True
1343
1343
1344 def pager(self, command):
1344 def pager(self, command):
1345 """Start a pager for subsequent command output.
1345 """Start a pager for subsequent command output.
1346
1346
1347 Commands which produce a long stream of output should call
1347 Commands which produce a long stream of output should call
1348 this function to activate the user's preferred pagination
1348 this function to activate the user's preferred pagination
1349 mechanism (which may be no pager). Calling this function
1349 mechanism (which may be no pager). Calling this function
1350 precludes any future use of interactive functionality, such as
1350 precludes any future use of interactive functionality, such as
1351 prompting the user or activating curses.
1351 prompting the user or activating curses.
1352
1352
1353 Args:
1353 Args:
1354 command: The full, non-aliased name of the command. That is, "log"
1354 command: The full, non-aliased name of the command. That is, "log"
1355 not "history, "summary" not "summ", etc.
1355 not "history, "summary" not "summ", etc.
1356 """
1356 """
1357 if self._disablepager or self.pageractive:
1357 if self._disablepager or self.pageractive:
1358 # how pager should do is already determined
1358 # how pager should do is already determined
1359 return
1359 return
1360
1360
1361 if not command.startswith(b'internal-always-') and (
1361 if not command.startswith(b'internal-always-') and (
1362 # explicit --pager=on (= 'internal-always-' prefix) should
1362 # explicit --pager=on (= 'internal-always-' prefix) should
1363 # take precedence over disabling factors below
1363 # take precedence over disabling factors below
1364 command in self.configlist(b'pager', b'ignore')
1364 command in self.configlist(b'pager', b'ignore')
1365 or not self.configbool(b'ui', b'paginate')
1365 or not self.configbool(b'ui', b'paginate')
1366 or not self.configbool(b'pager', b'attend-' + command, True)
1366 or not self.configbool(b'pager', b'attend-' + command, True)
1367 or encoding.environ.get(b'TERM') == b'dumb'
1367 or encoding.environ.get(b'TERM') == b'dumb'
1368 # TODO: if we want to allow HGPLAINEXCEPT=pager,
1368 # TODO: if we want to allow HGPLAINEXCEPT=pager,
1369 # formatted() will need some adjustment.
1369 # formatted() will need some adjustment.
1370 or not self.formatted()
1370 or not self.formatted()
1371 or self.plain()
1371 or self.plain()
1372 or self._buffers
1372 or self._buffers
1373 # TODO: expose debugger-enabled on the UI object
1373 # TODO: expose debugger-enabled on the UI object
1374 or b'--debugger' in pycompat.sysargv
1374 or b'--debugger' in pycompat.sysargv
1375 ):
1375 ):
1376 # We only want to paginate if the ui appears to be
1376 # We only want to paginate if the ui appears to be
1377 # interactive, the user didn't say HGPLAIN or
1377 # interactive, the user didn't say HGPLAIN or
1378 # HGPLAINEXCEPT=pager, and the user didn't specify --debug.
1378 # HGPLAINEXCEPT=pager, and the user didn't specify --debug.
1379 return
1379 return
1380
1380
1381 pagercmd = self.config(b'pager', b'pager', rcutil.fallbackpager)
1381 pagercmd = self.config(b'pager', b'pager', rcutil.fallbackpager)
1382 if not pagercmd:
1382 if not pagercmd:
1383 return
1383 return
1384
1384
1385 pagerenv = {}
1385 pagerenv = {}
1386 for name, value in rcutil.defaultpagerenv().items():
1386 for name, value in rcutil.defaultpagerenv().items():
1387 if name not in encoding.environ:
1387 if name not in encoding.environ:
1388 pagerenv[name] = value
1388 pagerenv[name] = value
1389
1389
1390 self.debug(
1390 self.debug(
1391 b'starting pager for command %s\n' % stringutil.pprint(command)
1391 b'starting pager for command %s\n' % stringutil.pprint(command)
1392 )
1392 )
1393 self.flush()
1393 self.flush()
1394
1394
1395 wasformatted = self.formatted()
1395 wasformatted = self.formatted()
1396 if util.safehasattr(signal, b"SIGPIPE"):
1396 if util.safehasattr(signal, b"SIGPIPE"):
1397 signal.signal(signal.SIGPIPE, _catchterm)
1397 signal.signal(signal.SIGPIPE, _catchterm)
1398 if self._runpager(pagercmd, pagerenv):
1398 if self._runpager(pagercmd, pagerenv):
1399 self.pageractive = True
1399 self.pageractive = True
1400 # Preserve the formatted-ness of the UI. This is important
1400 # Preserve the formatted-ness of the UI. This is important
1401 # because we mess with stdout, which might confuse
1401 # because we mess with stdout, which might confuse
1402 # auto-detection of things being formatted.
1402 # auto-detection of things being formatted.
1403 self.setconfig(b'ui', b'formatted', wasformatted, b'pager')
1403 self.setconfig(b'ui', b'formatted', wasformatted, b'pager')
1404 self.setconfig(b'ui', b'interactive', False, b'pager')
1404 self.setconfig(b'ui', b'interactive', False, b'pager')
1405
1405
1406 # If pagermode differs from color.mode, reconfigure color now that
1406 # If pagermode differs from color.mode, reconfigure color now that
1407 # pageractive is set.
1407 # pageractive is set.
1408 cm = self._colormode
1408 cm = self._colormode
1409 if cm != self.config(b'color', b'pagermode', cm):
1409 if cm != self.config(b'color', b'pagermode', cm):
1410 color.setup(self)
1410 color.setup(self)
1411 else:
1411 else:
1412 # If the pager can't be spawned in dispatch when --pager=on is
1412 # If the pager can't be spawned in dispatch when --pager=on is
1413 # given, don't try again when the command runs, to avoid a duplicate
1413 # given, don't try again when the command runs, to avoid a duplicate
1414 # warning about a missing pager command.
1414 # warning about a missing pager command.
1415 self.disablepager()
1415 self.disablepager()
1416
1416
1417 def _runpager(self, command, env=None):
1417 def _runpager(self, command, env=None):
1418 """Actually start the pager and set up file descriptors.
1418 """Actually start the pager and set up file descriptors.
1419
1419
1420 This is separate in part so that extensions (like chg) can
1420 This is separate in part so that extensions (like chg) can
1421 override how a pager is invoked.
1421 override how a pager is invoked.
1422 """
1422 """
1423 if command == b'cat':
1423 if command == b'cat':
1424 # Save ourselves some work.
1424 # Save ourselves some work.
1425 return False
1425 return False
1426 # If the command doesn't contain any of these characters, we
1426 # If the command doesn't contain any of these characters, we
1427 # assume it's a binary and exec it directly. This means for
1427 # assume it's a binary and exec it directly. This means for
1428 # simple pager command configurations, we can degrade
1428 # simple pager command configurations, we can degrade
1429 # gracefully and tell the user about their broken pager.
1429 # gracefully and tell the user about their broken pager.
1430 shell = any(c in command for c in b"|&;<>()$`\\\"' \t\n*?[#~=%")
1430 shell = any(c in command for c in b"|&;<>()$`\\\"' \t\n*?[#~=%")
1431
1431
1432 if pycompat.iswindows and not shell:
1432 if pycompat.iswindows and not shell:
1433 # Window's built-in `more` cannot be invoked with shell=False, but
1433 # Window's built-in `more` cannot be invoked with shell=False, but
1434 # its `more.com` can. Hide this implementation detail from the
1434 # its `more.com` can. Hide this implementation detail from the
1435 # user so we can also get sane bad PAGER behavior. MSYS has
1435 # user so we can also get sane bad PAGER behavior. MSYS has
1436 # `more.exe`, so do a cmd.exe style resolution of the executable to
1436 # `more.exe`, so do a cmd.exe style resolution of the executable to
1437 # determine which one to use.
1437 # determine which one to use.
1438 fullcmd = procutil.findexe(command)
1438 fullcmd = procutil.findexe(command)
1439 if not fullcmd:
1439 if not fullcmd:
1440 self.warn(
1440 self.warn(
1441 _(b"missing pager command '%s', skipping pager\n") % command
1441 _(b"missing pager command '%s', skipping pager\n") % command
1442 )
1442 )
1443 return False
1443 return False
1444
1444
1445 command = fullcmd
1445 command = fullcmd
1446
1446
1447 try:
1447 try:
1448 pager = subprocess.Popen(
1448 pager = subprocess.Popen(
1449 procutil.tonativestr(command),
1449 procutil.tonativestr(command),
1450 shell=shell,
1450 shell=shell,
1451 bufsize=-1,
1451 bufsize=-1,
1452 close_fds=procutil.closefds,
1452 close_fds=procutil.closefds,
1453 stdin=subprocess.PIPE,
1453 stdin=subprocess.PIPE,
1454 stdout=procutil.stdout,
1454 stdout=procutil.stdout,
1455 stderr=procutil.stderr,
1455 stderr=procutil.stderr,
1456 env=procutil.tonativeenv(procutil.shellenviron(env)),
1456 env=procutil.tonativeenv(procutil.shellenviron(env)),
1457 )
1457 )
1458 except OSError as e:
1458 except OSError as e:
1459 if e.errno == errno.ENOENT and not shell:
1459 if e.errno == errno.ENOENT and not shell:
1460 self.warn(
1460 self.warn(
1461 _(b"missing pager command '%s', skipping pager\n") % command
1461 _(b"missing pager command '%s', skipping pager\n") % command
1462 )
1462 )
1463 return False
1463 return False
1464 raise
1464 raise
1465
1465
1466 # back up original file descriptors
1466 # back up original file descriptors
1467 stdoutfd = os.dup(procutil.stdout.fileno())
1467 stdoutfd = os.dup(procutil.stdout.fileno())
1468 stderrfd = os.dup(procutil.stderr.fileno())
1468 stderrfd = os.dup(procutil.stderr.fileno())
1469
1469
1470 os.dup2(pager.stdin.fileno(), procutil.stdout.fileno())
1470 os.dup2(pager.stdin.fileno(), procutil.stdout.fileno())
1471 if self._isatty(procutil.stderr):
1471 if self._isatty(procutil.stderr):
1472 os.dup2(pager.stdin.fileno(), procutil.stderr.fileno())
1472 os.dup2(pager.stdin.fileno(), procutil.stderr.fileno())
1473
1473
1474 @self.atexit
1474 @self.atexit
1475 def killpager():
1475 def killpager():
1476 if util.safehasattr(signal, b"SIGINT"):
1476 if util.safehasattr(signal, b"SIGINT"):
1477 signal.signal(signal.SIGINT, signal.SIG_IGN)
1477 signal.signal(signal.SIGINT, signal.SIG_IGN)
1478 # restore original fds, closing pager.stdin copies in the process
1478 # restore original fds, closing pager.stdin copies in the process
1479 os.dup2(stdoutfd, procutil.stdout.fileno())
1479 os.dup2(stdoutfd, procutil.stdout.fileno())
1480 os.dup2(stderrfd, procutil.stderr.fileno())
1480 os.dup2(stderrfd, procutil.stderr.fileno())
1481 pager.stdin.close()
1481 pager.stdin.close()
1482 pager.wait()
1482 pager.wait()
1483
1483
1484 return True
1484 return True
1485
1485
1486 @property
1486 @property
1487 def _exithandlers(self):
1487 def _exithandlers(self):
1488 return _reqexithandlers
1488 return _reqexithandlers
1489
1489
1490 def atexit(self, func, *args, **kwargs):
1490 def atexit(self, func, *args, **kwargs):
1491 """register a function to run after dispatching a request
1491 """register a function to run after dispatching a request
1492
1492
1493 Handlers do not stay registered across request boundaries."""
1493 Handlers do not stay registered across request boundaries."""
1494 self._exithandlers.append((func, args, kwargs))
1494 self._exithandlers.append((func, args, kwargs))
1495 return func
1495 return func
1496
1496
1497 def interface(self, feature):
1497 def interface(self, feature):
1498 """what interface to use for interactive console features?
1498 """what interface to use for interactive console features?
1499
1499
1500 The interface is controlled by the value of `ui.interface` but also by
1500 The interface is controlled by the value of `ui.interface` but also by
1501 the value of feature-specific configuration. For example:
1501 the value of feature-specific configuration. For example:
1502
1502
1503 ui.interface.histedit = text
1503 ui.interface.histedit = text
1504 ui.interface.chunkselector = curses
1504 ui.interface.chunkselector = curses
1505
1505
1506 Here the features are "histedit" and "chunkselector".
1506 Here the features are "histedit" and "chunkselector".
1507
1507
1508 The configuration above means that the default interfaces for commands
1508 The configuration above means that the default interfaces for commands
1509 is curses, the interface for histedit is text and the interface for
1509 is curses, the interface for histedit is text and the interface for
1510 selecting chunk is crecord (the best curses interface available).
1510 selecting chunk is crecord (the best curses interface available).
1511
1511
1512 Consider the following example:
1512 Consider the following example:
1513 ui.interface = curses
1513 ui.interface = curses
1514 ui.interface.histedit = text
1514 ui.interface.histedit = text
1515
1515
1516 Then histedit will use the text interface and chunkselector will use
1516 Then histedit will use the text interface and chunkselector will use
1517 the default curses interface (crecord at the moment).
1517 the default curses interface (crecord at the moment).
1518 """
1518 """
1519 alldefaults = frozenset([b"text", b"curses"])
1519 alldefaults = frozenset([b"text", b"curses"])
1520
1520
1521 featureinterfaces = {
1521 featureinterfaces = {
1522 b"chunkselector": [
1522 b"chunkselector": [
1523 b"text",
1523 b"text",
1524 b"curses",
1524 b"curses",
1525 ],
1525 ],
1526 b"histedit": [
1526 b"histedit": [
1527 b"text",
1527 b"text",
1528 b"curses",
1528 b"curses",
1529 ],
1529 ],
1530 }
1530 }
1531
1531
1532 # Feature-specific interface
1532 # Feature-specific interface
1533 if feature not in featureinterfaces.keys():
1533 if feature not in featureinterfaces.keys():
1534 # Programming error, not user error
1534 # Programming error, not user error
1535 raise ValueError(b"Unknown feature requested %s" % feature)
1535 raise ValueError(b"Unknown feature requested %s" % feature)
1536
1536
1537 availableinterfaces = frozenset(featureinterfaces[feature])
1537 availableinterfaces = frozenset(featureinterfaces[feature])
1538 if alldefaults > availableinterfaces:
1538 if alldefaults > availableinterfaces:
1539 # Programming error, not user error. We need a use case to
1539 # Programming error, not user error. We need a use case to
1540 # define the right thing to do here.
1540 # define the right thing to do here.
1541 raise ValueError(
1541 raise ValueError(
1542 b"Feature %s does not handle all default interfaces" % feature
1542 b"Feature %s does not handle all default interfaces" % feature
1543 )
1543 )
1544
1544
1545 if self.plain() or encoding.environ.get(b'TERM') == b'dumb':
1545 if self.plain() or encoding.environ.get(b'TERM') == b'dumb':
1546 return b"text"
1546 return b"text"
1547
1547
1548 # Default interface for all the features
1548 # Default interface for all the features
1549 defaultinterface = b"text"
1549 defaultinterface = b"text"
1550 i = self.config(b"ui", b"interface")
1550 i = self.config(b"ui", b"interface")
1551 if i in alldefaults:
1551 if i in alldefaults:
1552 defaultinterface = i
1552 defaultinterface = i
1553
1553
1554 choseninterface = defaultinterface
1554 choseninterface = defaultinterface
1555 f = self.config(b"ui", b"interface.%s" % feature)
1555 f = self.config(b"ui", b"interface.%s" % feature)
1556 if f in availableinterfaces:
1556 if f in availableinterfaces:
1557 choseninterface = f
1557 choseninterface = f
1558
1558
1559 if i is not None and defaultinterface != i:
1559 if i is not None and defaultinterface != i:
1560 if f is not None:
1560 if f is not None:
1561 self.warn(_(b"invalid value for ui.interface: %s\n") % (i,))
1561 self.warn(_(b"invalid value for ui.interface: %s\n") % (i,))
1562 else:
1562 else:
1563 self.warn(
1563 self.warn(
1564 _(b"invalid value for ui.interface: %s (using %s)\n")
1564 _(b"invalid value for ui.interface: %s (using %s)\n")
1565 % (i, choseninterface)
1565 % (i, choseninterface)
1566 )
1566 )
1567 if f is not None and choseninterface != f:
1567 if f is not None and choseninterface != f:
1568 self.warn(
1568 self.warn(
1569 _(b"invalid value for ui.interface.%s: %s (using %s)\n")
1569 _(b"invalid value for ui.interface.%s: %s (using %s)\n")
1570 % (feature, f, choseninterface)
1570 % (feature, f, choseninterface)
1571 )
1571 )
1572
1572
1573 return choseninterface
1573 return choseninterface
1574
1574
1575 def interactive(self):
1575 def interactive(self):
1576 """is interactive input allowed?
1576 """is interactive input allowed?
1577
1577
1578 An interactive session is a session where input can be reasonably read
1578 An interactive session is a session where input can be reasonably read
1579 from `sys.stdin'. If this function returns false, any attempt to read
1579 from `sys.stdin'. If this function returns false, any attempt to read
1580 from stdin should fail with an error, unless a sensible default has been
1580 from stdin should fail with an error, unless a sensible default has been
1581 specified.
1581 specified.
1582
1582
1583 Interactiveness is triggered by the value of the `ui.interactive'
1583 Interactiveness is triggered by the value of the `ui.interactive'
1584 configuration variable or - if it is unset - when `sys.stdin' points
1584 configuration variable or - if it is unset - when `sys.stdin' points
1585 to a terminal device.
1585 to a terminal device.
1586
1586
1587 This function refers to input only; for output, see `ui.formatted()'.
1587 This function refers to input only; for output, see `ui.formatted()'.
1588 """
1588 """
1589 i = self.configbool(b"ui", b"interactive")
1589 i = self.configbool(b"ui", b"interactive")
1590 if i is None:
1590 if i is None:
1591 # some environments replace stdin without implementing isatty
1591 # some environments replace stdin without implementing isatty
1592 # usually those are non-interactive
1592 # usually those are non-interactive
1593 return self._isatty(self._fin)
1593 return self._isatty(self._fin)
1594
1594
1595 return i
1595 return i
1596
1596
1597 def termwidth(self):
1597 def termwidth(self):
1598 """how wide is the terminal in columns?"""
1598 """how wide is the terminal in columns?"""
1599 if b'COLUMNS' in encoding.environ:
1599 if b'COLUMNS' in encoding.environ:
1600 try:
1600 try:
1601 return int(encoding.environ[b'COLUMNS'])
1601 return int(encoding.environ[b'COLUMNS'])
1602 except ValueError:
1602 except ValueError:
1603 pass
1603 pass
1604 return scmutil.termsize(self)[0]
1604 return scmutil.termsize(self)[0]
1605
1605
1606 def formatted(self):
1606 def formatted(self):
1607 """should formatted output be used?
1607 """should formatted output be used?
1608
1608
1609 It is often desirable to format the output to suite the output medium.
1609 It is often desirable to format the output to suite the output medium.
1610 Examples of this are truncating long lines or colorizing messages.
1610 Examples of this are truncating long lines or colorizing messages.
1611 However, this is not often not desirable when piping output into other
1611 However, this is not often not desirable when piping output into other
1612 utilities, e.g. `grep'.
1612 utilities, e.g. `grep'.
1613
1613
1614 Formatted output is triggered by the value of the `ui.formatted'
1614 Formatted output is triggered by the value of the `ui.formatted'
1615 configuration variable or - if it is unset - when `sys.stdout' points
1615 configuration variable or - if it is unset - when `sys.stdout' points
1616 to a terminal device. Please note that `ui.formatted' should be
1616 to a terminal device. Please note that `ui.formatted' should be
1617 considered an implementation detail; it is not intended for use outside
1617 considered an implementation detail; it is not intended for use outside
1618 Mercurial or its extensions.
1618 Mercurial or its extensions.
1619
1619
1620 This function refers to output only; for input, see `ui.interactive()'.
1620 This function refers to output only; for input, see `ui.interactive()'.
1621 This function always returns false when in plain mode, see `ui.plain()'.
1621 This function always returns false when in plain mode, see `ui.plain()'.
1622 """
1622 """
1623 if self.plain():
1623 if self.plain():
1624 return False
1624 return False
1625
1625
1626 i = self.configbool(b"ui", b"formatted")
1626 i = self.configbool(b"ui", b"formatted")
1627 if i is None:
1627 if i is None:
1628 # some environments replace stdout without implementing isatty
1628 # some environments replace stdout without implementing isatty
1629 # usually those are non-interactive
1629 # usually those are non-interactive
1630 return self._isatty(self._fout)
1630 return self._isatty(self._fout)
1631
1631
1632 return i
1632 return i
1633
1633
1634 def _readline(self, prompt=b' ', promptopts=None):
1634 def _readline(self, prompt=b' ', promptopts=None):
1635 # Replacing stdin/stdout temporarily is a hard problem on Python 3
1635 # Replacing stdin/stdout temporarily is a hard problem on Python 3
1636 # because they have to be text streams with *no buffering*. Instead,
1636 # because they have to be text streams with *no buffering*. Instead,
1637 # we use rawinput() only if call_readline() will be invoked by
1637 # we use rawinput() only if call_readline() will be invoked by
1638 # PyOS_Readline(), so no I/O will be made at Python layer.
1638 # PyOS_Readline(), so no I/O will be made at Python layer.
1639 usereadline = (
1639 usereadline = (
1640 self._isatty(self._fin)
1640 self._isatty(self._fin)
1641 and self._isatty(self._fout)
1641 and self._isatty(self._fout)
1642 and procutil.isstdin(self._fin)
1642 and procutil.isstdin(self._fin)
1643 and procutil.isstdout(self._fout)
1643 and procutil.isstdout(self._fout)
1644 )
1644 )
1645 if usereadline:
1645 if usereadline:
1646 try:
1646 try:
1647 # magically add command line editing support, where
1647 # magically add command line editing support, where
1648 # available
1648 # available
1649 import readline
1649 import readline
1650
1650
1651 # force demandimport to really load the module
1651 # force demandimport to really load the module
1652 readline.read_history_file
1652 readline.read_history_file
1653 # windows sometimes raises something other than ImportError
1653 # windows sometimes raises something other than ImportError
1654 except Exception:
1654 except Exception:
1655 usereadline = False
1655 usereadline = False
1656
1656
1657 if self._colormode == b'win32' or not usereadline:
1657 if self._colormode == b'win32' or not usereadline:
1658 if not promptopts:
1658 if not promptopts:
1659 promptopts = {}
1659 promptopts = {}
1660 self._writemsgnobuf(
1660 self._writemsgnobuf(
1661 self._fmsgout, prompt, type=b'prompt', **promptopts
1661 self._fmsgout, prompt, type=b'prompt', **promptopts
1662 )
1662 )
1663 self.flush()
1663 self.flush()
1664 prompt = b' '
1664 prompt = b' '
1665 else:
1665 else:
1666 prompt = self.label(prompt, b'ui.prompt') + b' '
1666 prompt = self.label(prompt, b'ui.prompt') + b' '
1667
1667
1668 # prompt ' ' must exist; otherwise readline may delete entire line
1668 # prompt ' ' must exist; otherwise readline may delete entire line
1669 # - http://bugs.python.org/issue12833
1669 # - http://bugs.python.org/issue12833
1670 with self.timeblockedsection(b'stdio'):
1670 with self.timeblockedsection(b'stdio'):
1671 if usereadline:
1671 if usereadline:
1672 self.flush()
1672 self.flush()
1673 prompt = encoding.strfromlocal(prompt)
1673 prompt = encoding.strfromlocal(prompt)
1674 line = encoding.strtolocal(pycompat.rawinput(prompt))
1674 line = encoding.strtolocal(pycompat.rawinput(prompt))
1675 # When stdin is in binary mode on Windows, it can cause
1675 # When stdin is in binary mode on Windows, it can cause
1676 # raw_input() to emit an extra trailing carriage return
1676 # raw_input() to emit an extra trailing carriage return
1677 if pycompat.oslinesep == b'\r\n' and line.endswith(b'\r'):
1677 if pycompat.oslinesep == b'\r\n' and line.endswith(b'\r'):
1678 line = line[:-1]
1678 line = line[:-1]
1679 else:
1679 else:
1680 self._fout.write(pycompat.bytestr(prompt))
1680 self._fout.write(pycompat.bytestr(prompt))
1681 self._fout.flush()
1681 self._fout.flush()
1682 line = self._fin.readline()
1682 line = self._fin.readline()
1683 if not line:
1683 if not line:
1684 raise EOFError
1684 raise EOFError
1685 line = line.rstrip(pycompat.oslinesep)
1685 line = line.rstrip(pycompat.oslinesep)
1686
1686
1687 return line
1687 return line
1688
1688
1689 def prompt(self, msg, default=b"y"):
1689 def prompt(self, msg, default=b"y"):
1690 """Prompt user with msg, read response.
1690 """Prompt user with msg, read response.
1691 If ui is not interactive, the default is returned.
1691 If ui is not interactive, the default is returned.
1692 """
1692 """
1693 return self._prompt(msg, default=default)
1693 return self._prompt(msg, default=default)
1694
1694
1695 def _prompt(self, msg, **opts):
1695 def _prompt(self, msg, **opts):
1696 default = opts['default']
1696 default = opts['default']
1697 if not self.interactive():
1697 if not self.interactive():
1698 self._writemsg(self._fmsgout, msg, b' ', type=b'prompt', **opts)
1698 self._writemsg(self._fmsgout, msg, b' ', type=b'prompt', **opts)
1699 self._writemsg(
1699 self._writemsg(
1700 self._fmsgout, default or b'', b"\n", type=b'promptecho'
1700 self._fmsgout, default or b'', b"\n", type=b'promptecho'
1701 )
1701 )
1702 return default
1702 return default
1703 try:
1703 try:
1704 r = self._readline(prompt=msg, promptopts=opts)
1704 r = self._readline(prompt=msg, promptopts=opts)
1705 if not r:
1705 if not r:
1706 r = default
1706 r = default
1707 if self.configbool(b'ui', b'promptecho'):
1707 if self.configbool(b'ui', b'promptecho'):
1708 self._writemsg(
1708 self._writemsg(
1709 self._fmsgout, r or b'', b"\n", type=b'promptecho'
1709 self._fmsgout, r or b'', b"\n", type=b'promptecho'
1710 )
1710 )
1711 return r
1711 return r
1712 except EOFError:
1712 except EOFError:
1713 raise error.ResponseExpected()
1713 raise error.ResponseExpected()
1714
1714
1715 @staticmethod
1715 @staticmethod
1716 def extractchoices(prompt):
1716 def extractchoices(prompt):
1717 """Extract prompt message and list of choices from specified prompt.
1717 """Extract prompt message and list of choices from specified prompt.
1718
1718
1719 This returns tuple "(message, choices)", and "choices" is the
1719 This returns tuple "(message, choices)", and "choices" is the
1720 list of tuple "(response character, text without &)".
1720 list of tuple "(response character, text without &)".
1721
1721
1722 >>> ui.extractchoices(b"awake? $$ &Yes $$ &No")
1722 >>> ui.extractchoices(b"awake? $$ &Yes $$ &No")
1723 ('awake? ', [('y', 'Yes'), ('n', 'No')])
1723 ('awake? ', [('y', 'Yes'), ('n', 'No')])
1724 >>> ui.extractchoices(b"line\\nbreak? $$ &Yes $$ &No")
1724 >>> ui.extractchoices(b"line\\nbreak? $$ &Yes $$ &No")
1725 ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
1725 ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
1726 >>> ui.extractchoices(b"want lots of $$money$$?$$Ye&s$$N&o")
1726 >>> ui.extractchoices(b"want lots of $$money$$?$$Ye&s$$N&o")
1727 ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
1727 ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
1728 """
1728 """
1729
1729
1730 # Sadly, the prompt string may have been built with a filename
1730 # Sadly, the prompt string may have been built with a filename
1731 # containing "$$" so let's try to find the first valid-looking
1731 # containing "$$" so let's try to find the first valid-looking
1732 # prompt to start parsing. Sadly, we also can't rely on
1732 # prompt to start parsing. Sadly, we also can't rely on
1733 # choices containing spaces, ASCII, or basically anything
1733 # choices containing spaces, ASCII, or basically anything
1734 # except an ampersand followed by a character.
1734 # except an ampersand followed by a character.
1735 m = re.match(br'(?s)(.+?)\$\$([^$]*&[^ $].*)', prompt)
1735 m = re.match(br'(?s)(.+?)\$\$([^$]*&[^ $].*)', prompt)
1736 msg = m.group(1)
1736 msg = m.group(1)
1737 choices = [p.strip(b' ') for p in m.group(2).split(b'$$')]
1737 choices = [p.strip(b' ') for p in m.group(2).split(b'$$')]
1738
1738
1739 def choicetuple(s):
1739 def choicetuple(s):
1740 ampidx = s.index(b'&')
1740 ampidx = s.index(b'&')
1741 return s[ampidx + 1 : ampidx + 2].lower(), s.replace(b'&', b'', 1)
1741 return s[ampidx + 1 : ampidx + 2].lower(), s.replace(b'&', b'', 1)
1742
1742
1743 return (msg, [choicetuple(s) for s in choices])
1743 return (msg, [choicetuple(s) for s in choices])
1744
1744
1745 def promptchoice(self, prompt, default=0):
1745 def promptchoice(self, prompt, default=0):
1746 """Prompt user with a message, read response, and ensure it matches
1746 """Prompt user with a message, read response, and ensure it matches
1747 one of the provided choices. The prompt is formatted as follows:
1747 one of the provided choices. The prompt is formatted as follows:
1748
1748
1749 "would you like fries with that (Yn)? $$ &Yes $$ &No"
1749 "would you like fries with that (Yn)? $$ &Yes $$ &No"
1750
1750
1751 The index of the choice is returned. Responses are case
1751 The index of the choice is returned. Responses are case
1752 insensitive. If ui is not interactive, the default is
1752 insensitive. If ui is not interactive, the default is
1753 returned.
1753 returned.
1754 """
1754 """
1755
1755
1756 msg, choices = self.extractchoices(prompt)
1756 msg, choices = self.extractchoices(prompt)
1757 resps = [r for r, t in choices]
1757 resps = [r for r, t in choices]
1758 while True:
1758 while True:
1759 r = self._prompt(msg, default=resps[default], choices=choices)
1759 r = self._prompt(msg, default=resps[default], choices=choices)
1760 if r.lower() in resps:
1760 if r.lower() in resps:
1761 return resps.index(r.lower())
1761 return resps.index(r.lower())
1762 # TODO: shouldn't it be a warning?
1762 # TODO: shouldn't it be a warning?
1763 self._writemsg(self._fmsgout, _(b"unrecognized response\n"))
1763 self._writemsg(self._fmsgout, _(b"unrecognized response\n"))
1764
1764
1765 def getpass(self, prompt=None, default=None):
1765 def getpass(self, prompt=None, default=None):
1766 if not self.interactive():
1766 if not self.interactive():
1767 return default
1767 return default
1768 try:
1768 try:
1769 self._writemsg(
1769 self._writemsg(
1770 self._fmsgerr,
1770 self._fmsgerr,
1771 prompt or _(b'password: '),
1771 prompt or _(b'password: '),
1772 type=b'prompt',
1772 type=b'prompt',
1773 password=True,
1773 password=True,
1774 )
1774 )
1775 # disable getpass() only if explicitly specified. it's still valid
1775 # disable getpass() only if explicitly specified. it's still valid
1776 # to interact with tty even if fin is not a tty.
1776 # to interact with tty even if fin is not a tty.
1777 with self.timeblockedsection(b'stdio'):
1777 with self.timeblockedsection(b'stdio'):
1778 if self.configbool(b'ui', b'nontty'):
1778 if self.configbool(b'ui', b'nontty'):
1779 l = self._fin.readline()
1779 l = self._fin.readline()
1780 if not l:
1780 if not l:
1781 raise EOFError
1781 raise EOFError
1782 return l.rstrip(b'\n')
1782 return l.rstrip(b'\n')
1783 else:
1783 else:
1784 return encoding.strtolocal(getpass.getpass(''))
1784 return encoding.strtolocal(getpass.getpass(''))
1785 except EOFError:
1785 except EOFError:
1786 raise error.ResponseExpected()
1786 raise error.ResponseExpected()
1787
1787
1788 def status(self, *msg, **opts):
1788 def status(self, *msg, **opts):
1789 """write status message to output (if ui.quiet is False)
1789 """write status message to output (if ui.quiet is False)
1790
1790
1791 This adds an output label of "ui.status".
1791 This adds an output label of "ui.status".
1792 """
1792 """
1793 if not self.quiet:
1793 if not self.quiet:
1794 self._writemsg(self._fmsgout, type=b'status', *msg, **opts)
1794 self._writemsg(self._fmsgout, type=b'status', *msg, **opts)
1795
1795
1796 def warn(self, *msg, **opts):
1796 def warn(self, *msg, **opts):
1797 """write warning message to output (stderr)
1797 """write warning message to output (stderr)
1798
1798
1799 This adds an output label of "ui.warning".
1799 This adds an output label of "ui.warning".
1800 """
1800 """
1801 self._writemsg(self._fmsgerr, type=b'warning', *msg, **opts)
1801 self._writemsg(self._fmsgerr, type=b'warning', *msg, **opts)
1802
1802
1803 def error(self, *msg, **opts):
1803 def error(self, *msg, **opts):
1804 """write error message to output (stderr)
1804 """write error message to output (stderr)
1805
1805
1806 This adds an output label of "ui.error".
1806 This adds an output label of "ui.error".
1807 """
1807 """
1808 self._writemsg(self._fmsgerr, type=b'error', *msg, **opts)
1808 self._writemsg(self._fmsgerr, type=b'error', *msg, **opts)
1809
1809
1810 def note(self, *msg, **opts):
1810 def note(self, *msg, **opts):
1811 """write note to output (if ui.verbose is True)
1811 """write note to output (if ui.verbose is True)
1812
1812
1813 This adds an output label of "ui.note".
1813 This adds an output label of "ui.note".
1814 """
1814 """
1815 if self.verbose:
1815 if self.verbose:
1816 self._writemsg(self._fmsgout, type=b'note', *msg, **opts)
1816 self._writemsg(self._fmsgout, type=b'note', *msg, **opts)
1817
1817
1818 def debug(self, *msg, **opts):
1818 def debug(self, *msg, **opts):
1819 """write debug message to output (if ui.debugflag is True)
1819 """write debug message to output (if ui.debugflag is True)
1820
1820
1821 This adds an output label of "ui.debug".
1821 This adds an output label of "ui.debug".
1822 """
1822 """
1823 if self.debugflag:
1823 if self.debugflag:
1824 self._writemsg(self._fmsgout, type=b'debug', *msg, **opts)
1824 self._writemsg(self._fmsgout, type=b'debug', *msg, **opts)
1825 self.log(b'debug', b'%s', b''.join(msg))
1825 self.log(b'debug', b'%s', b''.join(msg))
1826
1826
1827 # Aliases to defeat check-code.
1827 # Aliases to defeat check-code.
1828 statusnoi18n = status
1828 statusnoi18n = status
1829 notenoi18n = note
1829 notenoi18n = note
1830 warnnoi18n = warn
1830 warnnoi18n = warn
1831 writenoi18n = write
1831 writenoi18n = write
1832
1832
1833 def edit(
1833 def edit(
1834 self,
1834 self,
1835 text,
1835 text,
1836 user,
1836 user,
1837 extra=None,
1837 extra=None,
1838 editform=None,
1838 editform=None,
1839 pending=None,
1839 pending=None,
1840 repopath=None,
1840 repopath=None,
1841 action=None,
1841 action=None,
1842 ):
1842 ):
1843 if action is None:
1843 if action is None:
1844 self.develwarn(
1844 self.develwarn(
1845 b'action is None but will soon be a required '
1845 b'action is None but will soon be a required '
1846 b'parameter to ui.edit()'
1846 b'parameter to ui.edit()'
1847 )
1847 )
1848 extra_defaults = {
1848 extra_defaults = {
1849 b'prefix': b'editor',
1849 b'prefix': b'editor',
1850 b'suffix': b'.txt',
1850 b'suffix': b'.txt',
1851 }
1851 }
1852 if extra is not None:
1852 if extra is not None:
1853 if extra.get(b'suffix') is not None:
1853 if extra.get(b'suffix') is not None:
1854 self.develwarn(
1854 self.develwarn(
1855 b'extra.suffix is not None but will soon be '
1855 b'extra.suffix is not None but will soon be '
1856 b'ignored by ui.edit()'
1856 b'ignored by ui.edit()'
1857 )
1857 )
1858 extra_defaults.update(extra)
1858 extra_defaults.update(extra)
1859 extra = extra_defaults
1859 extra = extra_defaults
1860
1860
1861 if action == b'diff':
1861 if action == b'diff':
1862 suffix = b'.diff'
1862 suffix = b'.diff'
1863 elif action:
1863 elif action:
1864 suffix = b'.%s.hg.txt' % action
1864 suffix = b'.%s.hg.txt' % action
1865 else:
1865 else:
1866 suffix = extra[b'suffix']
1866 suffix = extra[b'suffix']
1867
1867
1868 rdir = None
1868 rdir = None
1869 if self.configbool(b'experimental', b'editortmpinhg'):
1869 if self.configbool(b'experimental', b'editortmpinhg'):
1870 rdir = repopath
1870 rdir = repopath
1871 (fd, name) = pycompat.mkstemp(
1871 (fd, name) = pycompat.mkstemp(
1872 prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir
1872 prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir
1873 )
1873 )
1874 try:
1874 try:
1875 with os.fdopen(fd, 'wb') as f:
1875 with os.fdopen(fd, 'wb') as f:
1876 f.write(util.tonativeeol(text))
1876 f.write(util.tonativeeol(text))
1877
1877
1878 environ = {b'HGUSER': user}
1878 environ = {b'HGUSER': user}
1879 if b'transplant_source' in extra:
1879 if b'transplant_source' in extra:
1880 environ.update(
1880 environ.update(
1881 {b'HGREVISION': hex(extra[b'transplant_source'])}
1881 {b'HGREVISION': hex(extra[b'transplant_source'])}
1882 )
1882 )
1883 for label in (b'intermediate-source', b'source', b'rebase_source'):
1883 for label in (b'intermediate-source', b'source', b'rebase_source'):
1884 if label in extra:
1884 if label in extra:
1885 environ.update({b'HGREVISION': extra[label]})
1885 environ.update({b'HGREVISION': extra[label]})
1886 break
1886 break
1887 if editform:
1887 if editform:
1888 environ.update({b'HGEDITFORM': editform})
1888 environ.update({b'HGEDITFORM': editform})
1889 if pending:
1889 if pending:
1890 environ.update({b'HG_PENDING': pending})
1890 environ.update({b'HG_PENDING': pending})
1891
1891
1892 editor = self.geteditor()
1892 editor = self.geteditor()
1893
1893
1894 self.system(
1894 self.system(
1895 b"%s \"%s\"" % (editor, name),
1895 b"%s \"%s\"" % (editor, name),
1896 environ=environ,
1896 environ=environ,
1897 onerr=error.CanceledError,
1897 onerr=error.CanceledError,
1898 errprefix=_(b"edit failed"),
1898 errprefix=_(b"edit failed"),
1899 blockedtag=b'editor',
1899 blockedtag=b'editor',
1900 )
1900 )
1901
1901
1902 with open(name, 'rb') as f:
1902 with open(name, 'rb') as f:
1903 t = util.fromnativeeol(f.read())
1903 t = util.fromnativeeol(f.read())
1904 finally:
1904 finally:
1905 os.unlink(name)
1905 os.unlink(name)
1906
1906
1907 return t
1907 return t
1908
1908
1909 def system(
1909 def system(
1910 self,
1910 self,
1911 cmd,
1911 cmd,
1912 environ=None,
1912 environ=None,
1913 cwd=None,
1913 cwd=None,
1914 onerr=None,
1914 onerr=None,
1915 errprefix=None,
1915 errprefix=None,
1916 blockedtag=None,
1916 blockedtag=None,
1917 ):
1917 ):
1918 """execute shell command with appropriate output stream. command
1918 """execute shell command with appropriate output stream. command
1919 output will be redirected if fout is not stdout.
1919 output will be redirected if fout is not stdout.
1920
1920
1921 if command fails and onerr is None, return status, else raise onerr
1921 if command fails and onerr is None, return status, else raise onerr
1922 object as exception.
1922 object as exception.
1923 """
1923 """
1924 if blockedtag is None:
1924 if blockedtag is None:
1925 # Long cmds tend to be because of an absolute path on cmd. Keep
1925 # Long cmds tend to be because of an absolute path on cmd. Keep
1926 # the tail end instead
1926 # the tail end instead
1927 cmdsuffix = cmd.translate(None, _keepalnum)[-85:]
1927 cmdsuffix = cmd.translate(None, _keepalnum)[-85:]
1928 blockedtag = b'unknown_system_' + cmdsuffix
1928 blockedtag = b'unknown_system_' + cmdsuffix
1929 out = self._fout
1929 out = self._fout
1930 if any(s[1] for s in self._bufferstates):
1930 if any(s[1] for s in self._bufferstates):
1931 out = self
1931 out = self
1932 with self.timeblockedsection(blockedtag):
1932 with self.timeblockedsection(blockedtag):
1933 rc = self._runsystem(cmd, environ=environ, cwd=cwd, out=out)
1933 rc = self._runsystem(cmd, environ=environ, cwd=cwd, out=out)
1934 if rc and onerr:
1934 if rc and onerr:
1935 errmsg = b'%s %s' % (
1935 errmsg = b'%s %s' % (
1936 procutil.shellsplit(cmd)[0],
1936 procutil.shellsplit(cmd)[0],
1937 procutil.explainexit(rc),
1937 procutil.explainexit(rc),
1938 )
1938 )
1939 if errprefix:
1939 if errprefix:
1940 errmsg = b'%s: %s' % (errprefix, errmsg)
1940 errmsg = b'%s: %s' % (errprefix, errmsg)
1941 raise onerr(errmsg)
1941 raise onerr(errmsg)
1942 return rc
1942 return rc
1943
1943
1944 def _runsystem(self, cmd, environ, cwd, out):
1944 def _runsystem(self, cmd, environ, cwd, out):
1945 """actually execute the given shell command (can be overridden by
1945 """actually execute the given shell command (can be overridden by
1946 extensions like chg)"""
1946 extensions like chg)"""
1947 return procutil.system(cmd, environ=environ, cwd=cwd, out=out)
1947 return procutil.system(cmd, environ=environ, cwd=cwd, out=out)
1948
1948
1949 def traceback(self, exc=None, force=False):
1949 def traceback(self, exc=None, force=False):
1950 """print exception traceback if traceback printing enabled or forced.
1950 """print exception traceback if traceback printing enabled or forced.
1951 only to call in exception handler. returns true if traceback
1951 only to call in exception handler. returns true if traceback
1952 printed."""
1952 printed."""
1953 if self.tracebackflag or force:
1953 if self.tracebackflag or force:
1954 if exc is None:
1954 if exc is None:
1955 exc = sys.exc_info()
1955 exc = sys.exc_info()
1956 cause = getattr(exc[1], 'cause', None)
1956 cause = getattr(exc[1], 'cause', None)
1957
1957
1958 if cause is not None:
1958 if cause is not None:
1959 causetb = traceback.format_tb(cause[2])
1959 causetb = traceback.format_tb(cause[2])
1960 exctb = traceback.format_tb(exc[2])
1960 exctb = traceback.format_tb(exc[2])
1961 exconly = traceback.format_exception_only(cause[0], cause[1])
1961 exconly = traceback.format_exception_only(cause[0], cause[1])
1962
1962
1963 # exclude frame where 'exc' was chained and rethrown from exctb
1963 # exclude frame where 'exc' was chained and rethrown from exctb
1964 self.write_err(
1964 self.write_err(
1965 b'Traceback (most recent call last):\n',
1965 b'Traceback (most recent call last):\n',
1966 encoding.strtolocal(''.join(exctb[:-1])),
1966 encoding.strtolocal(''.join(exctb[:-1])),
1967 encoding.strtolocal(''.join(causetb)),
1967 encoding.strtolocal(''.join(causetb)),
1968 encoding.strtolocal(''.join(exconly)),
1968 encoding.strtolocal(''.join(exconly)),
1969 )
1969 )
1970 else:
1970 else:
1971 output = traceback.format_exception(exc[0], exc[1], exc[2])
1971 output = traceback.format_exception(exc[0], exc[1], exc[2])
1972 self.write_err(encoding.strtolocal(''.join(output)))
1972 self.write_err(encoding.strtolocal(''.join(output)))
1973 return self.tracebackflag or force
1973 return self.tracebackflag or force
1974
1974
1975 def geteditor(self):
1975 def geteditor(self):
1976 '''return editor to use'''
1976 '''return editor to use'''
1977 if pycompat.sysplatform == b'plan9':
1977 if pycompat.sysplatform == b'plan9':
1978 # vi is the MIPS instruction simulator on Plan 9. We
1978 # vi is the MIPS instruction simulator on Plan 9. We
1979 # instead default to E to plumb commit messages to
1979 # instead default to E to plumb commit messages to
1980 # avoid confusion.
1980 # avoid confusion.
1981 editor = b'E'
1981 editor = b'E'
1982 elif pycompat.isdarwin:
1982 elif pycompat.isdarwin:
1983 # vi on darwin is POSIX compatible to a fault, and that includes
1983 # vi on darwin is POSIX compatible to a fault, and that includes
1984 # exiting non-zero if you make any mistake when running an ex
1984 # exiting non-zero if you make any mistake when running an ex
1985 # command. Proof: `vi -c ':unknown' -c ':qa'; echo $?` produces 1,
1985 # command. Proof: `vi -c ':unknown' -c ':qa'; echo $?` produces 1,
1986 # while s/vi/vim/ doesn't.
1986 # while s/vi/vim/ doesn't.
1987 editor = b'vim'
1987 editor = b'vim'
1988 else:
1988 else:
1989 editor = b'vi'
1989 editor = b'vi'
1990 return encoding.environ.get(b"HGEDITOR") or self.config(
1990 return encoding.environ.get(b"HGEDITOR") or self.config(
1991 b"ui", b"editor", editor
1991 b"ui", b"editor", editor
1992 )
1992 )
1993
1993
1994 @util.propertycache
1994 @util.propertycache
1995 def _progbar(self):
1995 def _progbar(self):
1996 """setup the progbar singleton to the ui object"""
1996 """setup the progbar singleton to the ui object"""
1997 if (
1997 if (
1998 self.quiet
1998 self.quiet
1999 or self.debugflag
1999 or self.debugflag
2000 or self.configbool(b'progress', b'disable')
2000 or self.configbool(b'progress', b'disable')
2001 or not progress.shouldprint(self)
2001 or not progress.shouldprint(self)
2002 ):
2002 ):
2003 return None
2003 return None
2004 return getprogbar(self)
2004 return getprogbar(self)
2005
2005
2006 def _progclear(self):
2006 def _progclear(self):
2007 """clear progress bar output if any. use it before any output"""
2007 """clear progress bar output if any. use it before any output"""
2008 if not haveprogbar(): # nothing loaded yet
2008 if not haveprogbar(): # nothing loaded yet
2009 return
2009 return
2010 if self._progbar is not None and self._progbar.printed:
2010 if self._progbar is not None and self._progbar.printed:
2011 self._progbar.clear()
2011 self._progbar.clear()
2012
2012
2013 def makeprogress(self, topic, unit=b"", total=None):
2013 def makeprogress(self, topic, unit=b"", total=None):
2014 """Create a progress helper for the specified topic"""
2014 """Create a progress helper for the specified topic"""
2015 if getattr(self._fmsgerr, 'structured', False):
2015 if getattr(self._fmsgerr, 'structured', False):
2016 # channel for machine-readable output with metadata, just send
2016 # channel for machine-readable output with metadata, just send
2017 # raw information
2017 # raw information
2018 # TODO: consider porting some useful information (e.g. estimated
2018 # TODO: consider porting some useful information (e.g. estimated
2019 # time) from progbar. we might want to support update delay to
2019 # time) from progbar. we might want to support update delay to
2020 # reduce the cost of transferring progress messages.
2020 # reduce the cost of transferring progress messages.
2021 def updatebar(topic, pos, item, unit, total):
2021 def updatebar(topic, pos, item, unit, total):
2022 self._fmsgerr.write(
2022 self._fmsgerr.write(
2023 None,
2023 None,
2024 type=b'progress',
2024 type=b'progress',
2025 topic=topic,
2025 topic=topic,
2026 pos=pos,
2026 pos=pos,
2027 item=item,
2027 item=item,
2028 unit=unit,
2028 unit=unit,
2029 total=total,
2029 total=total,
2030 )
2030 )
2031
2031
2032 elif self._progbar is not None:
2032 elif self._progbar is not None:
2033 updatebar = self._progbar.progress
2033 updatebar = self._progbar.progress
2034 else:
2034 else:
2035
2035
2036 def updatebar(topic, pos, item, unit, total):
2036 def updatebar(topic, pos, item, unit, total):
2037 pass
2037 pass
2038
2038
2039 return scmutil.progress(self, updatebar, topic, unit, total)
2039 return scmutil.progress(self, updatebar, topic, unit, total)
2040
2040
2041 def getlogger(self, name):
2041 def getlogger(self, name):
2042 """Returns a logger of the given name; or None if not registered"""
2042 """Returns a logger of the given name; or None if not registered"""
2043 return self._loggers.get(name)
2043 return self._loggers.get(name)
2044
2044
2045 def setlogger(self, name, logger):
2045 def setlogger(self, name, logger):
2046 """Install logger which can be identified later by the given name
2046 """Install logger which can be identified later by the given name
2047
2047
2048 More than one loggers can be registered. Use extension or module
2048 More than one loggers can be registered. Use extension or module
2049 name to uniquely identify the logger instance.
2049 name to uniquely identify the logger instance.
2050 """
2050 """
2051 self._loggers[name] = logger
2051 self._loggers[name] = logger
2052
2052
2053 def log(self, event, msgfmt, *msgargs, **opts):
2053 def log(self, event, msgfmt, *msgargs, **opts):
2054 """hook for logging facility extensions
2054 """hook for logging facility extensions
2055
2055
2056 event should be a readily-identifiable subsystem, which will
2056 event should be a readily-identifiable subsystem, which will
2057 allow filtering.
2057 allow filtering.
2058
2058
2059 msgfmt should be a newline-terminated format string to log, and
2059 msgfmt should be a newline-terminated format string to log, and
2060 *msgargs are %-formatted into it.
2060 *msgargs are %-formatted into it.
2061
2061
2062 **opts currently has no defined meanings.
2062 **opts currently has no defined meanings.
2063 """
2063 """
2064 if not self._loggers:
2064 if not self._loggers:
2065 return
2065 return
2066 activeloggers = [
2066 activeloggers = [
2067 l for l in pycompat.itervalues(self._loggers) if l.tracked(event)
2067 l for l in pycompat.itervalues(self._loggers) if l.tracked(event)
2068 ]
2068 ]
2069 if not activeloggers:
2069 if not activeloggers:
2070 return
2070 return
2071 msg = msgfmt % msgargs
2071 msg = msgfmt % msgargs
2072 opts = pycompat.byteskwargs(opts)
2072 opts = pycompat.byteskwargs(opts)
2073 # guard against recursion from e.g. ui.debug()
2073 # guard against recursion from e.g. ui.debug()
2074 registeredloggers = self._loggers
2074 registeredloggers = self._loggers
2075 self._loggers = {}
2075 self._loggers = {}
2076 try:
2076 try:
2077 for logger in activeloggers:
2077 for logger in activeloggers:
2078 logger.log(self, event, msg, opts)
2078 logger.log(self, event, msg, opts)
2079 finally:
2079 finally:
2080 self._loggers = registeredloggers
2080 self._loggers = registeredloggers
2081
2081
2082 def label(self, msg, label):
2082 def label(self, msg, label):
2083 """style msg based on supplied label
2083 """style msg based on supplied label
2084
2084
2085 If some color mode is enabled, this will add the necessary control
2085 If some color mode is enabled, this will add the necessary control
2086 characters to apply such color. In addition, 'debug' color mode adds
2086 characters to apply such color. In addition, 'debug' color mode adds
2087 markup showing which label affects a piece of text.
2087 markup showing which label affects a piece of text.
2088
2088
2089 ui.write(s, 'label') is equivalent to
2089 ui.write(s, 'label') is equivalent to
2090 ui.write(ui.label(s, 'label')).
2090 ui.write(ui.label(s, 'label')).
2091 """
2091 """
2092 if self._colormode is not None:
2092 if self._colormode is not None:
2093 return color.colorlabel(self, msg, label)
2093 return color.colorlabel(self, msg, label)
2094 return msg
2094 return msg
2095
2095
2096 def develwarn(self, msg, stacklevel=1, config=None):
2096 def develwarn(self, msg, stacklevel=1, config=None):
2097 """issue a developer warning message
2097 """issue a developer warning message
2098
2098
2099 Use 'stacklevel' to report the offender some layers further up in the
2099 Use 'stacklevel' to report the offender some layers further up in the
2100 stack.
2100 stack.
2101 """
2101 """
2102 if not self.configbool(b'devel', b'all-warnings'):
2102 if not self.configbool(b'devel', b'all-warnings'):
2103 if config is None or not self.configbool(b'devel', config):
2103 if config is None or not self.configbool(b'devel', config):
2104 return
2104 return
2105 msg = b'devel-warn: ' + msg
2105 msg = b'devel-warn: ' + msg
2106 stacklevel += 1 # get in develwarn
2106 stacklevel += 1 # get in develwarn
2107 if self.tracebackflag:
2107 if self.tracebackflag:
2108 util.debugstacktrace(msg, stacklevel, self._ferr, self._fout)
2108 util.debugstacktrace(msg, stacklevel, self._ferr, self._fout)
2109 self.log(
2109 self.log(
2110 b'develwarn',
2110 b'develwarn',
2111 b'%s at:\n%s'
2111 b'%s at:\n%s'
2112 % (msg, b''.join(util.getstackframes(stacklevel))),
2112 % (msg, b''.join(util.getstackframes(stacklevel))),
2113 )
2113 )
2114 else:
2114 else:
2115 curframe = inspect.currentframe()
2115 curframe = inspect.currentframe()
2116 calframe = inspect.getouterframes(curframe, 2)
2116 calframe = inspect.getouterframes(curframe, 2)
2117 fname, lineno, fmsg = calframe[stacklevel][1:4]
2117 fname, lineno, fmsg = calframe[stacklevel][1:4]
2118 fname, fmsg = pycompat.sysbytes(fname), pycompat.sysbytes(fmsg)
2118 fname, fmsg = pycompat.sysbytes(fname), pycompat.sysbytes(fmsg)
2119 self.write_err(b'%s at: %s:%d (%s)\n' % (msg, fname, lineno, fmsg))
2119 self.write_err(b'%s at: %s:%d (%s)\n' % (msg, fname, lineno, fmsg))
2120 self.log(
2120 self.log(
2121 b'develwarn', b'%s at: %s:%d (%s)\n', msg, fname, lineno, fmsg
2121 b'develwarn', b'%s at: %s:%d (%s)\n', msg, fname, lineno, fmsg
2122 )
2122 )
2123
2123
2124 # avoid cycles
2124 # avoid cycles
2125 del curframe
2125 del curframe
2126 del calframe
2126 del calframe
2127
2127
2128 def deprecwarn(self, msg, version, stacklevel=2):
2128 def deprecwarn(self, msg, version, stacklevel=2):
2129 """issue a deprecation warning
2129 """issue a deprecation warning
2130
2130
2131 - msg: message explaining what is deprecated and how to upgrade,
2131 - msg: message explaining what is deprecated and how to upgrade,
2132 - version: last version where the API will be supported,
2132 - version: last version where the API will be supported,
2133 """
2133 """
2134 if not (
2134 if not (
2135 self.configbool(b'devel', b'all-warnings')
2135 self.configbool(b'devel', b'all-warnings')
2136 or self.configbool(b'devel', b'deprec-warn')
2136 or self.configbool(b'devel', b'deprec-warn')
2137 ):
2137 ):
2138 return
2138 return
2139 msg += (
2139 msg += (
2140 b"\n(compatibility will be dropped after Mercurial-%s,"
2140 b"\n(compatibility will be dropped after Mercurial-%s,"
2141 b" update your code.)"
2141 b" update your code.)"
2142 ) % version
2142 ) % version
2143 self.develwarn(msg, stacklevel=stacklevel, config=b'deprec-warn')
2143 self.develwarn(msg, stacklevel=stacklevel, config=b'deprec-warn')
2144
2144
2145 def exportableenviron(self):
2145 def exportableenviron(self):
2146 """The environment variables that are safe to export, e.g. through
2146 """The environment variables that are safe to export, e.g. through
2147 hgweb.
2147 hgweb.
2148 """
2148 """
2149 return self._exportableenviron
2149 return self._exportableenviron
2150
2150
2151 @contextlib.contextmanager
2151 @contextlib.contextmanager
2152 def configoverride(self, overrides, source=b""):
2152 def configoverride(self, overrides, source=b""):
2153 """Context manager for temporary config overrides
2153 """Context manager for temporary config overrides
2154 `overrides` must be a dict of the following structure:
2154 `overrides` must be a dict of the following structure:
2155 {(section, name) : value}"""
2155 {(section, name) : value}"""
2156 backups = {}
2156 backups = {}
2157 try:
2157 try:
2158 for (section, name), value in overrides.items():
2158 for (section, name), value in overrides.items():
2159 backups[(section, name)] = self.backupconfig(section, name)
2159 backups[(section, name)] = self.backupconfig(section, name)
2160 self.setconfig(section, name, value, source)
2160 self.setconfig(section, name, value, source)
2161 yield
2161 yield
2162 finally:
2162 finally:
2163 for __, backup in backups.items():
2163 for __, backup in backups.items():
2164 self.restoreconfig(backup)
2164 self.restoreconfig(backup)
2165 # just restoring ui.quiet config to the previous value is not enough
2165 # just restoring ui.quiet config to the previous value is not enough
2166 # as it does not update ui.quiet class member
2166 # as it does not update ui.quiet class member
2167 if (b'ui', b'quiet') in overrides:
2167 if (b'ui', b'quiet') in overrides:
2168 self.fixconfig(section=b'ui')
2168 self.fixconfig(section=b'ui')
2169
2169
2170 def estimatememory(self):
2170 def estimatememory(self):
2171 """Provide an estimate for the available system memory in Bytes.
2171 """Provide an estimate for the available system memory in Bytes.
2172
2172
2173 This can be overriden via ui.available-memory. It returns None, if
2173 This can be overriden via ui.available-memory. It returns None, if
2174 no estimate can be computed.
2174 no estimate can be computed.
2175 """
2175 """
2176 value = self.config(b'ui', b'available-memory')
2176 value = self.config(b'ui', b'available-memory')
2177 if value is not None:
2177 if value is not None:
2178 try:
2178 try:
2179 return util.sizetoint(value)
2179 return util.sizetoint(value)
2180 except error.ParseError:
2180 except error.ParseError:
2181 raise error.ConfigError(
2181 raise error.ConfigError(
2182 _(b"ui.available-memory value is invalid ('%s')") % value
2182 _(b"ui.available-memory value is invalid ('%s')") % value
2183 )
2183 )
2184 return util._estimatememory()
2184 return util._estimatememory()
2185
2185
2186
2186
2187 # we instantiate one globally shared progress bar to avoid
2187 # we instantiate one globally shared progress bar to avoid
2188 # competing progress bars when multiple UI objects get created
2188 # competing progress bars when multiple UI objects get created
2189 _progresssingleton = None
2189 _progresssingleton = None
2190
2190
2191
2191
2192 def getprogbar(ui):
2192 def getprogbar(ui):
2193 global _progresssingleton
2193 global _progresssingleton
2194 if _progresssingleton is None:
2194 if _progresssingleton is None:
2195 # passing 'ui' object to the singleton is fishy,
2195 # passing 'ui' object to the singleton is fishy,
2196 # this is how the extension used to work but feel free to rework it.
2196 # this is how the extension used to work but feel free to rework it.
2197 _progresssingleton = progress.progbar(ui)
2197 _progresssingleton = progress.progbar(ui)
2198 return _progresssingleton
2198 return _progresssingleton
2199
2199
2200
2200
2201 def haveprogbar():
2201 def haveprogbar():
2202 return _progresssingleton is not None
2202 return _progresssingleton is not None
2203
2203
2204
2204
2205 def _selectmsgdests(ui):
2205 def _selectmsgdests(ui):
2206 name = ui.config(b'ui', b'message-output')
2206 name = ui.config(b'ui', b'message-output')
2207 if name == b'channel':
2207 if name == b'channel':
2208 if ui.fmsg:
2208 if ui.fmsg:
2209 return ui.fmsg, ui.fmsg
2209 return ui.fmsg, ui.fmsg
2210 else:
2210 else:
2211 # fall back to ferr if channel isn't ready so that status/error
2211 # fall back to ferr if channel isn't ready so that status/error
2212 # messages can be printed
2212 # messages can be printed
2213 return ui.ferr, ui.ferr
2213 return ui.ferr, ui.ferr
2214 if name == b'stdio':
2214 if name == b'stdio':
2215 return ui.fout, ui.ferr
2215 return ui.fout, ui.ferr
2216 if name == b'stderr':
2216 if name == b'stderr':
2217 return ui.ferr, ui.ferr
2217 return ui.ferr, ui.ferr
2218 raise error.Abort(b'invalid ui.message-output destination: %s' % name)
2218 raise error.Abort(b'invalid ui.message-output destination: %s' % name)
2219
2219
2220
2220
2221 def _writemsgwith(write, dest, *args, **opts):
2221 def _writemsgwith(write, dest, *args, **opts):
2222 """Write ui message with the given ui._write*() function
2222 """Write ui message with the given ui._write*() function
2223
2223
2224 The specified message type is translated to 'ui.<type>' label if the dest
2224 The specified message type is translated to 'ui.<type>' label if the dest
2225 isn't a structured channel, so that the message will be colorized.
2225 isn't a structured channel, so that the message will be colorized.
2226 """
2226 """
2227 # TODO: maybe change 'type' to a mandatory option
2227 # TODO: maybe change 'type' to a mandatory option
2228 if 'type' in opts and not getattr(dest, 'structured', False):
2228 if 'type' in opts and not getattr(dest, 'structured', False):
2229 opts['label'] = opts.get('label', b'') + b' ui.%s' % opts.pop('type')
2229 opts['label'] = opts.get('label', b'') + b' ui.%s' % opts.pop('type')
2230 write(dest, *args, **opts)
2230 write(dest, *args, **opts)
@@ -1,876 +1,966 b''
1 # stringutil.py - utility for generic string formatting, parsing, etc.
1 # stringutil.py - utility for generic string formatting, parsing, etc.
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 from __future__ import absolute_import
10 from __future__ import absolute_import
11
11
12 import ast
12 import ast
13 import codecs
13 import codecs
14 import re as remod
14 import re as remod
15 import textwrap
15 import textwrap
16 import types
16 import types
17
17
18 from ..i18n import _
18 from ..i18n import _
19 from ..thirdparty import attr
19 from ..thirdparty import attr
20
20
21 from .. import (
21 from .. import (
22 encoding,
22 encoding,
23 error,
23 error,
24 pycompat,
24 pycompat,
25 )
25 )
26
26
27 # regex special chars pulled from https://bugs.python.org/issue29995
27 # regex special chars pulled from https://bugs.python.org/issue29995
28 # which was part of Python 3.7.
28 # which was part of Python 3.7.
29 _respecial = pycompat.bytestr(b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f')
29 _respecial = pycompat.bytestr(b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f')
30 _regexescapemap = {ord(i): (b'\\' + i).decode('latin1') for i in _respecial}
30 _regexescapemap = {ord(i): (b'\\' + i).decode('latin1') for i in _respecial}
31 regexbytesescapemap = {i: (b'\\' + i) for i in _respecial}
31 regexbytesescapemap = {i: (b'\\' + i) for i in _respecial}
32
32
33
33
34 def reescape(pat):
34 def reescape(pat):
35 """Drop-in replacement for re.escape."""
35 """Drop-in replacement for re.escape."""
36 # NOTE: it is intentional that this works on unicodes and not
36 # NOTE: it is intentional that this works on unicodes and not
37 # bytes, as it's only possible to do the escaping with
37 # bytes, as it's only possible to do the escaping with
38 # unicode.translate, not bytes.translate. Sigh.
38 # unicode.translate, not bytes.translate. Sigh.
39 wantuni = True
39 wantuni = True
40 if isinstance(pat, bytes):
40 if isinstance(pat, bytes):
41 wantuni = False
41 wantuni = False
42 pat = pat.decode('latin1')
42 pat = pat.decode('latin1')
43 pat = pat.translate(_regexescapemap)
43 pat = pat.translate(_regexescapemap)
44 if wantuni:
44 if wantuni:
45 return pat
45 return pat
46 return pat.encode('latin1')
46 return pat.encode('latin1')
47
47
48
48
49 def pprint(o, bprefix=False, indent=0, level=0):
49 def pprint(o, bprefix=False, indent=0, level=0):
50 """Pretty print an object."""
50 """Pretty print an object."""
51 return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level))
51 return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level))
52
52
53
53
54 def pprintgen(o, bprefix=False, indent=0, level=0):
54 def pprintgen(o, bprefix=False, indent=0, level=0):
55 """Pretty print an object to a generator of atoms.
55 """Pretty print an object to a generator of atoms.
56
56
57 ``bprefix`` is a flag influencing whether bytestrings are preferred with
57 ``bprefix`` is a flag influencing whether bytestrings are preferred with
58 a ``b''`` prefix.
58 a ``b''`` prefix.
59
59
60 ``indent`` controls whether collections and nested data structures
60 ``indent`` controls whether collections and nested data structures
61 span multiple lines via the indentation amount in spaces. By default,
61 span multiple lines via the indentation amount in spaces. By default,
62 no newlines are emitted.
62 no newlines are emitted.
63
63
64 ``level`` specifies the initial indent level. Used if ``indent > 0``.
64 ``level`` specifies the initial indent level. Used if ``indent > 0``.
65 """
65 """
66
66
67 if isinstance(o, bytes):
67 if isinstance(o, bytes):
68 if bprefix:
68 if bprefix:
69 yield b"b'%s'" % escapestr(o)
69 yield b"b'%s'" % escapestr(o)
70 else:
70 else:
71 yield b"'%s'" % escapestr(o)
71 yield b"'%s'" % escapestr(o)
72 elif isinstance(o, bytearray):
72 elif isinstance(o, bytearray):
73 # codecs.escape_encode() can't handle bytearray, so escapestr fails
73 # codecs.escape_encode() can't handle bytearray, so escapestr fails
74 # without coercion.
74 # without coercion.
75 yield b"bytearray['%s']" % escapestr(bytes(o))
75 yield b"bytearray['%s']" % escapestr(bytes(o))
76 elif isinstance(o, list):
76 elif isinstance(o, list):
77 if not o:
77 if not o:
78 yield b'[]'
78 yield b'[]'
79 return
79 return
80
80
81 yield b'['
81 yield b'['
82
82
83 if indent:
83 if indent:
84 level += 1
84 level += 1
85 yield b'\n'
85 yield b'\n'
86 yield b' ' * (level * indent)
86 yield b' ' * (level * indent)
87
87
88 for i, a in enumerate(o):
88 for i, a in enumerate(o):
89 for chunk in pprintgen(
89 for chunk in pprintgen(
90 a, bprefix=bprefix, indent=indent, level=level
90 a, bprefix=bprefix, indent=indent, level=level
91 ):
91 ):
92 yield chunk
92 yield chunk
93
93
94 if i + 1 < len(o):
94 if i + 1 < len(o):
95 if indent:
95 if indent:
96 yield b',\n'
96 yield b',\n'
97 yield b' ' * (level * indent)
97 yield b' ' * (level * indent)
98 else:
98 else:
99 yield b', '
99 yield b', '
100
100
101 if indent:
101 if indent:
102 level -= 1
102 level -= 1
103 yield b'\n'
103 yield b'\n'
104 yield b' ' * (level * indent)
104 yield b' ' * (level * indent)
105
105
106 yield b']'
106 yield b']'
107 elif isinstance(o, dict):
107 elif isinstance(o, dict):
108 if not o:
108 if not o:
109 yield b'{}'
109 yield b'{}'
110 return
110 return
111
111
112 yield b'{'
112 yield b'{'
113
113
114 if indent:
114 if indent:
115 level += 1
115 level += 1
116 yield b'\n'
116 yield b'\n'
117 yield b' ' * (level * indent)
117 yield b' ' * (level * indent)
118
118
119 for i, (k, v) in enumerate(sorted(o.items())):
119 for i, (k, v) in enumerate(sorted(o.items())):
120 for chunk in pprintgen(
120 for chunk in pprintgen(
121 k, bprefix=bprefix, indent=indent, level=level
121 k, bprefix=bprefix, indent=indent, level=level
122 ):
122 ):
123 yield chunk
123 yield chunk
124
124
125 yield b': '
125 yield b': '
126
126
127 for chunk in pprintgen(
127 for chunk in pprintgen(
128 v, bprefix=bprefix, indent=indent, level=level
128 v, bprefix=bprefix, indent=indent, level=level
129 ):
129 ):
130 yield chunk
130 yield chunk
131
131
132 if i + 1 < len(o):
132 if i + 1 < len(o):
133 if indent:
133 if indent:
134 yield b',\n'
134 yield b',\n'
135 yield b' ' * (level * indent)
135 yield b' ' * (level * indent)
136 else:
136 else:
137 yield b', '
137 yield b', '
138
138
139 if indent:
139 if indent:
140 level -= 1
140 level -= 1
141 yield b'\n'
141 yield b'\n'
142 yield b' ' * (level * indent)
142 yield b' ' * (level * indent)
143
143
144 yield b'}'
144 yield b'}'
145 elif isinstance(o, set):
145 elif isinstance(o, set):
146 if not o:
146 if not o:
147 yield b'set([])'
147 yield b'set([])'
148 return
148 return
149
149
150 yield b'set(['
150 yield b'set(['
151
151
152 if indent:
152 if indent:
153 level += 1
153 level += 1
154 yield b'\n'
154 yield b'\n'
155 yield b' ' * (level * indent)
155 yield b' ' * (level * indent)
156
156
157 for i, k in enumerate(sorted(o)):
157 for i, k in enumerate(sorted(o)):
158 for chunk in pprintgen(
158 for chunk in pprintgen(
159 k, bprefix=bprefix, indent=indent, level=level
159 k, bprefix=bprefix, indent=indent, level=level
160 ):
160 ):
161 yield chunk
161 yield chunk
162
162
163 if i + 1 < len(o):
163 if i + 1 < len(o):
164 if indent:
164 if indent:
165 yield b',\n'
165 yield b',\n'
166 yield b' ' * (level * indent)
166 yield b' ' * (level * indent)
167 else:
167 else:
168 yield b', '
168 yield b', '
169
169
170 if indent:
170 if indent:
171 level -= 1
171 level -= 1
172 yield b'\n'
172 yield b'\n'
173 yield b' ' * (level * indent)
173 yield b' ' * (level * indent)
174
174
175 yield b'])'
175 yield b'])'
176 elif isinstance(o, tuple):
176 elif isinstance(o, tuple):
177 if not o:
177 if not o:
178 yield b'()'
178 yield b'()'
179 return
179 return
180
180
181 yield b'('
181 yield b'('
182
182
183 if indent:
183 if indent:
184 level += 1
184 level += 1
185 yield b'\n'
185 yield b'\n'
186 yield b' ' * (level * indent)
186 yield b' ' * (level * indent)
187
187
188 for i, a in enumerate(o):
188 for i, a in enumerate(o):
189 for chunk in pprintgen(
189 for chunk in pprintgen(
190 a, bprefix=bprefix, indent=indent, level=level
190 a, bprefix=bprefix, indent=indent, level=level
191 ):
191 ):
192 yield chunk
192 yield chunk
193
193
194 if i + 1 < len(o):
194 if i + 1 < len(o):
195 if indent:
195 if indent:
196 yield b',\n'
196 yield b',\n'
197 yield b' ' * (level * indent)
197 yield b' ' * (level * indent)
198 else:
198 else:
199 yield b', '
199 yield b', '
200
200
201 if indent:
201 if indent:
202 level -= 1
202 level -= 1
203 yield b'\n'
203 yield b'\n'
204 yield b' ' * (level * indent)
204 yield b' ' * (level * indent)
205
205
206 yield b')'
206 yield b')'
207 elif isinstance(o, types.GeneratorType):
207 elif isinstance(o, types.GeneratorType):
208 # Special case of empty generator.
208 # Special case of empty generator.
209 try:
209 try:
210 nextitem = next(o)
210 nextitem = next(o)
211 except StopIteration:
211 except StopIteration:
212 yield b'gen[]'
212 yield b'gen[]'
213 return
213 return
214
214
215 yield b'gen['
215 yield b'gen['
216
216
217 if indent:
217 if indent:
218 level += 1
218 level += 1
219 yield b'\n'
219 yield b'\n'
220 yield b' ' * (level * indent)
220 yield b' ' * (level * indent)
221
221
222 last = False
222 last = False
223
223
224 while not last:
224 while not last:
225 current = nextitem
225 current = nextitem
226
226
227 try:
227 try:
228 nextitem = next(o)
228 nextitem = next(o)
229 except StopIteration:
229 except StopIteration:
230 last = True
230 last = True
231
231
232 for chunk in pprintgen(
232 for chunk in pprintgen(
233 current, bprefix=bprefix, indent=indent, level=level
233 current, bprefix=bprefix, indent=indent, level=level
234 ):
234 ):
235 yield chunk
235 yield chunk
236
236
237 if not last:
237 if not last:
238 if indent:
238 if indent:
239 yield b',\n'
239 yield b',\n'
240 yield b' ' * (level * indent)
240 yield b' ' * (level * indent)
241 else:
241 else:
242 yield b', '
242 yield b', '
243
243
244 if indent:
244 if indent:
245 level -= 1
245 level -= 1
246 yield b'\n'
246 yield b'\n'
247 yield b' ' * (level * indent)
247 yield b' ' * (level * indent)
248
248
249 yield b']'
249 yield b']'
250 else:
250 else:
251 yield pycompat.byterepr(o)
251 yield pycompat.byterepr(o)
252
252
253
253
254 def prettyrepr(o):
254 def prettyrepr(o):
255 """Pretty print a representation of a possibly-nested object"""
255 """Pretty print a representation of a possibly-nested object"""
256 lines = []
256 lines = []
257 rs = pycompat.byterepr(o)
257 rs = pycompat.byterepr(o)
258 p0 = p1 = 0
258 p0 = p1 = 0
259 while p0 < len(rs):
259 while p0 < len(rs):
260 # '... field=<type ... field=<type ...'
260 # '... field=<type ... field=<type ...'
261 # ~~~~~~~~~~~~~~~~
261 # ~~~~~~~~~~~~~~~~
262 # p0 p1 q0 q1
262 # p0 p1 q0 q1
263 q0 = -1
263 q0 = -1
264 q1 = rs.find(b'<', p1 + 1)
264 q1 = rs.find(b'<', p1 + 1)
265 if q1 < 0:
265 if q1 < 0:
266 q1 = len(rs)
266 q1 = len(rs)
267 elif q1 > p1 + 1 and rs.startswith(b'=', q1 - 1):
267 elif q1 > p1 + 1 and rs.startswith(b'=', q1 - 1):
268 # backtrack for ' field=<'
268 # backtrack for ' field=<'
269 q0 = rs.rfind(b' ', p1 + 1, q1 - 1)
269 q0 = rs.rfind(b' ', p1 + 1, q1 - 1)
270 if q0 < 0:
270 if q0 < 0:
271 q0 = q1
271 q0 = q1
272 else:
272 else:
273 q0 += 1 # skip ' '
273 q0 += 1 # skip ' '
274 l = rs.count(b'<', 0, p0) - rs.count(b'>', 0, p0)
274 l = rs.count(b'<', 0, p0) - rs.count(b'>', 0, p0)
275 assert l >= 0
275 assert l >= 0
276 lines.append((l, rs[p0:q0].rstrip()))
276 lines.append((l, rs[p0:q0].rstrip()))
277 p0, p1 = q0, q1
277 p0, p1 = q0, q1
278 return b'\n'.join(b' ' * l + s for l, s in lines)
278 return b'\n'.join(b' ' * l + s for l, s in lines)
279
279
280
280
281 def buildrepr(r):
281 def buildrepr(r):
282 """Format an optional printable representation from unexpanded bits
282 """Format an optional printable representation from unexpanded bits
283
283
284 ======== =================================
284 ======== =================================
285 type(r) example
285 type(r) example
286 ======== =================================
286 ======== =================================
287 tuple ('<not %r>', other)
287 tuple ('<not %r>', other)
288 bytes '<branch closed>'
288 bytes '<branch closed>'
289 callable lambda: '<branch %r>' % sorted(b)
289 callable lambda: '<branch %r>' % sorted(b)
290 object other
290 object other
291 ======== =================================
291 ======== =================================
292 """
292 """
293 if r is None:
293 if r is None:
294 return b''
294 return b''
295 elif isinstance(r, tuple):
295 elif isinstance(r, tuple):
296 return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
296 return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
297 elif isinstance(r, bytes):
297 elif isinstance(r, bytes):
298 return r
298 return r
299 elif callable(r):
299 elif callable(r):
300 return r()
300 return r()
301 else:
301 else:
302 return pprint(r)
302 return pprint(r)
303
303
304
304
305 def binary(s):
305 def binary(s):
306 """return true if a string is binary data"""
306 """return true if a string is binary data"""
307 return bool(s and b'\0' in s)
307 return bool(s and b'\0' in s)
308
308
309
309
310 def _splitpattern(pattern):
310 def _splitpattern(pattern):
311 if pattern.startswith(b're:'):
311 if pattern.startswith(b're:'):
312 return b're', pattern[3:]
312 return b're', pattern[3:]
313 elif pattern.startswith(b'literal:'):
313 elif pattern.startswith(b'literal:'):
314 return b'literal', pattern[8:]
314 return b'literal', pattern[8:]
315 return b'literal', pattern
315 return b'literal', pattern
316
316
317
317
318 def stringmatcher(pattern, casesensitive=True):
318 def stringmatcher(pattern, casesensitive=True):
319 """
319 """
320 accepts a string, possibly starting with 're:' or 'literal:' prefix.
320 accepts a string, possibly starting with 're:' or 'literal:' prefix.
321 returns the matcher name, pattern, and matcher function.
321 returns the matcher name, pattern, and matcher function.
322 missing or unknown prefixes are treated as literal matches.
322 missing or unknown prefixes are treated as literal matches.
323
323
324 helper for tests:
324 helper for tests:
325 >>> def test(pattern, *tests):
325 >>> def test(pattern, *tests):
326 ... kind, pattern, matcher = stringmatcher(pattern)
326 ... kind, pattern, matcher = stringmatcher(pattern)
327 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
327 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
328 >>> def itest(pattern, *tests):
328 >>> def itest(pattern, *tests):
329 ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False)
329 ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False)
330 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
330 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
331
331
332 exact matching (no prefix):
332 exact matching (no prefix):
333 >>> test(b'abcdefg', b'abc', b'def', b'abcdefg')
333 >>> test(b'abcdefg', b'abc', b'def', b'abcdefg')
334 ('literal', 'abcdefg', [False, False, True])
334 ('literal', 'abcdefg', [False, False, True])
335
335
336 regex matching ('re:' prefix)
336 regex matching ('re:' prefix)
337 >>> test(b're:a.+b', b'nomatch', b'fooadef', b'fooadefbar')
337 >>> test(b're:a.+b', b'nomatch', b'fooadef', b'fooadefbar')
338 ('re', 'a.+b', [False, False, True])
338 ('re', 'a.+b', [False, False, True])
339
339
340 force exact matches ('literal:' prefix)
340 force exact matches ('literal:' prefix)
341 >>> test(b'literal:re:foobar', b'foobar', b're:foobar')
341 >>> test(b'literal:re:foobar', b'foobar', b're:foobar')
342 ('literal', 're:foobar', [False, True])
342 ('literal', 're:foobar', [False, True])
343
343
344 unknown prefixes are ignored and treated as literals
344 unknown prefixes are ignored and treated as literals
345 >>> test(b'foo:bar', b'foo', b'bar', b'foo:bar')
345 >>> test(b'foo:bar', b'foo', b'bar', b'foo:bar')
346 ('literal', 'foo:bar', [False, False, True])
346 ('literal', 'foo:bar', [False, False, True])
347
347
348 case insensitive regex matches
348 case insensitive regex matches
349 >>> itest(b're:A.+b', b'nomatch', b'fooadef', b'fooadefBar')
349 >>> itest(b're:A.+b', b'nomatch', b'fooadef', b'fooadefBar')
350 ('re', 'A.+b', [False, False, True])
350 ('re', 'A.+b', [False, False, True])
351
351
352 case insensitive literal matches
352 case insensitive literal matches
353 >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
353 >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
354 ('literal', 'ABCDEFG', [False, False, True])
354 ('literal', 'ABCDEFG', [False, False, True])
355 """
355 """
356 kind, pattern = _splitpattern(pattern)
356 kind, pattern = _splitpattern(pattern)
357 if kind == b're':
357 if kind == b're':
358 try:
358 try:
359 flags = 0
359 flags = 0
360 if not casesensitive:
360 if not casesensitive:
361 flags = remod.I
361 flags = remod.I
362 regex = remod.compile(pattern, flags)
362 regex = remod.compile(pattern, flags)
363 except remod.error as e:
363 except remod.error as e:
364 raise error.ParseError(
364 raise error.ParseError(
365 _(b'invalid regular expression: %s') % forcebytestr(e)
365 _(b'invalid regular expression: %s') % forcebytestr(e)
366 )
366 )
367 return kind, pattern, regex.search
367 return kind, pattern, regex.search
368 elif kind == b'literal':
368 elif kind == b'literal':
369 if casesensitive:
369 if casesensitive:
370 match = pattern.__eq__
370 match = pattern.__eq__
371 else:
371 else:
372 ipat = encoding.lower(pattern)
372 ipat = encoding.lower(pattern)
373 match = lambda s: ipat == encoding.lower(s)
373 match = lambda s: ipat == encoding.lower(s)
374 return kind, pattern, match
374 return kind, pattern, match
375
375
376 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
376 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
377
377
378
378
379 def substringregexp(pattern, flags=0):
379 def substringregexp(pattern, flags=0):
380 """Build a regexp object from a string pattern possibly starting with
380 """Build a regexp object from a string pattern possibly starting with
381 're:' or 'literal:' prefix.
381 're:' or 'literal:' prefix.
382
382
383 helper for tests:
383 helper for tests:
384 >>> def test(pattern, *tests):
384 >>> def test(pattern, *tests):
385 ... regexp = substringregexp(pattern)
385 ... regexp = substringregexp(pattern)
386 ... return [bool(regexp.search(t)) for t in tests]
386 ... return [bool(regexp.search(t)) for t in tests]
387 >>> def itest(pattern, *tests):
387 >>> def itest(pattern, *tests):
388 ... regexp = substringregexp(pattern, remod.I)
388 ... regexp = substringregexp(pattern, remod.I)
389 ... return [bool(regexp.search(t)) for t in tests]
389 ... return [bool(regexp.search(t)) for t in tests]
390
390
391 substring matching (no prefix):
391 substring matching (no prefix):
392 >>> test(b'bcde', b'abc', b'def', b'abcdefg')
392 >>> test(b'bcde', b'abc', b'def', b'abcdefg')
393 [False, False, True]
393 [False, False, True]
394
394
395 substring pattern should be escaped:
395 substring pattern should be escaped:
396 >>> substringregexp(b'.bc').pattern
396 >>> substringregexp(b'.bc').pattern
397 '\\\\.bc'
397 '\\\\.bc'
398 >>> test(b'.bc', b'abc', b'def', b'abcdefg')
398 >>> test(b'.bc', b'abc', b'def', b'abcdefg')
399 [False, False, False]
399 [False, False, False]
400
400
401 regex matching ('re:' prefix)
401 regex matching ('re:' prefix)
402 >>> test(b're:a.+b', b'nomatch', b'fooadef', b'fooadefbar')
402 >>> test(b're:a.+b', b'nomatch', b'fooadef', b'fooadefbar')
403 [False, False, True]
403 [False, False, True]
404
404
405 force substring matches ('literal:' prefix)
405 force substring matches ('literal:' prefix)
406 >>> test(b'literal:re:foobar', b'foobar', b're:foobar')
406 >>> test(b'literal:re:foobar', b'foobar', b're:foobar')
407 [False, True]
407 [False, True]
408
408
409 case insensitive literal matches
409 case insensitive literal matches
410 >>> itest(b'BCDE', b'abc', b'def', b'abcdefg')
410 >>> itest(b'BCDE', b'abc', b'def', b'abcdefg')
411 [False, False, True]
411 [False, False, True]
412
412
413 case insensitive regex matches
413 case insensitive regex matches
414 >>> itest(b're:A.+b', b'nomatch', b'fooadef', b'fooadefBar')
414 >>> itest(b're:A.+b', b'nomatch', b'fooadef', b'fooadefBar')
415 [False, False, True]
415 [False, False, True]
416 """
416 """
417 kind, pattern = _splitpattern(pattern)
417 kind, pattern = _splitpattern(pattern)
418 if kind == b're':
418 if kind == b're':
419 try:
419 try:
420 return remod.compile(pattern, flags)
420 return remod.compile(pattern, flags)
421 except remod.error as e:
421 except remod.error as e:
422 raise error.ParseError(
422 raise error.ParseError(
423 _(b'invalid regular expression: %s') % forcebytestr(e)
423 _(b'invalid regular expression: %s') % forcebytestr(e)
424 )
424 )
425 elif kind == b'literal':
425 elif kind == b'literal':
426 return remod.compile(remod.escape(pattern), flags)
426 return remod.compile(remod.escape(pattern), flags)
427
427
428 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
428 raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
429
429
430
430
431 def shortuser(user):
431 def shortuser(user):
432 """Return a short representation of a user name or email address."""
432 """Return a short representation of a user name or email address."""
433 f = user.find(b'@')
433 f = user.find(b'@')
434 if f >= 0:
434 if f >= 0:
435 user = user[:f]
435 user = user[:f]
436 f = user.find(b'<')
436 f = user.find(b'<')
437 if f >= 0:
437 if f >= 0:
438 user = user[f + 1 :]
438 user = user[f + 1 :]
439 f = user.find(b' ')
439 f = user.find(b' ')
440 if f >= 0:
440 if f >= 0:
441 user = user[:f]
441 user = user[:f]
442 f = user.find(b'.')
442 f = user.find(b'.')
443 if f >= 0:
443 if f >= 0:
444 user = user[:f]
444 user = user[:f]
445 return user
445 return user
446
446
447
447
448 def emailuser(user):
448 def emailuser(user):
449 """Return the user portion of an email address."""
449 """Return the user portion of an email address."""
450 f = user.find(b'@')
450 f = user.find(b'@')
451 if f >= 0:
451 if f >= 0:
452 user = user[:f]
452 user = user[:f]
453 f = user.find(b'<')
453 f = user.find(b'<')
454 if f >= 0:
454 if f >= 0:
455 user = user[f + 1 :]
455 user = user[f + 1 :]
456 return user
456 return user
457
457
458
458
459 def email(author):
459 def email(author):
460 '''get email of author.'''
460 '''get email of author.'''
461 r = author.find(b'>')
461 r = author.find(b'>')
462 if r == -1:
462 if r == -1:
463 r = None
463 r = None
464 return author[author.find(b'<') + 1 : r]
464 return author[author.find(b'<') + 1 : r]
465
465
466
466
467 def person(author):
467 def person(author):
468 """Returns the name before an email address,
468 """Returns the name before an email address,
469 interpreting it as per RFC 5322
469 interpreting it as per RFC 5322
470
470
471 >>> person(b'foo@bar')
471 >>> person(b'foo@bar')
472 'foo'
472 'foo'
473 >>> person(b'Foo Bar <foo@bar>')
473 >>> person(b'Foo Bar <foo@bar>')
474 'Foo Bar'
474 'Foo Bar'
475 >>> person(b'"Foo Bar" <foo@bar>')
475 >>> person(b'"Foo Bar" <foo@bar>')
476 'Foo Bar'
476 'Foo Bar'
477 >>> person(b'"Foo \"buz\" Bar" <foo@bar>')
477 >>> person(b'"Foo \"buz\" Bar" <foo@bar>')
478 'Foo "buz" Bar'
478 'Foo "buz" Bar'
479 >>> # The following are invalid, but do exist in real-life
479 >>> # The following are invalid, but do exist in real-life
480 ...
480 ...
481 >>> person(b'Foo "buz" Bar <foo@bar>')
481 >>> person(b'Foo "buz" Bar <foo@bar>')
482 'Foo "buz" Bar'
482 'Foo "buz" Bar'
483 >>> person(b'"Foo Bar <foo@bar>')
483 >>> person(b'"Foo Bar <foo@bar>')
484 'Foo Bar'
484 'Foo Bar'
485 """
485 """
486 if b'@' not in author:
486 if b'@' not in author:
487 return author
487 return author
488 f = author.find(b'<')
488 f = author.find(b'<')
489 if f != -1:
489 if f != -1:
490 return author[:f].strip(b' "').replace(b'\\"', b'"')
490 return author[:f].strip(b' "').replace(b'\\"', b'"')
491 f = author.find(b'@')
491 f = author.find(b'@')
492 return author[:f].replace(b'.', b' ')
492 return author[:f].replace(b'.', b' ')
493
493
494
494
495 @attr.s(hash=True)
495 @attr.s(hash=True)
496 class mailmapping(object):
496 class mailmapping(object):
497 """Represents a username/email key or value in
497 """Represents a username/email key or value in
498 a mailmap file"""
498 a mailmap file"""
499
499
500 email = attr.ib()
500 email = attr.ib()
501 name = attr.ib(default=None)
501 name = attr.ib(default=None)
502
502
503
503
504 def _ismailmaplineinvalid(names, emails):
504 def _ismailmaplineinvalid(names, emails):
505 """Returns True if the parsed names and emails
505 """Returns True if the parsed names and emails
506 in a mailmap entry are invalid.
506 in a mailmap entry are invalid.
507
507
508 >>> # No names or emails fails
508 >>> # No names or emails fails
509 >>> names, emails = [], []
509 >>> names, emails = [], []
510 >>> _ismailmaplineinvalid(names, emails)
510 >>> _ismailmaplineinvalid(names, emails)
511 True
511 True
512 >>> # Only one email fails
512 >>> # Only one email fails
513 >>> emails = [b'email@email.com']
513 >>> emails = [b'email@email.com']
514 >>> _ismailmaplineinvalid(names, emails)
514 >>> _ismailmaplineinvalid(names, emails)
515 True
515 True
516 >>> # One email and one name passes
516 >>> # One email and one name passes
517 >>> names = [b'Test Name']
517 >>> names = [b'Test Name']
518 >>> _ismailmaplineinvalid(names, emails)
518 >>> _ismailmaplineinvalid(names, emails)
519 False
519 False
520 >>> # No names but two emails passes
520 >>> # No names but two emails passes
521 >>> names = []
521 >>> names = []
522 >>> emails = [b'proper@email.com', b'commit@email.com']
522 >>> emails = [b'proper@email.com', b'commit@email.com']
523 >>> _ismailmaplineinvalid(names, emails)
523 >>> _ismailmaplineinvalid(names, emails)
524 False
524 False
525 """
525 """
526 return not emails or not names and len(emails) < 2
526 return not emails or not names and len(emails) < 2
527
527
528
528
529 def parsemailmap(mailmapcontent):
529 def parsemailmap(mailmapcontent):
530 """Parses data in the .mailmap format
530 """Parses data in the .mailmap format
531
531
532 >>> mmdata = b"\\n".join([
532 >>> mmdata = b"\\n".join([
533 ... b'# Comment',
533 ... b'# Comment',
534 ... b'Name <commit1@email.xx>',
534 ... b'Name <commit1@email.xx>',
535 ... b'<name@email.xx> <commit2@email.xx>',
535 ... b'<name@email.xx> <commit2@email.xx>',
536 ... b'Name <proper@email.xx> <commit3@email.xx>',
536 ... b'Name <proper@email.xx> <commit3@email.xx>',
537 ... b'Name <proper@email.xx> Commit <commit4@email.xx>',
537 ... b'Name <proper@email.xx> Commit <commit4@email.xx>',
538 ... ])
538 ... ])
539 >>> mm = parsemailmap(mmdata)
539 >>> mm = parsemailmap(mmdata)
540 >>> for key in sorted(mm.keys()):
540 >>> for key in sorted(mm.keys()):
541 ... print(key)
541 ... print(key)
542 mailmapping(email='commit1@email.xx', name=None)
542 mailmapping(email='commit1@email.xx', name=None)
543 mailmapping(email='commit2@email.xx', name=None)
543 mailmapping(email='commit2@email.xx', name=None)
544 mailmapping(email='commit3@email.xx', name=None)
544 mailmapping(email='commit3@email.xx', name=None)
545 mailmapping(email='commit4@email.xx', name='Commit')
545 mailmapping(email='commit4@email.xx', name='Commit')
546 >>> for val in sorted(mm.values()):
546 >>> for val in sorted(mm.values()):
547 ... print(val)
547 ... print(val)
548 mailmapping(email='commit1@email.xx', name='Name')
548 mailmapping(email='commit1@email.xx', name='Name')
549 mailmapping(email='name@email.xx', name=None)
549 mailmapping(email='name@email.xx', name=None)
550 mailmapping(email='proper@email.xx', name='Name')
550 mailmapping(email='proper@email.xx', name='Name')
551 mailmapping(email='proper@email.xx', name='Name')
551 mailmapping(email='proper@email.xx', name='Name')
552 """
552 """
553 mailmap = {}
553 mailmap = {}
554
554
555 if mailmapcontent is None:
555 if mailmapcontent is None:
556 return mailmap
556 return mailmap
557
557
558 for line in mailmapcontent.splitlines():
558 for line in mailmapcontent.splitlines():
559
559
560 # Don't bother checking the line if it is a comment or
560 # Don't bother checking the line if it is a comment or
561 # is an improperly formed author field
561 # is an improperly formed author field
562 if line.lstrip().startswith(b'#'):
562 if line.lstrip().startswith(b'#'):
563 continue
563 continue
564
564
565 # names, emails hold the parsed emails and names for each line
565 # names, emails hold the parsed emails and names for each line
566 # name_builder holds the words in a persons name
566 # name_builder holds the words in a persons name
567 names, emails = [], []
567 names, emails = [], []
568 namebuilder = []
568 namebuilder = []
569
569
570 for element in line.split():
570 for element in line.split():
571 if element.startswith(b'#'):
571 if element.startswith(b'#'):
572 # If we reach a comment in the mailmap file, move on
572 # If we reach a comment in the mailmap file, move on
573 break
573 break
574
574
575 elif element.startswith(b'<') and element.endswith(b'>'):
575 elif element.startswith(b'<') and element.endswith(b'>'):
576 # We have found an email.
576 # We have found an email.
577 # Parse it, and finalize any names from earlier
577 # Parse it, and finalize any names from earlier
578 emails.append(element[1:-1]) # Slice off the "<>"
578 emails.append(element[1:-1]) # Slice off the "<>"
579
579
580 if namebuilder:
580 if namebuilder:
581 names.append(b' '.join(namebuilder))
581 names.append(b' '.join(namebuilder))
582 namebuilder = []
582 namebuilder = []
583
583
584 # Break if we have found a second email, any other
584 # Break if we have found a second email, any other
585 # data does not fit the spec for .mailmap
585 # data does not fit the spec for .mailmap
586 if len(emails) > 1:
586 if len(emails) > 1:
587 break
587 break
588
588
589 else:
589 else:
590 # We have found another word in the committers name
590 # We have found another word in the committers name
591 namebuilder.append(element)
591 namebuilder.append(element)
592
592
593 # Check to see if we have parsed the line into a valid form
593 # Check to see if we have parsed the line into a valid form
594 # We require at least one email, and either at least one
594 # We require at least one email, and either at least one
595 # name or a second email
595 # name or a second email
596 if _ismailmaplineinvalid(names, emails):
596 if _ismailmaplineinvalid(names, emails):
597 continue
597 continue
598
598
599 mailmapkey = mailmapping(
599 mailmapkey = mailmapping(
600 email=emails[-1],
600 email=emails[-1],
601 name=names[-1] if len(names) == 2 else None,
601 name=names[-1] if len(names) == 2 else None,
602 )
602 )
603
603
604 mailmap[mailmapkey] = mailmapping(
604 mailmap[mailmapkey] = mailmapping(
605 email=emails[0],
605 email=emails[0],
606 name=names[0] if names else None,
606 name=names[0] if names else None,
607 )
607 )
608
608
609 return mailmap
609 return mailmap
610
610
611
611
612 def mapname(mailmap, author):
612 def mapname(mailmap, author):
613 """Returns the author field according to the mailmap cache, or
613 """Returns the author field according to the mailmap cache, or
614 the original author field.
614 the original author field.
615
615
616 >>> mmdata = b"\\n".join([
616 >>> mmdata = b"\\n".join([
617 ... b'# Comment',
617 ... b'# Comment',
618 ... b'Name <commit1@email.xx>',
618 ... b'Name <commit1@email.xx>',
619 ... b'<name@email.xx> <commit2@email.xx>',
619 ... b'<name@email.xx> <commit2@email.xx>',
620 ... b'Name <proper@email.xx> <commit3@email.xx>',
620 ... b'Name <proper@email.xx> <commit3@email.xx>',
621 ... b'Name <proper@email.xx> Commit <commit4@email.xx>',
621 ... b'Name <proper@email.xx> Commit <commit4@email.xx>',
622 ... ])
622 ... ])
623 >>> m = parsemailmap(mmdata)
623 >>> m = parsemailmap(mmdata)
624 >>> mapname(m, b'Commit <commit1@email.xx>')
624 >>> mapname(m, b'Commit <commit1@email.xx>')
625 'Name <commit1@email.xx>'
625 'Name <commit1@email.xx>'
626 >>> mapname(m, b'Name <commit2@email.xx>')
626 >>> mapname(m, b'Name <commit2@email.xx>')
627 'Name <name@email.xx>'
627 'Name <name@email.xx>'
628 >>> mapname(m, b'Commit <commit3@email.xx>')
628 >>> mapname(m, b'Commit <commit3@email.xx>')
629 'Name <proper@email.xx>'
629 'Name <proper@email.xx>'
630 >>> mapname(m, b'Commit <commit4@email.xx>')
630 >>> mapname(m, b'Commit <commit4@email.xx>')
631 'Name <proper@email.xx>'
631 'Name <proper@email.xx>'
632 >>> mapname(m, b'Unknown Name <unknown@email.com>')
632 >>> mapname(m, b'Unknown Name <unknown@email.com>')
633 'Unknown Name <unknown@email.com>'
633 'Unknown Name <unknown@email.com>'
634 """
634 """
635 # If the author field coming in isn't in the correct format,
635 # If the author field coming in isn't in the correct format,
636 # or the mailmap is empty just return the original author field
636 # or the mailmap is empty just return the original author field
637 if not isauthorwellformed(author) or not mailmap:
637 if not isauthorwellformed(author) or not mailmap:
638 return author
638 return author
639
639
640 # Turn the user name into a mailmapping
640 # Turn the user name into a mailmapping
641 commit = mailmapping(name=person(author), email=email(author))
641 commit = mailmapping(name=person(author), email=email(author))
642
642
643 try:
643 try:
644 # Try and use both the commit email and name as the key
644 # Try and use both the commit email and name as the key
645 proper = mailmap[commit]
645 proper = mailmap[commit]
646
646
647 except KeyError:
647 except KeyError:
648 # If the lookup fails, use just the email as the key instead
648 # If the lookup fails, use just the email as the key instead
649 # We call this commit2 as not to erase original commit fields
649 # We call this commit2 as not to erase original commit fields
650 commit2 = mailmapping(email=commit.email)
650 commit2 = mailmapping(email=commit.email)
651 proper = mailmap.get(commit2, mailmapping(None, None))
651 proper = mailmap.get(commit2, mailmapping(None, None))
652
652
653 # Return the author field with proper values filled in
653 # Return the author field with proper values filled in
654 return b'%s <%s>' % (
654 return b'%s <%s>' % (
655 proper.name if proper.name else commit.name,
655 proper.name if proper.name else commit.name,
656 proper.email if proper.email else commit.email,
656 proper.email if proper.email else commit.email,
657 )
657 )
658
658
659
659
660 _correctauthorformat = remod.compile(br'^[^<]+\s<[^<>]+@[^<>]+>$')
660 _correctauthorformat = remod.compile(br'^[^<]+\s<[^<>]+@[^<>]+>$')
661
661
662
662
663 def isauthorwellformed(author):
663 def isauthorwellformed(author):
664 """Return True if the author field is well formed
664 """Return True if the author field is well formed
665 (ie "Contributor Name <contrib@email.dom>")
665 (ie "Contributor Name <contrib@email.dom>")
666
666
667 >>> isauthorwellformed(b'Good Author <good@author.com>')
667 >>> isauthorwellformed(b'Good Author <good@author.com>')
668 True
668 True
669 >>> isauthorwellformed(b'Author <good@author.com>')
669 >>> isauthorwellformed(b'Author <good@author.com>')
670 True
670 True
671 >>> isauthorwellformed(b'Bad Author')
671 >>> isauthorwellformed(b'Bad Author')
672 False
672 False
673 >>> isauthorwellformed(b'Bad Author <author@author.com')
673 >>> isauthorwellformed(b'Bad Author <author@author.com')
674 False
674 False
675 >>> isauthorwellformed(b'Bad Author author@author.com')
675 >>> isauthorwellformed(b'Bad Author author@author.com')
676 False
676 False
677 >>> isauthorwellformed(b'<author@author.com>')
677 >>> isauthorwellformed(b'<author@author.com>')
678 False
678 False
679 >>> isauthorwellformed(b'Bad Author <author>')
679 >>> isauthorwellformed(b'Bad Author <author>')
680 False
680 False
681 """
681 """
682 return _correctauthorformat.match(author) is not None
682 return _correctauthorformat.match(author) is not None
683
683
684
684
685 def ellipsis(text, maxlength=400):
685 def ellipsis(text, maxlength=400):
686 """Trim string to at most maxlength (default: 400) columns in display."""
686 """Trim string to at most maxlength (default: 400) columns in display."""
687 return encoding.trim(text, maxlength, ellipsis=b'...')
687 return encoding.trim(text, maxlength, ellipsis=b'...')
688
688
689
689
690 def escapestr(s):
690 def escapestr(s):
691 if isinstance(s, memoryview):
691 if isinstance(s, memoryview):
692 s = bytes(s)
692 s = bytes(s)
693 # call underlying function of s.encode('string_escape') directly for
693 # call underlying function of s.encode('string_escape') directly for
694 # Python 3 compatibility
694 # Python 3 compatibility
695 return codecs.escape_encode(s)[0]
695 return codecs.escape_encode(s)[0]
696
696
697
697
698 def unescapestr(s):
698 def unescapestr(s):
699 return codecs.escape_decode(s)[0]
699 return codecs.escape_decode(s)[0]
700
700
701
701
702 def forcebytestr(obj):
702 def forcebytestr(obj):
703 """Portably format an arbitrary object (e.g. exception) into a byte
703 """Portably format an arbitrary object (e.g. exception) into a byte
704 string."""
704 string."""
705 try:
705 try:
706 return pycompat.bytestr(obj)
706 return pycompat.bytestr(obj)
707 except UnicodeEncodeError:
707 except UnicodeEncodeError:
708 # non-ascii string, may be lossy
708 # non-ascii string, may be lossy
709 return pycompat.bytestr(encoding.strtolocal(str(obj)))
709 return pycompat.bytestr(encoding.strtolocal(str(obj)))
710
710
711
711
712 def uirepr(s):
712 def uirepr(s):
713 # Avoid double backslash in Windows path repr()
713 # Avoid double backslash in Windows path repr()
714 return pycompat.byterepr(pycompat.bytestr(s)).replace(b'\\\\', b'\\')
714 return pycompat.byterepr(pycompat.bytestr(s)).replace(b'\\\\', b'\\')
715
715
716
716
717 # delay import of textwrap
717 # delay import of textwrap
718 def _MBTextWrapper(**kwargs):
718 def _MBTextWrapper(**kwargs):
719 class tw(textwrap.TextWrapper):
719 class tw(textwrap.TextWrapper):
720 """
720 """
721 Extend TextWrapper for width-awareness.
721 Extend TextWrapper for width-awareness.
722
722
723 Neither number of 'bytes' in any encoding nor 'characters' is
723 Neither number of 'bytes' in any encoding nor 'characters' is
724 appropriate to calculate terminal columns for specified string.
724 appropriate to calculate terminal columns for specified string.
725
725
726 Original TextWrapper implementation uses built-in 'len()' directly,
726 Original TextWrapper implementation uses built-in 'len()' directly,
727 so overriding is needed to use width information of each characters.
727 so overriding is needed to use width information of each characters.
728
728
729 In addition, characters classified into 'ambiguous' width are
729 In addition, characters classified into 'ambiguous' width are
730 treated as wide in East Asian area, but as narrow in other.
730 treated as wide in East Asian area, but as narrow in other.
731
731
732 This requires use decision to determine width of such characters.
732 This requires use decision to determine width of such characters.
733 """
733 """
734
734
735 def _cutdown(self, ucstr, space_left):
735 def _cutdown(self, ucstr, space_left):
736 l = 0
736 l = 0
737 colwidth = encoding.ucolwidth
737 colwidth = encoding.ucolwidth
738 for i in pycompat.xrange(len(ucstr)):
738 for i in pycompat.xrange(len(ucstr)):
739 l += colwidth(ucstr[i])
739 l += colwidth(ucstr[i])
740 if space_left < l:
740 if space_left < l:
741 return (ucstr[:i], ucstr[i:])
741 return (ucstr[:i], ucstr[i:])
742 return ucstr, b''
742 return ucstr, b''
743
743
744 # overriding of base class
744 # overriding of base class
745 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
745 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
746 space_left = max(width - cur_len, 1)
746 space_left = max(width - cur_len, 1)
747
747
748 if self.break_long_words:
748 if self.break_long_words:
749 cut, res = self._cutdown(reversed_chunks[-1], space_left)
749 cut, res = self._cutdown(reversed_chunks[-1], space_left)
750 cur_line.append(cut)
750 cur_line.append(cut)
751 reversed_chunks[-1] = res
751 reversed_chunks[-1] = res
752 elif not cur_line:
752 elif not cur_line:
753 cur_line.append(reversed_chunks.pop())
753 cur_line.append(reversed_chunks.pop())
754
754
755 # this overriding code is imported from TextWrapper of Python 2.6
755 # this overriding code is imported from TextWrapper of Python 2.6
756 # to calculate columns of string by 'encoding.ucolwidth()'
756 # to calculate columns of string by 'encoding.ucolwidth()'
757 def _wrap_chunks(self, chunks):
757 def _wrap_chunks(self, chunks):
758 colwidth = encoding.ucolwidth
758 colwidth = encoding.ucolwidth
759
759
760 lines = []
760 lines = []
761 if self.width <= 0:
761 if self.width <= 0:
762 raise ValueError(b"invalid width %r (must be > 0)" % self.width)
762 raise ValueError(b"invalid width %r (must be > 0)" % self.width)
763
763
764 # Arrange in reverse order so items can be efficiently popped
764 # Arrange in reverse order so items can be efficiently popped
765 # from a stack of chucks.
765 # from a stack of chucks.
766 chunks.reverse()
766 chunks.reverse()
767
767
768 while chunks:
768 while chunks:
769
769
770 # Start the list of chunks that will make up the current line.
770 # Start the list of chunks that will make up the current line.
771 # cur_len is just the length of all the chunks in cur_line.
771 # cur_len is just the length of all the chunks in cur_line.
772 cur_line = []
772 cur_line = []
773 cur_len = 0
773 cur_len = 0
774
774
775 # Figure out which static string will prefix this line.
775 # Figure out which static string will prefix this line.
776 if lines:
776 if lines:
777 indent = self.subsequent_indent
777 indent = self.subsequent_indent
778 else:
778 else:
779 indent = self.initial_indent
779 indent = self.initial_indent
780
780
781 # Maximum width for this line.
781 # Maximum width for this line.
782 width = self.width - len(indent)
782 width = self.width - len(indent)
783
783
784 # First chunk on line is whitespace -- drop it, unless this
784 # First chunk on line is whitespace -- drop it, unless this
785 # is the very beginning of the text (i.e. no lines started yet).
785 # is the very beginning of the text (i.e. no lines started yet).
786 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
786 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
787 del chunks[-1]
787 del chunks[-1]
788
788
789 while chunks:
789 while chunks:
790 l = colwidth(chunks[-1])
790 l = colwidth(chunks[-1])
791
791
792 # Can at least squeeze this chunk onto the current line.
792 # Can at least squeeze this chunk onto the current line.
793 if cur_len + l <= width:
793 if cur_len + l <= width:
794 cur_line.append(chunks.pop())
794 cur_line.append(chunks.pop())
795 cur_len += l
795 cur_len += l
796
796
797 # Nope, this line is full.
797 # Nope, this line is full.
798 else:
798 else:
799 break
799 break
800
800
801 # The current line is full, and the next chunk is too big to
801 # The current line is full, and the next chunk is too big to
802 # fit on *any* line (not just this one).
802 # fit on *any* line (not just this one).
803 if chunks and colwidth(chunks[-1]) > width:
803 if chunks and colwidth(chunks[-1]) > width:
804 self._handle_long_word(chunks, cur_line, cur_len, width)
804 self._handle_long_word(chunks, cur_line, cur_len, width)
805
805
806 # If the last chunk on this line is all whitespace, drop it.
806 # If the last chunk on this line is all whitespace, drop it.
807 if (
807 if (
808 self.drop_whitespace
808 self.drop_whitespace
809 and cur_line
809 and cur_line
810 and cur_line[-1].strip() == r''
810 and cur_line[-1].strip() == r''
811 ):
811 ):
812 del cur_line[-1]
812 del cur_line[-1]
813
813
814 # Convert current line back to a string and store it in list
814 # Convert current line back to a string and store it in list
815 # of all lines (return value).
815 # of all lines (return value).
816 if cur_line:
816 if cur_line:
817 lines.append(indent + ''.join(cur_line))
817 lines.append(indent + ''.join(cur_line))
818
818
819 return lines
819 return lines
820
820
821 global _MBTextWrapper
821 global _MBTextWrapper
822 _MBTextWrapper = tw
822 _MBTextWrapper = tw
823 return tw(**kwargs)
823 return tw(**kwargs)
824
824
825
825
826 def wrap(line, width, initindent=b'', hangindent=b''):
826 def wrap(line, width, initindent=b'', hangindent=b''):
827 maxindent = max(len(hangindent), len(initindent))
827 maxindent = max(len(hangindent), len(initindent))
828 if width <= maxindent:
828 if width <= maxindent:
829 # adjust for weird terminal size
829 # adjust for weird terminal size
830 width = max(78, maxindent + 1)
830 width = max(78, maxindent + 1)
831 line = line.decode(
831 line = line.decode(
832 pycompat.sysstr(encoding.encoding),
832 pycompat.sysstr(encoding.encoding),
833 pycompat.sysstr(encoding.encodingmode),
833 pycompat.sysstr(encoding.encodingmode),
834 )
834 )
835 initindent = initindent.decode(
835 initindent = initindent.decode(
836 pycompat.sysstr(encoding.encoding),
836 pycompat.sysstr(encoding.encoding),
837 pycompat.sysstr(encoding.encodingmode),
837 pycompat.sysstr(encoding.encodingmode),
838 )
838 )
839 hangindent = hangindent.decode(
839 hangindent = hangindent.decode(
840 pycompat.sysstr(encoding.encoding),
840 pycompat.sysstr(encoding.encoding),
841 pycompat.sysstr(encoding.encodingmode),
841 pycompat.sysstr(encoding.encodingmode),
842 )
842 )
843 wrapper = _MBTextWrapper(
843 wrapper = _MBTextWrapper(
844 width=width, initial_indent=initindent, subsequent_indent=hangindent
844 width=width, initial_indent=initindent, subsequent_indent=hangindent
845 )
845 )
846 return wrapper.fill(line).encode(pycompat.sysstr(encoding.encoding))
846 return wrapper.fill(line).encode(pycompat.sysstr(encoding.encoding))
847
847
848
848
849 _booleans = {
849 _booleans = {
850 b'1': True,
850 b'1': True,
851 b'yes': True,
851 b'yes': True,
852 b'true': True,
852 b'true': True,
853 b'on': True,
853 b'on': True,
854 b'always': True,
854 b'always': True,
855 b'0': False,
855 b'0': False,
856 b'no': False,
856 b'no': False,
857 b'false': False,
857 b'false': False,
858 b'off': False,
858 b'off': False,
859 b'never': False,
859 b'never': False,
860 }
860 }
861
861
862
862
863 def parsebool(s):
863 def parsebool(s):
864 """Parse s into a boolean.
864 """Parse s into a boolean.
865
865
866 If s is not a valid boolean, returns None.
866 If s is not a valid boolean, returns None.
867 """
867 """
868 return _booleans.get(s.lower(), None)
868 return _booleans.get(s.lower(), None)
869
869
870
870
871 def parselist(value):
872 """parse a configuration value as a list of comma/space separated strings
873
874 >>> parselist(b'this,is "a small" ,test')
875 ['this', 'is', 'a small', 'test']
876 """
877
878 def _parse_plain(parts, s, offset):
879 whitespace = False
880 while offset < len(s) and (
881 s[offset : offset + 1].isspace() or s[offset : offset + 1] == b','
882 ):
883 whitespace = True
884 offset += 1
885 if offset >= len(s):
886 return None, parts, offset
887 if whitespace:
888 parts.append(b'')
889 if s[offset : offset + 1] == b'"' and not parts[-1]:
890 return _parse_quote, parts, offset + 1
891 elif s[offset : offset + 1] == b'"' and parts[-1][-1:] == b'\\':
892 parts[-1] = parts[-1][:-1] + s[offset : offset + 1]
893 return _parse_plain, parts, offset + 1
894 parts[-1] += s[offset : offset + 1]
895 return _parse_plain, parts, offset + 1
896
897 def _parse_quote(parts, s, offset):
898 if offset < len(s) and s[offset : offset + 1] == b'"': # ""
899 parts.append(b'')
900 offset += 1
901 while offset < len(s) and (
902 s[offset : offset + 1].isspace()
903 or s[offset : offset + 1] == b','
904 ):
905 offset += 1
906 return _parse_plain, parts, offset
907
908 while offset < len(s) and s[offset : offset + 1] != b'"':
909 if (
910 s[offset : offset + 1] == b'\\'
911 and offset + 1 < len(s)
912 and s[offset + 1 : offset + 2] == b'"'
913 ):
914 offset += 1
915 parts[-1] += b'"'
916 else:
917 parts[-1] += s[offset : offset + 1]
918 offset += 1
919
920 if offset >= len(s):
921 real_parts = _configlist(parts[-1])
922 if not real_parts:
923 parts[-1] = b'"'
924 else:
925 real_parts[0] = b'"' + real_parts[0]
926 parts = parts[:-1]
927 parts.extend(real_parts)
928 return None, parts, offset
929
930 offset += 1
931 while offset < len(s) and s[offset : offset + 1] in [b' ', b',']:
932 offset += 1
933
934 if offset < len(s):
935 if offset + 1 == len(s) and s[offset : offset + 1] == b'"':
936 parts[-1] += b'"'
937 offset += 1
938 else:
939 parts.append(b'')
940 else:
941 return None, parts, offset
942
943 return _parse_plain, parts, offset
944
945 def _configlist(s):
946 s = s.rstrip(b' ,')
947 if not s:
948 return []
949 parser, parts, offset = _parse_plain, [b''], 0
950 while parser:
951 parser, parts, offset = parser(parts, s, offset)
952 return parts
953
954 if value is not None and isinstance(value, bytes):
955 result = _configlist(value.lstrip(b' ,\n'))
956 else:
957 result = value
958 return result or []
959
960
871 def evalpythonliteral(s):
961 def evalpythonliteral(s):
872 """Evaluate a string containing a Python literal expression"""
962 """Evaluate a string containing a Python literal expression"""
873 # We could backport our tokenizer hack to rewrite '' to u'' if we want
963 # We could backport our tokenizer hack to rewrite '' to u'' if we want
874 if pycompat.ispy3:
964 if pycompat.ispy3:
875 return ast.literal_eval(s.decode('latin1'))
965 return ast.literal_eval(s.decode('latin1'))
876 return ast.literal_eval(s)
966 return ast.literal_eval(s)
@@ -1,480 +1,481 b''
1 // config.rs
1 // config.rs
2 //
2 //
3 // Copyright 2020
3 // Copyright 2020
4 // Valentin Gatien-Baron,
4 // Valentin Gatien-Baron,
5 // Raphaël Gomès <rgomes@octobus.net>
5 // Raphaël Gomès <rgomes@octobus.net>
6 //
6 //
7 // This software may be used and distributed according to the terms of the
7 // This software may be used and distributed according to the terms of the
8 // GNU General Public License version 2 or any later version.
8 // GNU General Public License version 2 or any later version.
9
9
10 use super::layer;
10 use super::layer;
11 use super::values;
11 use super::values;
12 use crate::config::layer::{
12 use crate::config::layer::{
13 ConfigError, ConfigLayer, ConfigOrigin, ConfigValue,
13 ConfigError, ConfigLayer, ConfigOrigin, ConfigValue,
14 };
14 };
15 use crate::utils::files::get_bytes_from_os_str;
15 use crate::utils::files::get_bytes_from_os_str;
16 use crate::utils::SliceExt;
16 use crate::utils::SliceExt;
17 use format_bytes::{write_bytes, DisplayBytes};
17 use format_bytes::{write_bytes, DisplayBytes};
18 use std::collections::HashSet;
18 use std::collections::HashSet;
19 use std::env;
19 use std::env;
20 use std::fmt;
20 use std::fmt;
21 use std::path::{Path, PathBuf};
21 use std::path::{Path, PathBuf};
22 use std::str;
22 use std::str;
23
23
24 use crate::errors::{HgResultExt, IoResultExt};
24 use crate::errors::{HgResultExt, IoResultExt};
25
25
26 /// Holds the config values for the current repository
26 /// Holds the config values for the current repository
27 /// TODO update this docstring once we support more sources
27 /// TODO update this docstring once we support more sources
28 #[derive(Clone)]
28 #[derive(Clone)]
29 pub struct Config {
29 pub struct Config {
30 layers: Vec<layer::ConfigLayer>,
30 layers: Vec<layer::ConfigLayer>,
31 }
31 }
32
32
33 impl DisplayBytes for Config {
33 impl DisplayBytes for Config {
34 fn display_bytes(
34 fn display_bytes(
35 &self,
35 &self,
36 out: &mut dyn std::io::Write,
36 out: &mut dyn std::io::Write,
37 ) -> std::io::Result<()> {
37 ) -> std::io::Result<()> {
38 for (index, layer) in self.layers.iter().rev().enumerate() {
38 for (index, layer) in self.layers.iter().rev().enumerate() {
39 write_bytes!(
39 write_bytes!(
40 out,
40 out,
41 b"==== Layer {} (trusted: {}) ====\n{}",
41 b"==== Layer {} (trusted: {}) ====\n{}",
42 index,
42 index,
43 if layer.trusted {
43 if layer.trusted {
44 &b"yes"[..]
44 &b"yes"[..]
45 } else {
45 } else {
46 &b"no"[..]
46 &b"no"[..]
47 },
47 },
48 layer
48 layer
49 )?;
49 )?;
50 }
50 }
51 Ok(())
51 Ok(())
52 }
52 }
53 }
53 }
54
54
55 pub enum ConfigSource {
55 pub enum ConfigSource {
56 /// Absolute path to a config file
56 /// Absolute path to a config file
57 AbsPath(PathBuf),
57 AbsPath(PathBuf),
58 /// Already parsed (from the CLI, env, Python resources, etc.)
58 /// Already parsed (from the CLI, env, Python resources, etc.)
59 Parsed(layer::ConfigLayer),
59 Parsed(layer::ConfigLayer),
60 }
60 }
61
61
62 #[derive(Debug)]
62 #[derive(Debug)]
63 pub struct ConfigValueParseError {
63 pub struct ConfigValueParseError {
64 pub origin: ConfigOrigin,
64 pub origin: ConfigOrigin,
65 pub line: Option<usize>,
65 pub line: Option<usize>,
66 pub section: Vec<u8>,
66 pub section: Vec<u8>,
67 pub item: Vec<u8>,
67 pub item: Vec<u8>,
68 pub value: Vec<u8>,
68 pub value: Vec<u8>,
69 pub expected_type: &'static str,
69 pub expected_type: &'static str,
70 }
70 }
71
71
72 impl fmt::Display for ConfigValueParseError {
72 impl fmt::Display for ConfigValueParseError {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 // TODO: add origin and line number information, here and in
74 // TODO: add origin and line number information, here and in
75 // corresponding python code
75 // corresponding python code
76 write!(
76 write!(
77 f,
77 f,
78 "config error: {}.{} is not a {} ('{}')",
78 "config error: {}.{} is not a {} ('{}')",
79 String::from_utf8_lossy(&self.section),
79 String::from_utf8_lossy(&self.section),
80 String::from_utf8_lossy(&self.item),
80 String::from_utf8_lossy(&self.item),
81 self.expected_type,
81 self.expected_type,
82 String::from_utf8_lossy(&self.value)
82 String::from_utf8_lossy(&self.value)
83 )
83 )
84 }
84 }
85 }
85 }
86
86
87 impl Config {
87 impl Config {
88 /// Load system and user configuration from various files.
88 /// Load system and user configuration from various files.
89 ///
89 ///
90 /// This is also affected by some environment variables.
90 /// This is also affected by some environment variables.
91 pub fn load(
91 pub fn load(
92 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
92 cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
93 ) -> Result<Self, ConfigError> {
93 ) -> Result<Self, ConfigError> {
94 let mut config = Self { layers: Vec::new() };
94 let mut config = Self { layers: Vec::new() };
95 let opt_rc_path = env::var_os("HGRCPATH");
95 let opt_rc_path = env::var_os("HGRCPATH");
96 // HGRCPATH replaces system config
96 // HGRCPATH replaces system config
97 if opt_rc_path.is_none() {
97 if opt_rc_path.is_none() {
98 config.add_system_config()?
98 config.add_system_config()?
99 }
99 }
100
100
101 config.add_for_environment_variable("EDITOR", b"ui", b"editor");
101 config.add_for_environment_variable("EDITOR", b"ui", b"editor");
102 config.add_for_environment_variable("VISUAL", b"ui", b"editor");
102 config.add_for_environment_variable("VISUAL", b"ui", b"editor");
103 config.add_for_environment_variable("PAGER", b"pager", b"pager");
103 config.add_for_environment_variable("PAGER", b"pager", b"pager");
104
104
105 // These are set by `run-tests.py --rhg` to enable fallback for the
105 // These are set by `run-tests.py --rhg` to enable fallback for the
106 // entire test suite. Alternatives would be setting configuration
106 // entire test suite. Alternatives would be setting configuration
107 // through `$HGRCPATH` but some tests override that, or changing the
107 // through `$HGRCPATH` but some tests override that, or changing the
108 // `hg` shell alias to include `--config` but that disrupts tests that
108 // `hg` shell alias to include `--config` but that disrupts tests that
109 // print command lines and check expected output.
109 // print command lines and check expected output.
110 config.add_for_environment_variable(
110 config.add_for_environment_variable(
111 "RHG_ON_UNSUPPORTED",
111 "RHG_ON_UNSUPPORTED",
112 b"rhg",
112 b"rhg",
113 b"on-unsupported",
113 b"on-unsupported",
114 );
114 );
115 config.add_for_environment_variable(
115 config.add_for_environment_variable(
116 "RHG_FALLBACK_EXECUTABLE",
116 "RHG_FALLBACK_EXECUTABLE",
117 b"rhg",
117 b"rhg",
118 b"fallback-executable",
118 b"fallback-executable",
119 );
119 );
120
120
121 // HGRCPATH replaces user config
121 // HGRCPATH replaces user config
122 if opt_rc_path.is_none() {
122 if opt_rc_path.is_none() {
123 config.add_user_config()?
123 config.add_user_config()?
124 }
124 }
125 if let Some(rc_path) = &opt_rc_path {
125 if let Some(rc_path) = &opt_rc_path {
126 for path in env::split_paths(rc_path) {
126 for path in env::split_paths(rc_path) {
127 if !path.as_os_str().is_empty() {
127 if !path.as_os_str().is_empty() {
128 if path.is_dir() {
128 if path.is_dir() {
129 config.add_trusted_dir(&path)?
129 config.add_trusted_dir(&path)?
130 } else {
130 } else {
131 config.add_trusted_file(&path)?
131 config.add_trusted_file(&path)?
132 }
132 }
133 }
133 }
134 }
134 }
135 }
135 }
136 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
136 if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
137 config.layers.push(layer)
137 config.layers.push(layer)
138 }
138 }
139 Ok(config)
139 Ok(config)
140 }
140 }
141
141
142 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
142 fn add_trusted_dir(&mut self, path: &Path) -> Result<(), ConfigError> {
143 if let Some(entries) = std::fs::read_dir(path)
143 if let Some(entries) = std::fs::read_dir(path)
144 .when_reading_file(path)
144 .when_reading_file(path)
145 .io_not_found_as_none()?
145 .io_not_found_as_none()?
146 {
146 {
147 let mut file_paths = entries
147 let mut file_paths = entries
148 .map(|result| {
148 .map(|result| {
149 result.when_reading_file(path).map(|entry| entry.path())
149 result.when_reading_file(path).map(|entry| entry.path())
150 })
150 })
151 .collect::<Result<Vec<_>, _>>()?;
151 .collect::<Result<Vec<_>, _>>()?;
152 file_paths.sort();
152 file_paths.sort();
153 for file_path in &file_paths {
153 for file_path in &file_paths {
154 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
154 if file_path.extension() == Some(std::ffi::OsStr::new("rc")) {
155 self.add_trusted_file(&file_path)?
155 self.add_trusted_file(&file_path)?
156 }
156 }
157 }
157 }
158 }
158 }
159 Ok(())
159 Ok(())
160 }
160 }
161
161
162 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
162 fn add_trusted_file(&mut self, path: &Path) -> Result<(), ConfigError> {
163 if let Some(data) = std::fs::read(path)
163 if let Some(data) = std::fs::read(path)
164 .when_reading_file(path)
164 .when_reading_file(path)
165 .io_not_found_as_none()?
165 .io_not_found_as_none()?
166 {
166 {
167 self.layers.extend(ConfigLayer::parse(path, &data)?)
167 self.layers.extend(ConfigLayer::parse(path, &data)?)
168 }
168 }
169 Ok(())
169 Ok(())
170 }
170 }
171
171
172 fn add_for_environment_variable(
172 fn add_for_environment_variable(
173 &mut self,
173 &mut self,
174 var: &str,
174 var: &str,
175 section: &[u8],
175 section: &[u8],
176 key: &[u8],
176 key: &[u8],
177 ) {
177 ) {
178 if let Some(value) = env::var_os(var) {
178 if let Some(value) = env::var_os(var) {
179 let origin = layer::ConfigOrigin::Environment(var.into());
179 let origin = layer::ConfigOrigin::Environment(var.into());
180 let mut layer = ConfigLayer::new(origin);
180 let mut layer = ConfigLayer::new(origin);
181 layer.add(
181 layer.add(
182 section.to_owned(),
182 section.to_owned(),
183 key.to_owned(),
183 key.to_owned(),
184 get_bytes_from_os_str(value),
184 get_bytes_from_os_str(value),
185 None,
185 None,
186 );
186 );
187 self.layers.push(layer)
187 self.layers.push(layer)
188 }
188 }
189 }
189 }
190
190
191 #[cfg(unix)] // TODO: other platforms
191 #[cfg(unix)] // TODO: other platforms
192 fn add_system_config(&mut self) -> Result<(), ConfigError> {
192 fn add_system_config(&mut self) -> Result<(), ConfigError> {
193 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
193 let mut add_for_prefix = |prefix: &Path| -> Result<(), ConfigError> {
194 let etc = prefix.join("etc").join("mercurial");
194 let etc = prefix.join("etc").join("mercurial");
195 self.add_trusted_file(&etc.join("hgrc"))?;
195 self.add_trusted_file(&etc.join("hgrc"))?;
196 self.add_trusted_dir(&etc.join("hgrc.d"))
196 self.add_trusted_dir(&etc.join("hgrc.d"))
197 };
197 };
198 let root = Path::new("/");
198 let root = Path::new("/");
199 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
199 // TODO: use `std::env::args_os().next().unwrap()` a.k.a. argv[0]
200 // instead? TODO: can this be a relative path?
200 // instead? TODO: can this be a relative path?
201 let hg = crate::utils::current_exe()?;
201 let hg = crate::utils::current_exe()?;
202 // TODO: this order (per-installation then per-system) matches
202 // TODO: this order (per-installation then per-system) matches
203 // `systemrcpath()` in `mercurial/scmposix.py`, but
203 // `systemrcpath()` in `mercurial/scmposix.py`, but
204 // `mercurial/helptext/config.txt` suggests it should be reversed
204 // `mercurial/helptext/config.txt` suggests it should be reversed
205 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
205 if let Some(installation_prefix) = hg.parent().and_then(Path::parent) {
206 if installation_prefix != root {
206 if installation_prefix != root {
207 add_for_prefix(&installation_prefix)?
207 add_for_prefix(&installation_prefix)?
208 }
208 }
209 }
209 }
210 add_for_prefix(root)?;
210 add_for_prefix(root)?;
211 Ok(())
211 Ok(())
212 }
212 }
213
213
214 #[cfg(unix)] // TODO: other plateforms
214 #[cfg(unix)] // TODO: other plateforms
215 fn add_user_config(&mut self) -> Result<(), ConfigError> {
215 fn add_user_config(&mut self) -> Result<(), ConfigError> {
216 let opt_home = home::home_dir();
216 let opt_home = home::home_dir();
217 if let Some(home) = &opt_home {
217 if let Some(home) = &opt_home {
218 self.add_trusted_file(&home.join(".hgrc"))?
218 self.add_trusted_file(&home.join(".hgrc"))?
219 }
219 }
220 let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
220 let darwin = cfg!(any(target_os = "macos", target_os = "ios"));
221 if !darwin {
221 if !darwin {
222 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
222 if let Some(config_home) = env::var_os("XDG_CONFIG_HOME")
223 .map(PathBuf::from)
223 .map(PathBuf::from)
224 .or_else(|| opt_home.map(|home| home.join(".config")))
224 .or_else(|| opt_home.map(|home| home.join(".config")))
225 {
225 {
226 self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
226 self.add_trusted_file(&config_home.join("hg").join("hgrc"))?
227 }
227 }
228 }
228 }
229 Ok(())
229 Ok(())
230 }
230 }
231
231
232 /// Loads in order, which means that the precedence is the same
232 /// Loads in order, which means that the precedence is the same
233 /// as the order of `sources`.
233 /// as the order of `sources`.
234 pub fn load_from_explicit_sources(
234 pub fn load_from_explicit_sources(
235 sources: Vec<ConfigSource>,
235 sources: Vec<ConfigSource>,
236 ) -> Result<Self, ConfigError> {
236 ) -> Result<Self, ConfigError> {
237 let mut layers = vec![];
237 let mut layers = vec![];
238
238
239 for source in sources.into_iter() {
239 for source in sources.into_iter() {
240 match source {
240 match source {
241 ConfigSource::Parsed(c) => layers.push(c),
241 ConfigSource::Parsed(c) => layers.push(c),
242 ConfigSource::AbsPath(c) => {
242 ConfigSource::AbsPath(c) => {
243 // TODO check if it should be trusted
243 // TODO check if it should be trusted
244 // mercurial/ui.py:427
244 // mercurial/ui.py:427
245 let data = match std::fs::read(&c) {
245 let data = match std::fs::read(&c) {
246 Err(_) => continue, // same as the python code
246 Err(_) => continue, // same as the python code
247 Ok(data) => data,
247 Ok(data) => data,
248 };
248 };
249 layers.extend(ConfigLayer::parse(&c, &data)?)
249 layers.extend(ConfigLayer::parse(&c, &data)?)
250 }
250 }
251 }
251 }
252 }
252 }
253
253
254 Ok(Config { layers })
254 Ok(Config { layers })
255 }
255 }
256
256
257 /// Loads the per-repository config into a new `Config` which is combined
257 /// Loads the per-repository config into a new `Config` which is combined
258 /// with `self`.
258 /// with `self`.
259 pub(crate) fn combine_with_repo(
259 pub(crate) fn combine_with_repo(
260 &self,
260 &self,
261 repo_config_files: &[PathBuf],
261 repo_config_files: &[PathBuf],
262 ) -> Result<Self, ConfigError> {
262 ) -> Result<Self, ConfigError> {
263 let (cli_layers, other_layers) = self
263 let (cli_layers, other_layers) = self
264 .layers
264 .layers
265 .iter()
265 .iter()
266 .cloned()
266 .cloned()
267 .partition(ConfigLayer::is_from_command_line);
267 .partition(ConfigLayer::is_from_command_line);
268
268
269 let mut repo_config = Self {
269 let mut repo_config = Self {
270 layers: other_layers,
270 layers: other_layers,
271 };
271 };
272 for path in repo_config_files {
272 for path in repo_config_files {
273 // TODO: check if this file should be trusted:
273 // TODO: check if this file should be trusted:
274 // `mercurial/ui.py:427`
274 // `mercurial/ui.py:427`
275 repo_config.add_trusted_file(path)?;
275 repo_config.add_trusted_file(path)?;
276 }
276 }
277 repo_config.layers.extend(cli_layers);
277 repo_config.layers.extend(cli_layers);
278 Ok(repo_config)
278 Ok(repo_config)
279 }
279 }
280
280
281 fn get_parse<'config, T: 'config>(
281 fn get_parse<'config, T: 'config>(
282 &'config self,
282 &'config self,
283 section: &[u8],
283 section: &[u8],
284 item: &[u8],
284 item: &[u8],
285 expected_type: &'static str,
285 expected_type: &'static str,
286 parse: impl Fn(&'config [u8]) -> Option<T>,
286 parse: impl Fn(&'config [u8]) -> Option<T>,
287 ) -> Result<Option<T>, ConfigValueParseError> {
287 ) -> Result<Option<T>, ConfigValueParseError> {
288 match self.get_inner(&section, &item) {
288 match self.get_inner(&section, &item) {
289 Some((layer, v)) => match parse(&v.bytes) {
289 Some((layer, v)) => match parse(&v.bytes) {
290 Some(b) => Ok(Some(b)),
290 Some(b) => Ok(Some(b)),
291 None => Err(ConfigValueParseError {
291 None => Err(ConfigValueParseError {
292 origin: layer.origin.to_owned(),
292 origin: layer.origin.to_owned(),
293 line: v.line,
293 line: v.line,
294 value: v.bytes.to_owned(),
294 value: v.bytes.to_owned(),
295 section: section.to_owned(),
295 section: section.to_owned(),
296 item: item.to_owned(),
296 item: item.to_owned(),
297 expected_type,
297 expected_type,
298 }),
298 }),
299 },
299 },
300 None => Ok(None),
300 None => Ok(None),
301 }
301 }
302 }
302 }
303
303
304 /// Returns an `Err` if the first value found is not a valid UTF-8 string.
304 /// Returns an `Err` if the first value found is not a valid UTF-8 string.
305 /// Otherwise, returns an `Ok(value)` if found, or `None`.
305 /// Otherwise, returns an `Ok(value)` if found, or `None`.
306 pub fn get_str(
306 pub fn get_str(
307 &self,
307 &self,
308 section: &[u8],
308 section: &[u8],
309 item: &[u8],
309 item: &[u8],
310 ) -> Result<Option<&str>, ConfigValueParseError> {
310 ) -> Result<Option<&str>, ConfigValueParseError> {
311 self.get_parse(section, item, "ASCII or UTF-8 string", |value| {
311 self.get_parse(section, item, "ASCII or UTF-8 string", |value| {
312 str::from_utf8(value).ok()
312 str::from_utf8(value).ok()
313 })
313 })
314 }
314 }
315
315
316 /// Returns an `Err` if the first value found is not a valid unsigned
316 /// Returns an `Err` if the first value found is not a valid unsigned
317 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`.
317 /// integer. Otherwise, returns an `Ok(value)` if found, or `None`.
318 pub fn get_u32(
318 pub fn get_u32(
319 &self,
319 &self,
320 section: &[u8],
320 section: &[u8],
321 item: &[u8],
321 item: &[u8],
322 ) -> Result<Option<u32>, ConfigValueParseError> {
322 ) -> Result<Option<u32>, ConfigValueParseError> {
323 self.get_parse(section, item, "valid integer", |value| {
323 self.get_parse(section, item, "valid integer", |value| {
324 str::from_utf8(value).ok()?.parse().ok()
324 str::from_utf8(value).ok()?.parse().ok()
325 })
325 })
326 }
326 }
327
327
328 /// Returns an `Err` if the first value found is not a valid file size
328 /// Returns an `Err` if the first value found is not a valid file size
329 /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`.
329 /// value such as `30` (default unit is bytes), `7 MB`, or `42.5 kb`.
330 /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`.
330 /// Otherwise, returns an `Ok(value_in_bytes)` if found, or `None`.
331 pub fn get_byte_size(
331 pub fn get_byte_size(
332 &self,
332 &self,
333 section: &[u8],
333 section: &[u8],
334 item: &[u8],
334 item: &[u8],
335 ) -> Result<Option<u64>, ConfigValueParseError> {
335 ) -> Result<Option<u64>, ConfigValueParseError> {
336 self.get_parse(section, item, "byte quantity", values::parse_byte_size)
336 self.get_parse(section, item, "byte quantity", values::parse_byte_size)
337 }
337 }
338
338
339 /// Returns an `Err` if the first value found is not a valid boolean.
339 /// Returns an `Err` if the first value found is not a valid boolean.
340 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
340 /// Otherwise, returns an `Ok(option)`, where `option` is the boolean if
341 /// found, or `None`.
341 /// found, or `None`.
342 pub fn get_option(
342 pub fn get_option(
343 &self,
343 &self,
344 section: &[u8],
344 section: &[u8],
345 item: &[u8],
345 item: &[u8],
346 ) -> Result<Option<bool>, ConfigValueParseError> {
346 ) -> Result<Option<bool>, ConfigValueParseError> {
347 self.get_parse(section, item, "boolean", values::parse_bool)
347 self.get_parse(section, item, "boolean", values::parse_bool)
348 }
348 }
349
349
350 /// Returns the corresponding boolean in the config. Returns `Ok(false)`
350 /// Returns the corresponding boolean in the config. Returns `Ok(false)`
351 /// if the value is not found, an `Err` if it's not a valid boolean.
351 /// if the value is not found, an `Err` if it's not a valid boolean.
352 pub fn get_bool(
352 pub fn get_bool(
353 &self,
353 &self,
354 section: &[u8],
354 section: &[u8],
355 item: &[u8],
355 item: &[u8],
356 ) -> Result<bool, ConfigValueParseError> {
356 ) -> Result<bool, ConfigValueParseError> {
357 Ok(self.get_option(section, item)?.unwrap_or(false))
357 Ok(self.get_option(section, item)?.unwrap_or(false))
358 }
358 }
359
359
360 /// Returns the corresponding list-value in the config if found, or `None`.
360 /// Returns the corresponding list-value in the config if found, or `None`.
361 ///
361 ///
362 /// This is appropriate for new configuration keys. The value syntax is
362 /// This is appropriate for new configuration keys. The value syntax is
363 /// **not** the same as most existing list-valued config, which has Python
363 /// **not** the same as most existing list-valued config, which has Python
364 /// parsing implemented in `parselist()` in `mercurial/config.py`.
364 /// parsing implemented in `parselist()` in
365 /// Faithfully porting that parsing algorithm to Rust (including behavior
365 /// `mercurial/utils/stringutil.py`. Faithfully porting that parsing
366 /// that are arguably bugs) turned out to be non-trivial and hasn’t been
366 /// algorithm to Rust (including behavior that are arguably bugs)
367 /// completed as of this writing.
367 /// turned out to be non-trivial and hasn’t been completed as of this
368 /// writing.
368 ///
369 ///
369 /// Instead, the "simple" syntax is: split on comma, then trim leading and
370 /// Instead, the "simple" syntax is: split on comma, then trim leading and
370 /// trailing whitespace of each component. Quotes or backslashes are not
371 /// trailing whitespace of each component. Quotes or backslashes are not
371 /// interpreted in any way. Commas are mandatory between values. Values
372 /// interpreted in any way. Commas are mandatory between values. Values
372 /// that contain a comma are not supported.
373 /// that contain a comma are not supported.
373 pub fn get_simple_list(
374 pub fn get_simple_list(
374 &self,
375 &self,
375 section: &[u8],
376 section: &[u8],
376 item: &[u8],
377 item: &[u8],
377 ) -> Option<impl Iterator<Item = &[u8]>> {
378 ) -> Option<impl Iterator<Item = &[u8]>> {
378 self.get(section, item).map(|value| {
379 self.get(section, item).map(|value| {
379 value
380 value
380 .split(|&byte| byte == b',')
381 .split(|&byte| byte == b',')
381 .map(|component| component.trim())
382 .map(|component| component.trim())
382 })
383 })
383 }
384 }
384
385
385 /// Returns the raw value bytes of the first one found, or `None`.
386 /// Returns the raw value bytes of the first one found, or `None`.
386 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
387 pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&[u8]> {
387 self.get_inner(section, item)
388 self.get_inner(section, item)
388 .map(|(_, value)| value.bytes.as_ref())
389 .map(|(_, value)| value.bytes.as_ref())
389 }
390 }
390
391
391 /// Returns the layer and the value of the first one found, or `None`.
392 /// Returns the layer and the value of the first one found, or `None`.
392 fn get_inner(
393 fn get_inner(
393 &self,
394 &self,
394 section: &[u8],
395 section: &[u8],
395 item: &[u8],
396 item: &[u8],
396 ) -> Option<(&ConfigLayer, &ConfigValue)> {
397 ) -> Option<(&ConfigLayer, &ConfigValue)> {
397 for layer in self.layers.iter().rev() {
398 for layer in self.layers.iter().rev() {
398 if !layer.trusted {
399 if !layer.trusted {
399 continue;
400 continue;
400 }
401 }
401 if let Some(v) = layer.get(&section, &item) {
402 if let Some(v) = layer.get(&section, &item) {
402 return Some((&layer, v));
403 return Some((&layer, v));
403 }
404 }
404 }
405 }
405 None
406 None
406 }
407 }
407
408
408 /// Return all keys defined for the given section
409 /// Return all keys defined for the given section
409 pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> {
410 pub fn get_section_keys(&self, section: &[u8]) -> HashSet<&[u8]> {
410 self.layers
411 self.layers
411 .iter()
412 .iter()
412 .flat_map(|layer| layer.iter_keys(section))
413 .flat_map(|layer| layer.iter_keys(section))
413 .collect()
414 .collect()
414 }
415 }
415
416
416 /// Get raw values bytes from all layers (even untrusted ones) in order
417 /// Get raw values bytes from all layers (even untrusted ones) in order
417 /// of precedence.
418 /// of precedence.
418 #[cfg(test)]
419 #[cfg(test)]
419 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
420 fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> {
420 let mut res = vec![];
421 let mut res = vec![];
421 for layer in self.layers.iter().rev() {
422 for layer in self.layers.iter().rev() {
422 if let Some(v) = layer.get(&section, &item) {
423 if let Some(v) = layer.get(&section, &item) {
423 res.push(v.bytes.as_ref());
424 res.push(v.bytes.as_ref());
424 }
425 }
425 }
426 }
426 res
427 res
427 }
428 }
428 }
429 }
429
430
430 #[cfg(test)]
431 #[cfg(test)]
431 mod tests {
432 mod tests {
432 use super::*;
433 use super::*;
433 use pretty_assertions::assert_eq;
434 use pretty_assertions::assert_eq;
434 use std::fs::File;
435 use std::fs::File;
435 use std::io::Write;
436 use std::io::Write;
436
437
437 #[test]
438 #[test]
438 fn test_include_layer_ordering() {
439 fn test_include_layer_ordering() {
439 let tmpdir = tempfile::tempdir().unwrap();
440 let tmpdir = tempfile::tempdir().unwrap();
440 let tmpdir_path = tmpdir.path();
441 let tmpdir_path = tmpdir.path();
441 let mut included_file =
442 let mut included_file =
442 File::create(&tmpdir_path.join("included.rc")).unwrap();
443 File::create(&tmpdir_path.join("included.rc")).unwrap();
443
444
444 included_file.write_all(b"[section]\nitem=value1").unwrap();
445 included_file.write_all(b"[section]\nitem=value1").unwrap();
445 let base_config_path = tmpdir_path.join("base.rc");
446 let base_config_path = tmpdir_path.join("base.rc");
446 let mut config_file = File::create(&base_config_path).unwrap();
447 let mut config_file = File::create(&base_config_path).unwrap();
447 let data =
448 let data =
448 b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\
449 b"[section]\nitem=value0\n%include included.rc\nitem=value2\n\
449 [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub";
450 [section2]\ncount = 4\nsize = 1.5 KB\nnot-count = 1.5\nnot-size = 1 ub";
450 config_file.write_all(data).unwrap();
451 config_file.write_all(data).unwrap();
451
452
452 let sources = vec![ConfigSource::AbsPath(base_config_path)];
453 let sources = vec![ConfigSource::AbsPath(base_config_path)];
453 let config = Config::load_from_explicit_sources(sources)
454 let config = Config::load_from_explicit_sources(sources)
454 .expect("expected valid config");
455 .expect("expected valid config");
455
456
456 let (_, value) = config.get_inner(b"section", b"item").unwrap();
457 let (_, value) = config.get_inner(b"section", b"item").unwrap();
457 assert_eq!(
458 assert_eq!(
458 value,
459 value,
459 &ConfigValue {
460 &ConfigValue {
460 bytes: b"value2".to_vec(),
461 bytes: b"value2".to_vec(),
461 line: Some(4)
462 line: Some(4)
462 }
463 }
463 );
464 );
464
465
465 let value = config.get(b"section", b"item").unwrap();
466 let value = config.get(b"section", b"item").unwrap();
466 assert_eq!(value, b"value2",);
467 assert_eq!(value, b"value2",);
467 assert_eq!(
468 assert_eq!(
468 config.get_all(b"section", b"item"),
469 config.get_all(b"section", b"item"),
469 [b"value2", b"value1", b"value0"]
470 [b"value2", b"value1", b"value0"]
470 );
471 );
471
472
472 assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4));
473 assert_eq!(config.get_u32(b"section2", b"count").unwrap(), Some(4));
473 assert_eq!(
474 assert_eq!(
474 config.get_byte_size(b"section2", b"size").unwrap(),
475 config.get_byte_size(b"section2", b"size").unwrap(),
475 Some(1024 + 512)
476 Some(1024 + 512)
476 );
477 );
477 assert!(config.get_u32(b"section2", b"not-count").is_err());
478 assert!(config.get_u32(b"section2", b"not-count").is_err());
478 assert!(config.get_byte_size(b"section2", b"not-size").is_err());
479 assert!(config.get_byte_size(b"section2", b"not-size").is_err());
479 }
480 }
480 }
481 }
@@ -1,178 +1,177 b''
1 # this is hack to make sure no escape characters are inserted into the output
1 # this is hack to make sure no escape characters are inserted into the output
2
2
3 from __future__ import absolute_import
3 from __future__ import absolute_import
4 from __future__ import print_function
4 from __future__ import print_function
5
5
6 import doctest
6 import doctest
7 import os
7 import os
8 import re
8 import re
9 import subprocess
9 import subprocess
10 import sys
10 import sys
11
11
12 ispy3 = sys.version_info[0] >= 3
12 ispy3 = sys.version_info[0] >= 3
13
13
14 if 'TERM' in os.environ:
14 if 'TERM' in os.environ:
15 del os.environ['TERM']
15 del os.environ['TERM']
16
16
17
17
18 class py3docchecker(doctest.OutputChecker):
18 class py3docchecker(doctest.OutputChecker):
19 def check_output(self, want, got, optionflags):
19 def check_output(self, want, got, optionflags):
20 want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want) # py2: u''
20 want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want) # py2: u''
21 got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got) # py3: b''
21 got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got) # py3: b''
22 # py3: <exc.name>: b'<msg>' -> <name>: <msg>
22 # py3: <exc.name>: b'<msg>' -> <name>: <msg>
23 # <exc.name>: <others> -> <name>: <others>
23 # <exc.name>: <others> -> <name>: <others>
24 got2 = re.sub(
24 got2 = re.sub(
25 r'''^mercurial\.\w+\.(\w+): (['"])(.*?)\2''',
25 r'''^mercurial\.\w+\.(\w+): (['"])(.*?)\2''',
26 r'\1: \3',
26 r'\1: \3',
27 got2,
27 got2,
28 re.MULTILINE,
28 re.MULTILINE,
29 )
29 )
30 got2 = re.sub(r'^mercurial\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE)
30 got2 = re.sub(r'^mercurial\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE)
31 return any(
31 return any(
32 doctest.OutputChecker.check_output(self, w, g, optionflags)
32 doctest.OutputChecker.check_output(self, w, g, optionflags)
33 for w, g in [(want, got), (want2, got2)]
33 for w, g in [(want, got), (want2, got2)]
34 )
34 )
35
35
36
36
37 def testmod(name, optionflags=0, testtarget=None):
37 def testmod(name, optionflags=0, testtarget=None):
38 __import__(name)
38 __import__(name)
39 mod = sys.modules[name]
39 mod = sys.modules[name]
40 if testtarget is not None:
40 if testtarget is not None:
41 mod = getattr(mod, testtarget)
41 mod = getattr(mod, testtarget)
42
42
43 # minimal copy of doctest.testmod()
43 # minimal copy of doctest.testmod()
44 finder = doctest.DocTestFinder()
44 finder = doctest.DocTestFinder()
45 checker = None
45 checker = None
46 if ispy3:
46 if ispy3:
47 checker = py3docchecker()
47 checker = py3docchecker()
48 runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags)
48 runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags)
49 for test in finder.find(mod, name):
49 for test in finder.find(mod, name):
50 runner.run(test)
50 runner.run(test)
51 runner.summarize()
51 runner.summarize()
52
52
53
53
54 DONT_RUN = []
54 DONT_RUN = []
55
55
56 # Exceptions to the defaults for a given detected module. The value for each
56 # Exceptions to the defaults for a given detected module. The value for each
57 # module name is a list of dicts that specify the kwargs to pass to testmod.
57 # module name is a list of dicts that specify the kwargs to pass to testmod.
58 # testmod is called once per item in the list, so an empty list will cause the
58 # testmod is called once per item in the list, so an empty list will cause the
59 # module to not be tested.
59 # module to not be tested.
60 testmod_arg_overrides = {
60 testmod_arg_overrides = {
61 'i18n.check-translation': DONT_RUN, # may require extra installation
61 'i18n.check-translation': DONT_RUN, # may require extra installation
62 'mercurial.dagparser': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
62 'mercurial.dagparser': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
63 'mercurial.keepalive': DONT_RUN, # >>> is an example, not a doctest
63 'mercurial.keepalive': DONT_RUN, # >>> is an example, not a doctest
64 'mercurial.posix': DONT_RUN, # run by mercurial.platform
64 'mercurial.posix': DONT_RUN, # run by mercurial.platform
65 'mercurial.statprof': DONT_RUN, # >>> is an example, not a doctest
65 'mercurial.statprof': DONT_RUN, # >>> is an example, not a doctest
66 'mercurial.util': [{}, {'testtarget': 'platform'}], # run twice!
66 'mercurial.util': [{}, {'testtarget': 'platform'}], # run twice!
67 'mercurial.windows': DONT_RUN, # run by mercurial.platform
67 'mercurial.windows': DONT_RUN, # run by mercurial.platform
68 'tests.test-url': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
68 'tests.test-url': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
69 }
69 }
70
70
71 fileset = 'set:(**.py)'
71 fileset = 'set:(**.py)'
72
72
73 cwd = os.path.dirname(os.environ["TESTDIR"])
73 cwd = os.path.dirname(os.environ["TESTDIR"])
74
74
75 if not os.path.isdir(os.path.join(cwd, ".hg")):
75 if not os.path.isdir(os.path.join(cwd, ".hg")):
76 sys.exit(0)
76 sys.exit(0)
77
77
78 files = subprocess.check_output(
78 files = subprocess.check_output(
79 "hg files --print0 \"%s\"" % fileset,
79 "hg files --print0 \"%s\"" % fileset,
80 shell=True,
80 shell=True,
81 cwd=cwd,
81 cwd=cwd,
82 ).split(b'\0')
82 ).split(b'\0')
83
83
84 if sys.version_info[0] >= 3:
84 if sys.version_info[0] >= 3:
85 cwd = os.fsencode(cwd)
85 cwd = os.fsencode(cwd)
86
86
87 mods_tested = set()
87 mods_tested = set()
88 for f in files:
88 for f in files:
89 if not f:
89 if not f:
90 continue
90 continue
91
91
92 with open(os.path.join(cwd, f), "rb") as fh:
92 with open(os.path.join(cwd, f), "rb") as fh:
93 if not re.search(br'\n\s*>>>', fh.read()):
93 if not re.search(br'\n\s*>>>', fh.read()):
94 continue
94 continue
95
95
96 if ispy3:
96 if ispy3:
97 f = f.decode()
97 f = f.decode()
98
98
99 modname = f.replace('.py', '').replace('\\', '.').replace('/', '.')
99 modname = f.replace('.py', '').replace('\\', '.').replace('/', '.')
100
100
101 # Third-party modules aren't our responsibility to test, and the modules in
101 # Third-party modules aren't our responsibility to test, and the modules in
102 # contrib generally do not have doctests in a good state, plus they're hard
102 # contrib generally do not have doctests in a good state, plus they're hard
103 # to import if this test is running with py2, so we just skip both for now.
103 # to import if this test is running with py2, so we just skip both for now.
104 if modname.startswith('mercurial.thirdparty.') or modname.startswith(
104 if modname.startswith('mercurial.thirdparty.') or modname.startswith(
105 'contrib.'
105 'contrib.'
106 ):
106 ):
107 continue
107 continue
108
108
109 for kwargs in testmod_arg_overrides.get(modname, [{}]):
109 for kwargs in testmod_arg_overrides.get(modname, [{}]):
110 mods_tested.add((modname, '%r' % (kwargs,)))
110 mods_tested.add((modname, '%r' % (kwargs,)))
111 if modname.startswith('tests.'):
111 if modname.startswith('tests.'):
112 # On py2, we can't import from tests.foo, but it works on both py2
112 # On py2, we can't import from tests.foo, but it works on both py2
113 # and py3 with the way that PYTHONPATH is setup to import without
113 # and py3 with the way that PYTHONPATH is setup to import without
114 # the 'tests.' prefix, so we do that.
114 # the 'tests.' prefix, so we do that.
115 modname = modname[len('tests.') :]
115 modname = modname[len('tests.') :]
116
116
117 testmod(modname, **kwargs)
117 testmod(modname, **kwargs)
118
118
119 # Meta-test: let's make sure that we actually ran what we expected to, above.
119 # Meta-test: let's make sure that we actually ran what we expected to, above.
120 # Each item in the set is a 2-tuple of module name and stringified kwargs passed
120 # Each item in the set is a 2-tuple of module name and stringified kwargs passed
121 # to testmod.
121 # to testmod.
122 expected_mods_tested = set(
122 expected_mods_tested = set(
123 [
123 [
124 ('hgext.convert.convcmd', '{}'),
124 ('hgext.convert.convcmd', '{}'),
125 ('hgext.convert.cvsps', '{}'),
125 ('hgext.convert.cvsps', '{}'),
126 ('hgext.convert.filemap', '{}'),
126 ('hgext.convert.filemap', '{}'),
127 ('hgext.convert.p4', '{}'),
127 ('hgext.convert.p4', '{}'),
128 ('hgext.convert.subversion', '{}'),
128 ('hgext.convert.subversion', '{}'),
129 ('hgext.fix', '{}'),
129 ('hgext.fix', '{}'),
130 ('hgext.mq', '{}'),
130 ('hgext.mq', '{}'),
131 ('mercurial.changelog', '{}'),
131 ('mercurial.changelog', '{}'),
132 ('mercurial.cmdutil', '{}'),
132 ('mercurial.cmdutil', '{}'),
133 ('mercurial.color', '{}'),
133 ('mercurial.color', '{}'),
134 ('mercurial.config', '{}'),
135 ('mercurial.dagparser', "{'optionflags': 4}"),
134 ('mercurial.dagparser', "{'optionflags': 4}"),
136 ('mercurial.encoding', '{}'),
135 ('mercurial.encoding', '{}'),
137 ('mercurial.fancyopts', '{}'),
136 ('mercurial.fancyopts', '{}'),
138 ('mercurial.formatter', '{}'),
137 ('mercurial.formatter', '{}'),
139 ('mercurial.hg', '{}'),
138 ('mercurial.hg', '{}'),
140 ('mercurial.hgweb.hgwebdir_mod', '{}'),
139 ('mercurial.hgweb.hgwebdir_mod', '{}'),
141 ('mercurial.match', '{}'),
140 ('mercurial.match', '{}'),
142 ('mercurial.mdiff', '{}'),
141 ('mercurial.mdiff', '{}'),
143 ('mercurial.minirst', '{}'),
142 ('mercurial.minirst', '{}'),
144 ('mercurial.parser', '{}'),
143 ('mercurial.parser', '{}'),
145 ('mercurial.patch', '{}'),
144 ('mercurial.patch', '{}'),
146 ('mercurial.pathutil', '{}'),
145 ('mercurial.pathutil', '{}'),
147 ('mercurial.pycompat', '{}'),
146 ('mercurial.pycompat', '{}'),
148 ('mercurial.revlogutils.deltas', '{}'),
147 ('mercurial.revlogutils.deltas', '{}'),
149 ('mercurial.revset', '{}'),
148 ('mercurial.revset', '{}'),
150 ('mercurial.revsetlang', '{}'),
149 ('mercurial.revsetlang', '{}'),
151 ('mercurial.simplemerge', '{}'),
150 ('mercurial.simplemerge', '{}'),
152 ('mercurial.smartset', '{}'),
151 ('mercurial.smartset', '{}'),
153 ('mercurial.store', '{}'),
152 ('mercurial.store', '{}'),
154 ('mercurial.subrepo', '{}'),
153 ('mercurial.subrepo', '{}'),
155 ('mercurial.templater', '{}'),
154 ('mercurial.templater', '{}'),
156 ('mercurial.ui', '{}'),
155 ('mercurial.ui', '{}'),
157 ('mercurial.util', "{'testtarget': 'platform'}"),
156 ('mercurial.util', "{'testtarget': 'platform'}"),
158 ('mercurial.util', '{}'),
157 ('mercurial.util', '{}'),
159 ('mercurial.utils.dateutil', '{}'),
158 ('mercurial.utils.dateutil', '{}'),
160 ('mercurial.utils.stringutil', '{}'),
159 ('mercurial.utils.stringutil', '{}'),
161 ('mercurial.utils.urlutil', '{}'),
160 ('mercurial.utils.urlutil', '{}'),
162 ('tests.drawdag', '{}'),
161 ('tests.drawdag', '{}'),
163 ('tests.test-run-tests', '{}'),
162 ('tests.test-run-tests', '{}'),
164 ('tests.test-url', "{'optionflags': 4}"),
163 ('tests.test-url', "{'optionflags': 4}"),
165 ]
164 ]
166 )
165 )
167
166
168 unexpectedly_run = mods_tested.difference(expected_mods_tested)
167 unexpectedly_run = mods_tested.difference(expected_mods_tested)
169 not_run = expected_mods_tested.difference(mods_tested)
168 not_run = expected_mods_tested.difference(mods_tested)
170
169
171 if unexpectedly_run:
170 if unexpectedly_run:
172 print('Unexpectedly ran (probably need to add to list):')
171 print('Unexpectedly ran (probably need to add to list):')
173 for r in sorted(unexpectedly_run):
172 for r in sorted(unexpectedly_run):
174 print(' %r' % (r,))
173 print(' %r' % (r,))
175 if not_run:
174 if not_run:
176 print('Expected to run, but was not run (doctest removed?):')
175 print('Expected to run, but was not run (doctest removed?):')
177 for r in sorted(not_run):
176 for r in sorted(not_run):
178 print(' %r' % (r,))
177 print(' %r' % (r,))
General Comments 0
You need to be logged in to leave comments. Login now