Show More
@@ -1,1118 +1,1121 b'' | |||
|
1 | 1 | # ui.py - user interface bits for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import errno |
|
11 | 11 | import getpass |
|
12 | 12 | import inspect |
|
13 | 13 | import os |
|
14 | 14 | import socket |
|
15 | 15 | import sys |
|
16 | 16 | import tempfile |
|
17 | 17 | import traceback |
|
18 | 18 | |
|
19 | 19 | from .i18n import _ |
|
20 | 20 | from .node import hex |
|
21 | 21 | |
|
22 | 22 | from . import ( |
|
23 | 23 | config, |
|
24 | 24 | error, |
|
25 | 25 | formatter, |
|
26 | 26 | progress, |
|
27 | 27 | scmutil, |
|
28 | 28 | util, |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | samplehgrcs = { |
|
32 | 32 | 'user': |
|
33 | 33 | """# example user config (see "hg help config" for more info) |
|
34 | 34 | [ui] |
|
35 | 35 | # name and email, e.g. |
|
36 | 36 | # username = Jane Doe <jdoe@example.com> |
|
37 | 37 | username = |
|
38 | 38 | |
|
39 | 39 | [extensions] |
|
40 | 40 | # uncomment these lines to enable some popular extensions |
|
41 | 41 | # (see "hg help extensions" for more info) |
|
42 | 42 | # |
|
43 | 43 | # pager = |
|
44 | 44 | # progress = |
|
45 | 45 | # color =""", |
|
46 | 46 | |
|
47 | 47 | 'cloned': |
|
48 | 48 | """# example repository config (see "hg help config" for more info) |
|
49 | 49 | [paths] |
|
50 | 50 | default = %s |
|
51 | 51 | |
|
52 | 52 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
53 | 53 | # (see "hg help config.paths" for more info) |
|
54 | 54 | # |
|
55 | 55 | # default-push = ssh://jdoe@example.net/hg/jdoes-fork |
|
56 | 56 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
57 | 57 | # my-clone = /home/jdoe/jdoes-clone |
|
58 | 58 | |
|
59 | 59 | [ui] |
|
60 | 60 | # name and email (local to this repository, optional), e.g. |
|
61 | 61 | # username = Jane Doe <jdoe@example.com> |
|
62 | 62 | """, |
|
63 | 63 | |
|
64 | 64 | 'local': |
|
65 | 65 | """# example repository config (see "hg help config" for more info) |
|
66 | 66 | [paths] |
|
67 | 67 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
68 | 68 | # (see "hg help config.paths" for more info) |
|
69 | 69 | # |
|
70 | 70 | # default = http://example.com/hg/example-repo |
|
71 | 71 | # default-push = ssh://jdoe@example.net/hg/jdoes-fork |
|
72 | 72 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
73 | 73 | # my-clone = /home/jdoe/jdoes-clone |
|
74 | 74 | |
|
75 | 75 | [ui] |
|
76 | 76 | # name and email (local to this repository, optional), e.g. |
|
77 | 77 | # username = Jane Doe <jdoe@example.com> |
|
78 | 78 | """, |
|
79 | 79 | |
|
80 | 80 | 'global': |
|
81 | 81 | """# example system-wide hg config (see "hg help config" for more info) |
|
82 | 82 | |
|
83 | 83 | [extensions] |
|
84 | 84 | # uncomment these lines to enable some popular extensions |
|
85 | 85 | # (see "hg help extensions" for more info) |
|
86 | 86 | # |
|
87 | 87 | # blackbox = |
|
88 | 88 | # progress = |
|
89 | 89 | # color = |
|
90 | 90 | # pager =""", |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | class ui(object): |
|
94 | 94 | def __init__(self, src=None): |
|
95 | 95 | # _buffers: used for temporary capture of output |
|
96 | 96 | self._buffers = [] |
|
97 | 97 | # _bufferstates: |
|
98 | 98 | # should the temporary capture include stderr and subprocess output |
|
99 | 99 | self._bufferstates = [] |
|
100 | 100 | self.quiet = self.verbose = self.debugflag = self.tracebackflag = False |
|
101 | 101 | self._reportuntrusted = True |
|
102 | 102 | self._ocfg = config.config() # overlay |
|
103 | 103 | self._tcfg = config.config() # trusted |
|
104 | 104 | self._ucfg = config.config() # untrusted |
|
105 | 105 | self._trustusers = set() |
|
106 | 106 | self._trustgroups = set() |
|
107 | 107 | self.callhooks = True |
|
108 | 108 | |
|
109 | 109 | if src: |
|
110 | 110 | self.fout = src.fout |
|
111 | 111 | self.ferr = src.ferr |
|
112 | 112 | self.fin = src.fin |
|
113 | 113 | |
|
114 | 114 | self._tcfg = src._tcfg.copy() |
|
115 | 115 | self._ucfg = src._ucfg.copy() |
|
116 | 116 | self._ocfg = src._ocfg.copy() |
|
117 | 117 | self._trustusers = src._trustusers.copy() |
|
118 | 118 | self._trustgroups = src._trustgroups.copy() |
|
119 | 119 | self.environ = src.environ |
|
120 | 120 | self.callhooks = src.callhooks |
|
121 | 121 | self.fixconfig() |
|
122 | 122 | else: |
|
123 | 123 | self.fout = sys.stdout |
|
124 | 124 | self.ferr = sys.stderr |
|
125 | 125 | self.fin = sys.stdin |
|
126 | 126 | |
|
127 | 127 | # shared read-only environment |
|
128 | 128 | self.environ = os.environ |
|
129 | 129 | # we always trust global config files |
|
130 | 130 | for f in scmutil.rcpath(): |
|
131 | 131 | self.readconfig(f, trust=True) |
|
132 | 132 | |
|
133 | 133 | def copy(self): |
|
134 | 134 | return self.__class__(self) |
|
135 | 135 | |
|
136 | 136 | def formatter(self, topic, opts): |
|
137 | 137 | return formatter.formatter(self, topic, opts) |
|
138 | 138 | |
|
139 | 139 | def _trusted(self, fp, f): |
|
140 | 140 | st = util.fstat(fp) |
|
141 | 141 | if util.isowner(st): |
|
142 | 142 | return True |
|
143 | 143 | |
|
144 | 144 | tusers, tgroups = self._trustusers, self._trustgroups |
|
145 | 145 | if '*' in tusers or '*' in tgroups: |
|
146 | 146 | return True |
|
147 | 147 | |
|
148 | 148 | user = util.username(st.st_uid) |
|
149 | 149 | group = util.groupname(st.st_gid) |
|
150 | 150 | if user in tusers or group in tgroups or user == util.username(): |
|
151 | 151 | return True |
|
152 | 152 | |
|
153 | 153 | if self._reportuntrusted: |
|
154 | 154 | self.warn(_('not trusting file %s from untrusted ' |
|
155 | 155 | 'user %s, group %s\n') % (f, user, group)) |
|
156 | 156 | return False |
|
157 | 157 | |
|
158 | 158 | def readconfig(self, filename, root=None, trust=False, |
|
159 | 159 | sections=None, remap=None): |
|
160 | 160 | try: |
|
161 | 161 | fp = open(filename) |
|
162 | 162 | except IOError: |
|
163 | 163 | if not sections: # ignore unless we were looking for something |
|
164 | 164 | return |
|
165 | 165 | raise |
|
166 | 166 | |
|
167 | 167 | cfg = config.config() |
|
168 | 168 | trusted = sections or trust or self._trusted(fp, filename) |
|
169 | 169 | |
|
170 | 170 | try: |
|
171 | 171 | cfg.read(filename, fp, sections=sections, remap=remap) |
|
172 | 172 | fp.close() |
|
173 | 173 | except error.ConfigError as inst: |
|
174 | 174 | if trusted: |
|
175 | 175 | raise |
|
176 | 176 | self.warn(_("ignored: %s\n") % str(inst)) |
|
177 | 177 | |
|
178 | 178 | if self.plain(): |
|
179 | 179 | for k in ('debug', 'fallbackencoding', 'quiet', 'slash', |
|
180 | 180 | 'logtemplate', 'statuscopies', 'style', |
|
181 | 181 | 'traceback', 'verbose'): |
|
182 | 182 | if k in cfg['ui']: |
|
183 | 183 | del cfg['ui'][k] |
|
184 | 184 | for k, v in cfg.items('defaults'): |
|
185 | 185 | del cfg['defaults'][k] |
|
186 | 186 | # Don't remove aliases from the configuration if in the exceptionlist |
|
187 | 187 | if self.plain('alias'): |
|
188 | 188 | for k, v in cfg.items('alias'): |
|
189 | 189 | del cfg['alias'][k] |
|
190 | 190 | if self.plain('revsetalias'): |
|
191 | 191 | for k, v in cfg.items('revsetalias'): |
|
192 | 192 | del cfg['revsetalias'][k] |
|
193 | 193 | |
|
194 | 194 | if trusted: |
|
195 | 195 | self._tcfg.update(cfg) |
|
196 | 196 | self._tcfg.update(self._ocfg) |
|
197 | 197 | self._ucfg.update(cfg) |
|
198 | 198 | self._ucfg.update(self._ocfg) |
|
199 | 199 | |
|
200 | 200 | if root is None: |
|
201 | 201 | root = os.path.expanduser('~') |
|
202 | 202 | self.fixconfig(root=root) |
|
203 | 203 | |
|
204 | 204 | def fixconfig(self, root=None, section=None): |
|
205 | 205 | if section in (None, 'paths'): |
|
206 | 206 | # expand vars and ~ |
|
207 | 207 | # translate paths relative to root (or home) into absolute paths |
|
208 | 208 | root = root or os.getcwd() |
|
209 | 209 | for c in self._tcfg, self._ucfg, self._ocfg: |
|
210 | 210 | for n, p in c.items('paths'): |
|
211 | 211 | if not p: |
|
212 | 212 | continue |
|
213 | 213 | if '%%' in p: |
|
214 | 214 | self.warn(_("(deprecated '%%' in path %s=%s from %s)\n") |
|
215 | 215 | % (n, p, self.configsource('paths', n))) |
|
216 | 216 | p = p.replace('%%', '%') |
|
217 | 217 | p = util.expandpath(p) |
|
218 | 218 | if not util.hasscheme(p) and not os.path.isabs(p): |
|
219 | 219 | p = os.path.normpath(os.path.join(root, p)) |
|
220 | 220 | c.set("paths", n, p) |
|
221 | 221 | |
|
222 | 222 | if section in (None, 'ui'): |
|
223 | 223 | # update ui options |
|
224 | 224 | self.debugflag = self.configbool('ui', 'debug') |
|
225 | 225 | self.verbose = self.debugflag or self.configbool('ui', 'verbose') |
|
226 | 226 | self.quiet = not self.debugflag and self.configbool('ui', 'quiet') |
|
227 | 227 | if self.verbose and self.quiet: |
|
228 | 228 | self.quiet = self.verbose = False |
|
229 | 229 | self._reportuntrusted = self.debugflag or self.configbool("ui", |
|
230 | 230 | "report_untrusted", True) |
|
231 | 231 | self.tracebackflag = self.configbool('ui', 'traceback', False) |
|
232 | 232 | |
|
233 | 233 | if section in (None, 'trusted'): |
|
234 | 234 | # update trust information |
|
235 | 235 | self._trustusers.update(self.configlist('trusted', 'users')) |
|
236 | 236 | self._trustgroups.update(self.configlist('trusted', 'groups')) |
|
237 | 237 | |
|
238 | 238 | def backupconfig(self, section, item): |
|
239 | 239 | return (self._ocfg.backup(section, item), |
|
240 | 240 | self._tcfg.backup(section, item), |
|
241 | 241 | self._ucfg.backup(section, item),) |
|
242 | 242 | def restoreconfig(self, data): |
|
243 | 243 | self._ocfg.restore(data[0]) |
|
244 | 244 | self._tcfg.restore(data[1]) |
|
245 | 245 | self._ucfg.restore(data[2]) |
|
246 | 246 | |
|
247 | 247 | def setconfig(self, section, name, value, source=''): |
|
248 | 248 | for cfg in (self._ocfg, self._tcfg, self._ucfg): |
|
249 | 249 | cfg.set(section, name, value, source) |
|
250 | 250 | self.fixconfig(section=section) |
|
251 | 251 | |
|
252 | 252 | def _data(self, untrusted): |
|
253 | 253 | return untrusted and self._ucfg or self._tcfg |
|
254 | 254 | |
|
255 | 255 | def configsource(self, section, name, untrusted=False): |
|
256 | 256 | return self._data(untrusted).source(section, name) or 'none' |
|
257 | 257 | |
|
258 | 258 | def config(self, section, name, default=None, untrusted=False): |
|
259 | 259 | if isinstance(name, list): |
|
260 | 260 | alternates = name |
|
261 | 261 | else: |
|
262 | 262 | alternates = [name] |
|
263 | 263 | |
|
264 | 264 | for n in alternates: |
|
265 | 265 | value = self._data(untrusted).get(section, n, None) |
|
266 | 266 | if value is not None: |
|
267 | 267 | name = n |
|
268 | 268 | break |
|
269 | 269 | else: |
|
270 | 270 | value = default |
|
271 | 271 | |
|
272 | 272 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
273 | 273 | for n in alternates: |
|
274 | 274 | uvalue = self._ucfg.get(section, n) |
|
275 | 275 | if uvalue is not None and uvalue != value: |
|
276 | 276 | self.debug("ignoring untrusted configuration option " |
|
277 | 277 | "%s.%s = %s\n" % (section, n, uvalue)) |
|
278 | 278 | return value |
|
279 | 279 | |
|
280 | 280 | def configpath(self, section, name, default=None, untrusted=False): |
|
281 | 281 | 'get a path config item, expanded relative to repo root or config file' |
|
282 | 282 | v = self.config(section, name, default, untrusted) |
|
283 | 283 | if v is None: |
|
284 | 284 | return None |
|
285 | 285 | if not os.path.isabs(v) or "://" not in v: |
|
286 | 286 | src = self.configsource(section, name, untrusted) |
|
287 | 287 | if ':' in src: |
|
288 | 288 | base = os.path.dirname(src.rsplit(':')[0]) |
|
289 | 289 | v = os.path.join(base, os.path.expanduser(v)) |
|
290 | 290 | return v |
|
291 | 291 | |
|
292 | 292 | def configbool(self, section, name, default=False, untrusted=False): |
|
293 | 293 | """parse a configuration element as a boolean |
|
294 | 294 | |
|
295 | 295 | >>> u = ui(); s = 'foo' |
|
296 | 296 | >>> u.setconfig(s, 'true', 'yes') |
|
297 | 297 | >>> u.configbool(s, 'true') |
|
298 | 298 | True |
|
299 | 299 | >>> u.setconfig(s, 'false', 'no') |
|
300 | 300 | >>> u.configbool(s, 'false') |
|
301 | 301 | False |
|
302 | 302 | >>> u.configbool(s, 'unknown') |
|
303 | 303 | False |
|
304 | 304 | >>> u.configbool(s, 'unknown', True) |
|
305 | 305 | True |
|
306 | 306 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
307 | 307 | >>> u.configbool(s, 'invalid') |
|
308 | 308 | Traceback (most recent call last): |
|
309 | 309 | ... |
|
310 | 310 | ConfigError: foo.invalid is not a boolean ('somevalue') |
|
311 | 311 | """ |
|
312 | 312 | |
|
313 | 313 | v = self.config(section, name, None, untrusted) |
|
314 | 314 | if v is None: |
|
315 | 315 | return default |
|
316 | 316 | if isinstance(v, bool): |
|
317 | 317 | return v |
|
318 | 318 | b = util.parsebool(v) |
|
319 | 319 | if b is None: |
|
320 | 320 | raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
|
321 | 321 | % (section, name, v)) |
|
322 | 322 | return b |
|
323 | 323 | |
|
324 | 324 | def configint(self, section, name, default=None, untrusted=False): |
|
325 | 325 | """parse a configuration element as an integer |
|
326 | 326 | |
|
327 | 327 | >>> u = ui(); s = 'foo' |
|
328 | 328 | >>> u.setconfig(s, 'int1', '42') |
|
329 | 329 | >>> u.configint(s, 'int1') |
|
330 | 330 | 42 |
|
331 | 331 | >>> u.setconfig(s, 'int2', '-42') |
|
332 | 332 | >>> u.configint(s, 'int2') |
|
333 | 333 | -42 |
|
334 | 334 | >>> u.configint(s, 'unknown', 7) |
|
335 | 335 | 7 |
|
336 | 336 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
337 | 337 | >>> u.configint(s, 'invalid') |
|
338 | 338 | Traceback (most recent call last): |
|
339 | 339 | ... |
|
340 | 340 | ConfigError: foo.invalid is not an integer ('somevalue') |
|
341 | 341 | """ |
|
342 | 342 | |
|
343 | 343 | v = self.config(section, name, None, untrusted) |
|
344 | 344 | if v is None: |
|
345 | 345 | return default |
|
346 | 346 | try: |
|
347 | 347 | return int(v) |
|
348 | 348 | except ValueError: |
|
349 | 349 | raise error.ConfigError(_("%s.%s is not an integer ('%s')") |
|
350 | 350 | % (section, name, v)) |
|
351 | 351 | |
|
352 | 352 | def configbytes(self, section, name, default=0, untrusted=False): |
|
353 | 353 | """parse a configuration element as a quantity in bytes |
|
354 | 354 | |
|
355 | 355 | Units can be specified as b (bytes), k or kb (kilobytes), m or |
|
356 | 356 | mb (megabytes), g or gb (gigabytes). |
|
357 | 357 | |
|
358 | 358 | >>> u = ui(); s = 'foo' |
|
359 | 359 | >>> u.setconfig(s, 'val1', '42') |
|
360 | 360 | >>> u.configbytes(s, 'val1') |
|
361 | 361 | 42 |
|
362 | 362 | >>> u.setconfig(s, 'val2', '42.5 kb') |
|
363 | 363 | >>> u.configbytes(s, 'val2') |
|
364 | 364 | 43520 |
|
365 | 365 | >>> u.configbytes(s, 'unknown', '7 MB') |
|
366 | 366 | 7340032 |
|
367 | 367 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
368 | 368 | >>> u.configbytes(s, 'invalid') |
|
369 | 369 | Traceback (most recent call last): |
|
370 | 370 | ... |
|
371 | 371 | ConfigError: foo.invalid is not a byte quantity ('somevalue') |
|
372 | 372 | """ |
|
373 | 373 | |
|
374 | 374 | value = self.config(section, name) |
|
375 | 375 | if value is None: |
|
376 | 376 | if not isinstance(default, str): |
|
377 | 377 | return default |
|
378 | 378 | value = default |
|
379 | 379 | try: |
|
380 | 380 | return util.sizetoint(value) |
|
381 | 381 | except error.ParseError: |
|
382 | 382 | raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')") |
|
383 | 383 | % (section, name, value)) |
|
384 | 384 | |
|
385 | 385 | def configlist(self, section, name, default=None, untrusted=False): |
|
386 | 386 | """parse a configuration element as a list of comma/space separated |
|
387 | 387 | strings |
|
388 | 388 | |
|
389 | 389 | >>> u = ui(); s = 'foo' |
|
390 | 390 | >>> u.setconfig(s, 'list1', 'this,is "a small" ,test') |
|
391 | 391 | >>> u.configlist(s, 'list1') |
|
392 | 392 | ['this', 'is', 'a small', 'test'] |
|
393 | 393 | """ |
|
394 | 394 | |
|
395 | 395 | def _parse_plain(parts, s, offset): |
|
396 | 396 | whitespace = False |
|
397 | 397 | while offset < len(s) and (s[offset].isspace() or s[offset] == ','): |
|
398 | 398 | whitespace = True |
|
399 | 399 | offset += 1 |
|
400 | 400 | if offset >= len(s): |
|
401 | 401 | return None, parts, offset |
|
402 | 402 | if whitespace: |
|
403 | 403 | parts.append('') |
|
404 | 404 | if s[offset] == '"' and not parts[-1]: |
|
405 | 405 | return _parse_quote, parts, offset + 1 |
|
406 | 406 | elif s[offset] == '"' and parts[-1][-1] == '\\': |
|
407 | 407 | parts[-1] = parts[-1][:-1] + s[offset] |
|
408 | 408 | return _parse_plain, parts, offset + 1 |
|
409 | 409 | parts[-1] += s[offset] |
|
410 | 410 | return _parse_plain, parts, offset + 1 |
|
411 | 411 | |
|
412 | 412 | def _parse_quote(parts, s, offset): |
|
413 | 413 | if offset < len(s) and s[offset] == '"': # "" |
|
414 | 414 | parts.append('') |
|
415 | 415 | offset += 1 |
|
416 | 416 | while offset < len(s) and (s[offset].isspace() or |
|
417 | 417 | s[offset] == ','): |
|
418 | 418 | offset += 1 |
|
419 | 419 | return _parse_plain, parts, offset |
|
420 | 420 | |
|
421 | 421 | while offset < len(s) and s[offset] != '"': |
|
422 | 422 | if (s[offset] == '\\' and offset + 1 < len(s) |
|
423 | 423 | and s[offset + 1] == '"'): |
|
424 | 424 | offset += 1 |
|
425 | 425 | parts[-1] += '"' |
|
426 | 426 | else: |
|
427 | 427 | parts[-1] += s[offset] |
|
428 | 428 | offset += 1 |
|
429 | 429 | |
|
430 | 430 | if offset >= len(s): |
|
431 | 431 | real_parts = _configlist(parts[-1]) |
|
432 | 432 | if not real_parts: |
|
433 | 433 | parts[-1] = '"' |
|
434 | 434 | else: |
|
435 | 435 | real_parts[0] = '"' + real_parts[0] |
|
436 | 436 | parts = parts[:-1] |
|
437 | 437 | parts.extend(real_parts) |
|
438 | 438 | return None, parts, offset |
|
439 | 439 | |
|
440 | 440 | offset += 1 |
|
441 | 441 | while offset < len(s) and s[offset] in [' ', ',']: |
|
442 | 442 | offset += 1 |
|
443 | 443 | |
|
444 | 444 | if offset < len(s): |
|
445 | 445 | if offset + 1 == len(s) and s[offset] == '"': |
|
446 | 446 | parts[-1] += '"' |
|
447 | 447 | offset += 1 |
|
448 | 448 | else: |
|
449 | 449 | parts.append('') |
|
450 | 450 | else: |
|
451 | 451 | return None, parts, offset |
|
452 | 452 | |
|
453 | 453 | return _parse_plain, parts, offset |
|
454 | 454 | |
|
455 | 455 | def _configlist(s): |
|
456 | 456 | s = s.rstrip(' ,') |
|
457 | 457 | if not s: |
|
458 | 458 | return [] |
|
459 | 459 | parser, parts, offset = _parse_plain, [''], 0 |
|
460 | 460 | while parser: |
|
461 | 461 | parser, parts, offset = parser(parts, s, offset) |
|
462 | 462 | return parts |
|
463 | 463 | |
|
464 | 464 | result = self.config(section, name, untrusted=untrusted) |
|
465 | 465 | if result is None: |
|
466 | 466 | result = default or [] |
|
467 | 467 | if isinstance(result, basestring): |
|
468 | 468 | result = _configlist(result.lstrip(' ,\n')) |
|
469 | 469 | if result is None: |
|
470 | 470 | result = default or [] |
|
471 | 471 | return result |
|
472 | 472 | |
|
473 | 473 | def has_section(self, section, untrusted=False): |
|
474 | 474 | '''tell whether section exists in config.''' |
|
475 | 475 | return section in self._data(untrusted) |
|
476 | 476 | |
|
477 | 477 | def configitems(self, section, untrusted=False): |
|
478 | 478 | items = self._data(untrusted).items(section) |
|
479 | 479 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
480 | 480 | for k, v in self._ucfg.items(section): |
|
481 | 481 | if self._tcfg.get(section, k) != v: |
|
482 | 482 | self.debug("ignoring untrusted configuration option " |
|
483 | 483 | "%s.%s = %s\n" % (section, k, v)) |
|
484 | 484 | return items |
|
485 | 485 | |
|
486 | 486 | def walkconfig(self, untrusted=False): |
|
487 | 487 | cfg = self._data(untrusted) |
|
488 | 488 | for section in cfg.sections(): |
|
489 | 489 | for name, value in self.configitems(section, untrusted): |
|
490 | 490 | yield section, name, value |
|
491 | 491 | |
|
492 | 492 | def plain(self, feature=None): |
|
493 | 493 | '''is plain mode active? |
|
494 | 494 | |
|
495 | 495 | Plain mode means that all configuration variables which affect |
|
496 | 496 | the behavior and output of Mercurial should be |
|
497 | 497 | ignored. Additionally, the output should be stable, |
|
498 | 498 | reproducible and suitable for use in scripts or applications. |
|
499 | 499 | |
|
500 | 500 | The only way to trigger plain mode is by setting either the |
|
501 | 501 | `HGPLAIN' or `HGPLAINEXCEPT' environment variables. |
|
502 | 502 | |
|
503 | 503 | The return value can either be |
|
504 | 504 | - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT |
|
505 | 505 | - True otherwise |
|
506 | 506 | ''' |
|
507 | 507 | if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ: |
|
508 | 508 | return False |
|
509 | 509 | exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',') |
|
510 | 510 | if feature and exceptions: |
|
511 | 511 | return feature not in exceptions |
|
512 | 512 | return True |
|
513 | 513 | |
|
514 | 514 | def username(self): |
|
515 | 515 | """Return default username to be used in commits. |
|
516 | 516 | |
|
517 | 517 | Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
|
518 | 518 | and stop searching if one of these is set. |
|
519 | 519 | If not found and ui.askusername is True, ask the user, else use |
|
520 | 520 | ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". |
|
521 | 521 | """ |
|
522 | 522 | user = os.environ.get("HGUSER") |
|
523 | 523 | if user is None: |
|
524 | 524 | user = self.config("ui", ["username", "user"]) |
|
525 | 525 | if user is not None: |
|
526 | 526 | user = os.path.expandvars(user) |
|
527 | 527 | if user is None: |
|
528 | 528 | user = os.environ.get("EMAIL") |
|
529 | 529 | if user is None and self.configbool("ui", "askusername"): |
|
530 | 530 | user = self.prompt(_("enter a commit username:"), default=None) |
|
531 | 531 | if user is None and not self.interactive(): |
|
532 | 532 | try: |
|
533 | 533 | user = '%s@%s' % (util.getuser(), socket.getfqdn()) |
|
534 | 534 | self.warn(_("no username found, using '%s' instead\n") % user) |
|
535 | 535 | except KeyError: |
|
536 | 536 | pass |
|
537 | 537 | if not user: |
|
538 | 538 | raise util.Abort(_('no username supplied'), |
|
539 | 539 | hint=_('use "hg config --edit" ' |
|
540 | 540 | 'to set your username')) |
|
541 | 541 | if "\n" in user: |
|
542 | 542 | raise util.Abort(_("username %s contains a newline\n") % repr(user)) |
|
543 | 543 | return user |
|
544 | 544 | |
|
545 | 545 | def shortuser(self, user): |
|
546 | 546 | """Return a short representation of a user name or email address.""" |
|
547 | 547 | if not self.verbose: |
|
548 | 548 | user = util.shortuser(user) |
|
549 | 549 | return user |
|
550 | 550 | |
|
551 | 551 | def expandpath(self, loc, default=None): |
|
552 | 552 | """Return repository location relative to cwd or from [paths]""" |
|
553 | 553 | try: |
|
554 | 554 | p = self.paths.getpath(loc) |
|
555 | 555 | if p: |
|
556 | 556 | return p.rawloc |
|
557 | 557 | except error.RepoError: |
|
558 | 558 | pass |
|
559 | 559 | |
|
560 | 560 | if default: |
|
561 | 561 | try: |
|
562 | 562 | p = self.paths.getpath(default) |
|
563 | 563 | if p: |
|
564 | 564 | return p.rawloc |
|
565 | 565 | except error.RepoError: |
|
566 | 566 | pass |
|
567 | 567 | |
|
568 | 568 | return loc |
|
569 | 569 | |
|
570 | 570 | @util.propertycache |
|
571 | 571 | def paths(self): |
|
572 | 572 | return paths(self) |
|
573 | 573 | |
|
574 | 574 | def pushbuffer(self, error=False, subproc=False): |
|
575 | 575 | """install a buffer to capture standard output of the ui object |
|
576 | 576 | |
|
577 | 577 | If error is True, the error output will be captured too. |
|
578 | 578 | |
|
579 | 579 | If subproc is True, output from subprocesses (typically hooks) will be |
|
580 | 580 | captured too.""" |
|
581 | 581 | self._buffers.append([]) |
|
582 | 582 | self._bufferstates.append((error, subproc)) |
|
583 | 583 | |
|
584 | 584 | def popbuffer(self, labeled=False): |
|
585 | 585 | '''pop the last buffer and return the buffered output |
|
586 | 586 | |
|
587 | 587 | If labeled is True, any labels associated with buffered |
|
588 | 588 | output will be handled. By default, this has no effect |
|
589 | 589 | on the output returned, but extensions and GUI tools may |
|
590 | 590 | handle this argument and returned styled output. If output |
|
591 | 591 | is being buffered so it can be captured and parsed or |
|
592 | 592 | processed, labeled should not be set to True. |
|
593 | 593 | ''' |
|
594 | 594 | self._bufferstates.pop() |
|
595 | 595 | return "".join(self._buffers.pop()) |
|
596 | 596 | |
|
597 | 597 | def write(self, *args, **opts): |
|
598 | 598 | '''write args to output |
|
599 | 599 | |
|
600 | 600 | By default, this method simply writes to the buffer or stdout, |
|
601 | 601 | but extensions or GUI tools may override this method, |
|
602 | 602 | write_err(), popbuffer(), and label() to style output from |
|
603 | 603 | various parts of hg. |
|
604 | 604 | |
|
605 | 605 | An optional keyword argument, "label", can be passed in. |
|
606 | 606 | This should be a string containing label names separated by |
|
607 | 607 | space. Label names take the form of "topic.type". For example, |
|
608 | 608 | ui.debug() issues a label of "ui.debug". |
|
609 | 609 | |
|
610 | 610 | When labeling output for a specific command, a label of |
|
611 | 611 | "cmdname.type" is recommended. For example, status issues |
|
612 | 612 | a label of "status.modified" for modified files. |
|
613 | 613 | ''' |
|
614 | 614 | self._progclear() |
|
615 | 615 | if self._buffers: |
|
616 | 616 | self._buffers[-1].extend([str(a) for a in args]) |
|
617 | 617 | else: |
|
618 | 618 | for a in args: |
|
619 | 619 | self.fout.write(str(a)) |
|
620 | 620 | |
|
621 | 621 | def write_err(self, *args, **opts): |
|
622 | 622 | self._progclear() |
|
623 | 623 | try: |
|
624 | 624 | if self._bufferstates and self._bufferstates[-1][0]: |
|
625 | 625 | return self.write(*args, **opts) |
|
626 | 626 | if not getattr(self.fout, 'closed', False): |
|
627 | 627 | self.fout.flush() |
|
628 | 628 | for a in args: |
|
629 | 629 | self.ferr.write(str(a)) |
|
630 | 630 | # stderr may be buffered under win32 when redirected to files, |
|
631 | 631 | # including stdout. |
|
632 | 632 | if not getattr(self.ferr, 'closed', False): |
|
633 | 633 | self.ferr.flush() |
|
634 | 634 | except IOError as inst: |
|
635 | 635 | if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF): |
|
636 | 636 | raise |
|
637 | 637 | |
|
638 | 638 | def flush(self): |
|
639 | 639 | try: self.fout.flush() |
|
640 | 640 | except (IOError, ValueError): pass |
|
641 | 641 | try: self.ferr.flush() |
|
642 | 642 | except (IOError, ValueError): pass |
|
643 | 643 | |
|
644 | 644 | def _isatty(self, fh): |
|
645 | 645 | if self.configbool('ui', 'nontty', False): |
|
646 | 646 | return False |
|
647 | 647 | return util.isatty(fh) |
|
648 | 648 | |
|
649 | 649 | def interactive(self): |
|
650 | 650 | '''is interactive input allowed? |
|
651 | 651 | |
|
652 | 652 | An interactive session is a session where input can be reasonably read |
|
653 | 653 | from `sys.stdin'. If this function returns false, any attempt to read |
|
654 | 654 | from stdin should fail with an error, unless a sensible default has been |
|
655 | 655 | specified. |
|
656 | 656 | |
|
657 | 657 | Interactiveness is triggered by the value of the `ui.interactive' |
|
658 | 658 | configuration variable or - if it is unset - when `sys.stdin' points |
|
659 | 659 | to a terminal device. |
|
660 | 660 | |
|
661 | 661 | This function refers to input only; for output, see `ui.formatted()'. |
|
662 | 662 | ''' |
|
663 | 663 | i = self.configbool("ui", "interactive", None) |
|
664 | 664 | if i is None: |
|
665 | 665 | # some environments replace stdin without implementing isatty |
|
666 | 666 | # usually those are non-interactive |
|
667 | 667 | return self._isatty(self.fin) |
|
668 | 668 | |
|
669 | 669 | return i |
|
670 | 670 | |
|
671 | 671 | def termwidth(self): |
|
672 | 672 | '''how wide is the terminal in columns? |
|
673 | 673 | ''' |
|
674 | 674 | if 'COLUMNS' in os.environ: |
|
675 | 675 | try: |
|
676 | 676 | return int(os.environ['COLUMNS']) |
|
677 | 677 | except ValueError: |
|
678 | 678 | pass |
|
679 | 679 | return util.termwidth() |
|
680 | 680 | |
|
681 | 681 | def formatted(self): |
|
682 | 682 | '''should formatted output be used? |
|
683 | 683 | |
|
684 | 684 | It is often desirable to format the output to suite the output medium. |
|
685 | 685 | Examples of this are truncating long lines or colorizing messages. |
|
686 | 686 | However, this is not often not desirable when piping output into other |
|
687 | 687 | utilities, e.g. `grep'. |
|
688 | 688 | |
|
689 | 689 | Formatted output is triggered by the value of the `ui.formatted' |
|
690 | 690 | configuration variable or - if it is unset - when `sys.stdout' points |
|
691 | 691 | to a terminal device. Please note that `ui.formatted' should be |
|
692 | 692 | considered an implementation detail; it is not intended for use outside |
|
693 | 693 | Mercurial or its extensions. |
|
694 | 694 | |
|
695 | 695 | This function refers to output only; for input, see `ui.interactive()'. |
|
696 | 696 | This function always returns false when in plain mode, see `ui.plain()'. |
|
697 | 697 | ''' |
|
698 | 698 | if self.plain(): |
|
699 | 699 | return False |
|
700 | 700 | |
|
701 | 701 | i = self.configbool("ui", "formatted", None) |
|
702 | 702 | if i is None: |
|
703 | 703 | # some environments replace stdout without implementing isatty |
|
704 | 704 | # usually those are non-interactive |
|
705 | 705 | return self._isatty(self.fout) |
|
706 | 706 | |
|
707 | 707 | return i |
|
708 | 708 | |
|
709 | 709 | def _readline(self, prompt=''): |
|
710 | 710 | if self._isatty(self.fin): |
|
711 | 711 | try: |
|
712 | 712 | # magically add command line editing support, where |
|
713 | 713 | # available |
|
714 | 714 | import readline |
|
715 | 715 | # force demandimport to really load the module |
|
716 | 716 | readline.read_history_file |
|
717 | 717 | # windows sometimes raises something other than ImportError |
|
718 | 718 | except Exception: |
|
719 | 719 | pass |
|
720 | 720 | |
|
721 | 721 | # call write() so output goes through subclassed implementation |
|
722 | 722 | # e.g. color extension on Windows |
|
723 | 723 | self.write(prompt) |
|
724 | 724 | |
|
725 | 725 | # instead of trying to emulate raw_input, swap (self.fin, |
|
726 | 726 | # self.fout) with (sys.stdin, sys.stdout) |
|
727 | 727 | oldin = sys.stdin |
|
728 | 728 | oldout = sys.stdout |
|
729 | 729 | sys.stdin = self.fin |
|
730 | 730 | sys.stdout = self.fout |
|
731 | 731 | # prompt ' ' must exist; otherwise readline may delete entire line |
|
732 | 732 | # - http://bugs.python.org/issue12833 |
|
733 | 733 | line = raw_input(' ') |
|
734 | 734 | sys.stdin = oldin |
|
735 | 735 | sys.stdout = oldout |
|
736 | 736 | |
|
737 | 737 | # When stdin is in binary mode on Windows, it can cause |
|
738 | 738 | # raw_input() to emit an extra trailing carriage return |
|
739 | 739 | if os.linesep == '\r\n' and line and line[-1] == '\r': |
|
740 | 740 | line = line[:-1] |
|
741 | 741 | return line |
|
742 | 742 | |
|
743 | 743 | def prompt(self, msg, default="y"): |
|
744 | 744 | """Prompt user with msg, read response. |
|
745 | 745 | If ui is not interactive, the default is returned. |
|
746 | 746 | """ |
|
747 | 747 | if not self.interactive(): |
|
748 | 748 | self.write(msg, ' ', default, "\n") |
|
749 | 749 | return default |
|
750 | 750 | try: |
|
751 | 751 | r = self._readline(self.label(msg, 'ui.prompt')) |
|
752 | 752 | if not r: |
|
753 | 753 | r = default |
|
754 | 754 | if self.configbool('ui', 'promptecho'): |
|
755 | 755 | self.write(r, "\n") |
|
756 | 756 | return r |
|
757 | 757 | except EOFError: |
|
758 | 758 | raise util.Abort(_('response expected')) |
|
759 | 759 | |
|
760 | 760 | @staticmethod |
|
761 | 761 | def extractchoices(prompt): |
|
762 | 762 | """Extract prompt message and list of choices from specified prompt. |
|
763 | 763 | |
|
764 | 764 | This returns tuple "(message, choices)", and "choices" is the |
|
765 | 765 | list of tuple "(response character, text without &)". |
|
766 | 766 | """ |
|
767 | 767 | parts = prompt.split('$$') |
|
768 | 768 | msg = parts[0].rstrip(' ') |
|
769 | 769 | choices = [p.strip(' ') for p in parts[1:]] |
|
770 | 770 | return (msg, |
|
771 | 771 | [(s[s.index('&') + 1].lower(), s.replace('&', '', 1)) |
|
772 | 772 | for s in choices]) |
|
773 | 773 | |
|
774 | 774 | def promptchoice(self, prompt, default=0): |
|
775 | 775 | """Prompt user with a message, read response, and ensure it matches |
|
776 | 776 | one of the provided choices. The prompt is formatted as follows: |
|
777 | 777 | |
|
778 | 778 | "would you like fries with that (Yn)? $$ &Yes $$ &No" |
|
779 | 779 | |
|
780 | 780 | The index of the choice is returned. Responses are case |
|
781 | 781 | insensitive. If ui is not interactive, the default is |
|
782 | 782 | returned. |
|
783 | 783 | """ |
|
784 | 784 | |
|
785 | 785 | msg, choices = self.extractchoices(prompt) |
|
786 | 786 | resps = [r for r, t in choices] |
|
787 | 787 | while True: |
|
788 | 788 | r = self.prompt(msg, resps[default]) |
|
789 | 789 | if r.lower() in resps: |
|
790 | 790 | return resps.index(r.lower()) |
|
791 | 791 | self.write(_("unrecognized response\n")) |
|
792 | 792 | |
|
793 | 793 | def getpass(self, prompt=None, default=None): |
|
794 | 794 | if not self.interactive(): |
|
795 | 795 | return default |
|
796 | 796 | try: |
|
797 | 797 | self.write_err(self.label(prompt or _('password: '), 'ui.prompt')) |
|
798 | 798 | # disable getpass() only if explicitly specified. it's still valid |
|
799 | 799 | # to interact with tty even if fin is not a tty. |
|
800 | 800 | if self.configbool('ui', 'nontty'): |
|
801 | 801 | return self.fin.readline().rstrip('\n') |
|
802 | 802 | else: |
|
803 | 803 | return getpass.getpass('') |
|
804 | 804 | except EOFError: |
|
805 | 805 | raise util.Abort(_('response expected')) |
|
806 | 806 | def status(self, *msg, **opts): |
|
807 | 807 | '''write status message to output (if ui.quiet is False) |
|
808 | 808 | |
|
809 | 809 | This adds an output label of "ui.status". |
|
810 | 810 | ''' |
|
811 | 811 | if not self.quiet: |
|
812 | 812 | opts['label'] = opts.get('label', '') + ' ui.status' |
|
813 | 813 | self.write(*msg, **opts) |
|
814 | 814 | def warn(self, *msg, **opts): |
|
815 | 815 | '''write warning message to output (stderr) |
|
816 | 816 | |
|
817 | 817 | This adds an output label of "ui.warning". |
|
818 | 818 | ''' |
|
819 | 819 | opts['label'] = opts.get('label', '') + ' ui.warning' |
|
820 | 820 | self.write_err(*msg, **opts) |
|
821 | 821 | def note(self, *msg, **opts): |
|
822 | 822 | '''write note to output (if ui.verbose is True) |
|
823 | 823 | |
|
824 | 824 | This adds an output label of "ui.note". |
|
825 | 825 | ''' |
|
826 | 826 | if self.verbose: |
|
827 | 827 | opts['label'] = opts.get('label', '') + ' ui.note' |
|
828 | 828 | self.write(*msg, **opts) |
|
829 | 829 | def debug(self, *msg, **opts): |
|
830 | 830 | '''write debug message to output (if ui.debugflag is True) |
|
831 | 831 | |
|
832 | 832 | This adds an output label of "ui.debug". |
|
833 | 833 | ''' |
|
834 | 834 | if self.debugflag: |
|
835 | 835 | opts['label'] = opts.get('label', '') + ' ui.debug' |
|
836 | 836 | self.write(*msg, **opts) |
|
837 | 837 | def edit(self, text, user, extra={}, editform=None): |
|
838 | 838 | (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt", |
|
839 | 839 | text=True) |
|
840 | 840 | try: |
|
841 | 841 | f = os.fdopen(fd, "w") |
|
842 | 842 | f.write(text) |
|
843 | 843 | f.close() |
|
844 | 844 | |
|
845 | 845 | environ = {'HGUSER': user} |
|
846 | 846 | if 'transplant_source' in extra: |
|
847 | 847 | environ.update({'HGREVISION': hex(extra['transplant_source'])}) |
|
848 | 848 | for label in ('intermediate-source', 'source', 'rebase_source'): |
|
849 | 849 | if label in extra: |
|
850 | 850 | environ.update({'HGREVISION': extra[label]}) |
|
851 | 851 | break |
|
852 | 852 | if editform: |
|
853 | 853 | environ.update({'HGEDITFORM': editform}) |
|
854 | 854 | |
|
855 | 855 | editor = self.geteditor() |
|
856 | 856 | |
|
857 | 857 | self.system("%s \"%s\"" % (editor, name), |
|
858 | 858 | environ=environ, |
|
859 | 859 | onerr=util.Abort, errprefix=_("edit failed")) |
|
860 | 860 | |
|
861 | 861 | f = open(name) |
|
862 | 862 | t = f.read() |
|
863 | 863 | f.close() |
|
864 | 864 | finally: |
|
865 | 865 | os.unlink(name) |
|
866 | 866 | |
|
867 | 867 | return t |
|
868 | 868 | |
|
869 | 869 | def system(self, cmd, environ={}, cwd=None, onerr=None, errprefix=None): |
|
870 | 870 | '''execute shell command with appropriate output stream. command |
|
871 | 871 | output will be redirected if fout is not stdout. |
|
872 | 872 | ''' |
|
873 | 873 | out = self.fout |
|
874 | 874 | if any(s[1] for s in self._bufferstates): |
|
875 | 875 | out = self |
|
876 | 876 | return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr, |
|
877 | 877 | errprefix=errprefix, out=out) |
|
878 | 878 | |
|
879 | 879 | def traceback(self, exc=None, force=False): |
|
880 | 880 | '''print exception traceback if traceback printing enabled or forced. |
|
881 | 881 | only to call in exception handler. returns true if traceback |
|
882 | 882 | printed.''' |
|
883 | 883 | if self.tracebackflag or force: |
|
884 | 884 | if exc is None: |
|
885 | 885 | exc = sys.exc_info() |
|
886 | 886 | cause = getattr(exc[1], 'cause', None) |
|
887 | 887 | |
|
888 | 888 | if cause is not None: |
|
889 | 889 | causetb = traceback.format_tb(cause[2]) |
|
890 | 890 | exctb = traceback.format_tb(exc[2]) |
|
891 | 891 | exconly = traceback.format_exception_only(cause[0], cause[1]) |
|
892 | 892 | |
|
893 | 893 | # exclude frame where 'exc' was chained and rethrown from exctb |
|
894 | 894 | self.write_err('Traceback (most recent call last):\n', |
|
895 | 895 | ''.join(exctb[:-1]), |
|
896 | 896 | ''.join(causetb), |
|
897 | 897 | ''.join(exconly)) |
|
898 | 898 | else: |
|
899 | 899 | output = traceback.format_exception(exc[0], exc[1], exc[2]) |
|
900 | 900 | self.write_err(''.join(output)) |
|
901 | 901 | return self.tracebackflag or force |
|
902 | 902 | |
|
903 | 903 | def geteditor(self): |
|
904 | 904 | '''return editor to use''' |
|
905 | 905 | if sys.platform == 'plan9': |
|
906 | 906 | # vi is the MIPS instruction simulator on Plan 9. We |
|
907 | 907 | # instead default to E to plumb commit messages to |
|
908 | 908 | # avoid confusion. |
|
909 | 909 | editor = 'E' |
|
910 | 910 | else: |
|
911 | 911 | editor = 'vi' |
|
912 | 912 | return (os.environ.get("HGEDITOR") or |
|
913 | 913 | self.config("ui", "editor") or |
|
914 | 914 | os.environ.get("VISUAL") or |
|
915 | 915 | os.environ.get("EDITOR", editor)) |
|
916 | 916 | |
|
917 | 917 | @util.propertycache |
|
918 | 918 | def _progbar(self): |
|
919 | 919 | """setup the progbar singleton to the ui object""" |
|
920 | 920 | if (self.quiet or self.debugflag |
|
921 | 921 | or self.configbool('progress', 'disable', False) |
|
922 | 922 | or not progress.shouldprint(self)): |
|
923 | 923 | return None |
|
924 | 924 | return getprogbar(self) |
|
925 | 925 | |
|
926 | 926 | def _progclear(self): |
|
927 | 927 | """clear progress bar output if any. use it before any output""" |
|
928 | 928 | if '_progbar' not in vars(self): # nothing loadef yet |
|
929 | 929 | return |
|
930 | 930 | if self._progbar is not None and self._progbar.printed: |
|
931 | 931 | self._progbar.clear() |
|
932 | 932 | |
|
933 | 933 | def progress(self, topic, pos, item="", unit="", total=None): |
|
934 | 934 | '''show a progress message |
|
935 | 935 | |
|
936 | 936 | With stock hg, this is simply a debug message that is hidden |
|
937 | 937 | by default, but with extensions or GUI tools it may be |
|
938 | 938 | visible. 'topic' is the current operation, 'item' is a |
|
939 | 939 | non-numeric marker of the current position (i.e. the currently |
|
940 | 940 | in-process file), 'pos' is the current numeric position (i.e. |
|
941 | 941 | revision, bytes, etc.), unit is a corresponding unit label, |
|
942 | 942 | and total is the highest expected pos. |
|
943 | 943 | |
|
944 | 944 | Multiple nested topics may be active at a time. |
|
945 | 945 | |
|
946 | 946 | All topics should be marked closed by setting pos to None at |
|
947 | 947 | termination. |
|
948 | 948 | ''' |
|
949 | 949 | if self._progbar is not None: |
|
950 | 950 | self._progbar.progress(topic, pos, item=item, unit=unit, |
|
951 | 951 | total=total) |
|
952 | 952 | if pos is None or not self.configbool('progress', 'debug'): |
|
953 | 953 | return |
|
954 | 954 | |
|
955 | 955 | if unit: |
|
956 | 956 | unit = ' ' + unit |
|
957 | 957 | if item: |
|
958 | 958 | item = ' ' + item |
|
959 | 959 | |
|
960 | 960 | if total: |
|
961 | 961 | pct = 100.0 * pos / total |
|
962 | 962 | self.debug('%s:%s %s/%s%s (%4.2f%%)\n' |
|
963 | 963 | % (topic, item, pos, total, unit, pct)) |
|
964 | 964 | else: |
|
965 | 965 | self.debug('%s:%s %s%s\n' % (topic, item, pos, unit)) |
|
966 | 966 | |
|
967 | 967 | def log(self, service, *msg, **opts): |
|
968 | 968 | '''hook for logging facility extensions |
|
969 | 969 | |
|
970 | 970 | service should be a readily-identifiable subsystem, which will |
|
971 | 971 | allow filtering. |
|
972 | message should be a newline-terminated string to log. | |
|
972 | ||
|
973 | *msg should be a newline-terminated format string to log, and | |
|
974 | then any values to %-format into that format string. | |
|
975 | ||
|
976 | **opts currently has no defined meanings. | |
|
973 | 977 | ''' |
|
974 | pass | |
|
975 | 978 | |
|
976 | 979 | def label(self, msg, label): |
|
977 | 980 | '''style msg based on supplied label |
|
978 | 981 | |
|
979 | 982 | Like ui.write(), this just returns msg unchanged, but extensions |
|
980 | 983 | and GUI tools can override it to allow styling output without |
|
981 | 984 | writing it. |
|
982 | 985 | |
|
983 | 986 | ui.write(s, 'label') is equivalent to |
|
984 | 987 | ui.write(ui.label(s, 'label')). |
|
985 | 988 | ''' |
|
986 | 989 | return msg |
|
987 | 990 | |
|
988 | 991 | def develwarn(self, msg): |
|
989 | 992 | """issue a developer warning message""" |
|
990 | 993 | msg = 'devel-warn: ' + msg |
|
991 | 994 | if self.tracebackflag: |
|
992 | 995 | util.debugstacktrace(msg, 2) |
|
993 | 996 | else: |
|
994 | 997 | curframe = inspect.currentframe() |
|
995 | 998 | calframe = inspect.getouterframes(curframe, 2) |
|
996 | 999 | self.write_err('%s at: %s:%s (%s)\n' % ((msg,) + calframe[2][1:4])) |
|
997 | 1000 | |
|
998 | 1001 | class paths(dict): |
|
999 | 1002 | """Represents a collection of paths and their configs. |
|
1000 | 1003 | |
|
1001 | 1004 | Data is initially derived from ui instances and the config files they have |
|
1002 | 1005 | loaded. |
|
1003 | 1006 | """ |
|
1004 | 1007 | def __init__(self, ui): |
|
1005 | 1008 | dict.__init__(self) |
|
1006 | 1009 | |
|
1007 | 1010 | for name, loc in ui.configitems('paths'): |
|
1008 | 1011 | # No location is the same as not existing. |
|
1009 | 1012 | if not loc: |
|
1010 | 1013 | continue |
|
1011 | 1014 | |
|
1012 | 1015 | # TODO ignore default-push once all consumers stop referencing it |
|
1013 | 1016 | # since it is handled specifically below. |
|
1014 | 1017 | |
|
1015 | 1018 | self[name] = path(name, rawloc=loc) |
|
1016 | 1019 | |
|
1017 | 1020 | # Handle default-push, which is a one-off that defines the push URL for |
|
1018 | 1021 | # the "default" path. |
|
1019 | 1022 | defaultpush = ui.config('paths', 'default-push') |
|
1020 | 1023 | if defaultpush and 'default' in self: |
|
1021 | 1024 | self['default']._pushloc = defaultpush |
|
1022 | 1025 | |
|
1023 | 1026 | def getpath(self, name, default=None): |
|
1024 | 1027 | """Return a ``path`` from a string, falling back to a default. |
|
1025 | 1028 | |
|
1026 | 1029 | ``name`` can be a named path or locations. Locations are filesystem |
|
1027 | 1030 | paths or URIs. |
|
1028 | 1031 | |
|
1029 | 1032 | Returns None if ``name`` is not a registered path, a URI, or a local |
|
1030 | 1033 | path to a repo. |
|
1031 | 1034 | """ |
|
1032 | 1035 | # Only fall back to default if no path was requested. |
|
1033 | 1036 | if name is None: |
|
1034 | 1037 | if default: |
|
1035 | 1038 | try: |
|
1036 | 1039 | return self[default] |
|
1037 | 1040 | except KeyError: |
|
1038 | 1041 | return None |
|
1039 | 1042 | else: |
|
1040 | 1043 | return None |
|
1041 | 1044 | |
|
1042 | 1045 | # Most likely empty string. |
|
1043 | 1046 | # This may need to raise in the future. |
|
1044 | 1047 | if not name: |
|
1045 | 1048 | return None |
|
1046 | 1049 | |
|
1047 | 1050 | try: |
|
1048 | 1051 | return self[name] |
|
1049 | 1052 | except KeyError: |
|
1050 | 1053 | # Try to resolve as a local path or URI. |
|
1051 | 1054 | try: |
|
1052 | 1055 | return path(None, rawloc=name) |
|
1053 | 1056 | except ValueError: |
|
1054 | 1057 | raise error.RepoError(_('repository %s does not exist') % |
|
1055 | 1058 | name) |
|
1056 | 1059 | |
|
1057 | 1060 | assert False |
|
1058 | 1061 | |
|
1059 | 1062 | class path(object): |
|
1060 | 1063 | """Represents an individual path and its configuration.""" |
|
1061 | 1064 | |
|
1062 | 1065 | def __init__(self, name, rawloc=None, pushloc=None): |
|
1063 | 1066 | """Construct a path from its config options. |
|
1064 | 1067 | |
|
1065 | 1068 | ``name`` is the symbolic name of the path. |
|
1066 | 1069 | ``rawloc`` is the raw location, as defined in the config. |
|
1067 | 1070 | ``pushloc`` is the raw locations pushes should be made to. |
|
1068 | 1071 | |
|
1069 | 1072 | If ``name`` is not defined, we require that the location be a) a local |
|
1070 | 1073 | filesystem path with a .hg directory or b) a URL. If not, |
|
1071 | 1074 | ``ValueError`` is raised. |
|
1072 | 1075 | """ |
|
1073 | 1076 | if not rawloc: |
|
1074 | 1077 | raise ValueError('rawloc must be defined') |
|
1075 | 1078 | |
|
1076 | 1079 | # Locations may define branches via syntax <base>#<branch>. |
|
1077 | 1080 | u = util.url(rawloc) |
|
1078 | 1081 | branch = None |
|
1079 | 1082 | if u.fragment: |
|
1080 | 1083 | branch = u.fragment |
|
1081 | 1084 | u.fragment = None |
|
1082 | 1085 | |
|
1083 | 1086 | self.url = u |
|
1084 | 1087 | self.branch = branch |
|
1085 | 1088 | |
|
1086 | 1089 | self.name = name |
|
1087 | 1090 | self.rawloc = rawloc |
|
1088 | 1091 | self.loc = str(u) |
|
1089 | 1092 | self._pushloc = pushloc |
|
1090 | 1093 | |
|
1091 | 1094 | # When given a raw location but not a symbolic name, validate the |
|
1092 | 1095 | # location is valid. |
|
1093 | 1096 | if not name and not u.scheme and not self._isvalidlocalpath(self.loc): |
|
1094 | 1097 | raise ValueError('location is not a URL or path to a local ' |
|
1095 | 1098 | 'repo: %s' % rawloc) |
|
1096 | 1099 | |
|
1097 | 1100 | def _isvalidlocalpath(self, path): |
|
1098 | 1101 | """Returns True if the given path is a potentially valid repository. |
|
1099 | 1102 | This is its own function so that extensions can change the definition of |
|
1100 | 1103 | 'valid' in this case (like when pulling from a git repo into a hg |
|
1101 | 1104 | one).""" |
|
1102 | 1105 | return os.path.isdir(os.path.join(path, '.hg')) |
|
1103 | 1106 | |
|
1104 | 1107 | @property |
|
1105 | 1108 | def pushloc(self): |
|
1106 | 1109 | return self._pushloc or self.loc |
|
1107 | 1110 | |
|
1108 | 1111 | # we instantiate one globally shared progress bar to avoid |
|
1109 | 1112 | # competing progress bars when multiple UI objects get created |
|
1110 | 1113 | _progresssingleton = None |
|
1111 | 1114 | |
|
1112 | 1115 | def getprogbar(ui): |
|
1113 | 1116 | global _progresssingleton |
|
1114 | 1117 | if _progresssingleton is None: |
|
1115 | 1118 | # passing 'ui' object to the singleton is fishy, |
|
1116 | 1119 | # this is how the extension used to work but feel free to rework it. |
|
1117 | 1120 | _progresssingleton = progress.progbar(ui) |
|
1118 | 1121 | return _progresssingleton |
General Comments 0
You need to be logged in to leave comments.
Login now