2005-06-01 10:57:53 +04:00
|
|
|
# ui.py - user interface bits for mercurial
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
|
2005-06-01 10:57:53 +04:00
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2005-06-01 10:57:53 +04:00
|
|
|
|
2006-12-15 05:25:19 +03:00
|
|
|
from i18n import _
|
2009-05-31 01:20:30 +04:00
|
|
|
import errno, getpass, os, socket, sys, tempfile, traceback
|
2012-02-21 02:42:48 +04:00
|
|
|
import config, scmutil, util, error, formatter
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2005-11-19 09:48:47 +03:00
|
|
|
class ui(object):
|
2009-04-27 01:50:43 +04:00
|
|
|
def __init__(self, src=None):
|
2009-04-27 01:50:44 +04:00
|
|
|
self._buffers = []
|
2009-11-13 01:05:52 +03:00
|
|
|
self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
|
2009-04-27 01:50:44 +04:00
|
|
|
self._reportuntrusted = True
|
2009-04-27 01:50:44 +04:00
|
|
|
self._ocfg = config.config() # overlay
|
|
|
|
self._tcfg = config.config() # trusted
|
|
|
|
self._ucfg = config.config() # untrusted
|
2009-05-17 18:20:27 +04:00
|
|
|
self._trustusers = set()
|
|
|
|
self._trustgroups = set()
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
if src:
|
2011-06-07 14:39:09 +04:00
|
|
|
self.fout = src.fout
|
|
|
|
self.ferr = src.ferr
|
|
|
|
self.fin = src.fin
|
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
self._tcfg = src._tcfg.copy()
|
|
|
|
self._ucfg = src._ucfg.copy()
|
|
|
|
self._ocfg = src._ocfg.copy()
|
2009-04-27 01:50:44 +04:00
|
|
|
self._trustusers = src._trustusers.copy()
|
|
|
|
self._trustgroups = src._trustgroups.copy()
|
2009-11-19 12:32:33 +03:00
|
|
|
self.environ = src.environ
|
2009-04-24 00:40:10 +04:00
|
|
|
self.fixconfig()
|
2009-04-24 00:40:10 +04:00
|
|
|
else:
|
2011-06-07 14:39:09 +04:00
|
|
|
self.fout = sys.stdout
|
|
|
|
self.ferr = sys.stderr
|
|
|
|
self.fin = sys.stdin
|
|
|
|
|
2009-11-19 12:32:33 +03:00
|
|
|
# shared read-only environment
|
|
|
|
self.environ = os.environ
|
2009-04-24 00:40:10 +04:00
|
|
|
# we always trust global config files
|
2011-04-21 22:14:29 +04:00
|
|
|
for f in scmutil.rcpath():
|
2009-04-27 01:50:44 +04:00
|
|
|
self.readconfig(f, trust=True)
|
2009-04-27 14:37:19 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
def copy(self):
|
2009-04-27 12:33:57 +04:00
|
|
|
return self.__class__(self)
|
2005-06-09 02:45:06 +04:00
|
|
|
|
2012-02-21 02:42:48 +04:00
|
|
|
def formatter(self, topic, opts):
|
|
|
|
return formatter.formatter(self, topic, opts)
|
|
|
|
|
2011-07-13 01:36:22 +04:00
|
|
|
def _trusted(self, fp, f):
|
2006-11-19 04:51:14 +03:00
|
|
|
st = util.fstat(fp)
|
2009-05-31 01:42:35 +04:00
|
|
|
if util.isowner(st):
|
2006-11-19 04:51:14 +03:00
|
|
|
return True
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
tusers, tgroups = self._trustusers, self._trustgroups
|
2009-04-24 00:40:10 +04:00
|
|
|
if '*' in tusers or '*' in tgroups:
|
|
|
|
return True
|
|
|
|
|
|
|
|
user = util.username(st.st_uid)
|
|
|
|
group = util.groupname(st.st_gid)
|
|
|
|
if user in tusers or group in tgroups or user == util.username():
|
|
|
|
return True
|
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
if self._reportuntrusted:
|
2009-04-24 00:40:10 +04:00
|
|
|
self.warn(_('Not trusting file %s from untrusted '
|
|
|
|
'user %s, group %s\n') % (f, user, group))
|
|
|
|
return False
|
2006-10-26 21:25:44 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
def readconfig(self, filename, root=None, trust=False,
|
2009-05-11 16:20:18 +04:00
|
|
|
sections=None, remap=None):
|
2009-04-24 00:40:10 +04:00
|
|
|
try:
|
|
|
|
fp = open(filename)
|
|
|
|
except IOError:
|
|
|
|
if not sections: # ignore unless we were looking for something
|
|
|
|
return
|
|
|
|
raise
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
cfg = config.config()
|
2011-07-13 01:36:22 +04:00
|
|
|
trusted = sections or trust or self._trusted(fp, filename)
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
try:
|
2009-05-11 16:20:18 +04:00
|
|
|
cfg.read(filename, fp, sections=sections, remap=remap)
|
2011-11-03 20:24:55 +04:00
|
|
|
fp.close()
|
2009-04-24 00:40:10 +04:00
|
|
|
except error.ConfigError, inst:
|
2006-10-26 21:25:45 +04:00
|
|
|
if trusted:
|
2009-04-24 00:40:10 +04:00
|
|
|
raise
|
|
|
|
self.warn(_("Ignored: %s\n") % str(inst))
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2010-02-07 16:56:18 +03:00
|
|
|
if self.plain():
|
2010-02-19 00:51:39 +03:00
|
|
|
for k in ('debug', 'fallbackencoding', 'quiet', 'slash',
|
2010-03-02 18:12:05 +03:00
|
|
|
'logtemplate', 'style',
|
2010-02-19 00:51:39 +03:00
|
|
|
'traceback', 'verbose'):
|
2010-02-07 16:56:18 +03:00
|
|
|
if k in cfg['ui']:
|
|
|
|
del cfg['ui'][k]
|
|
|
|
for k, v in cfg.items('defaults'):
|
|
|
|
del cfg['defaults'][k]
|
2011-05-17 02:17:52 +04:00
|
|
|
# Don't remove aliases from the configuration if in the exceptionlist
|
|
|
|
if self.plain('alias'):
|
|
|
|
for k, v in cfg.items('alias'):
|
|
|
|
del cfg['alias'][k]
|
2010-02-07 16:56:18 +03:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
if trusted:
|
2009-04-27 01:50:44 +04:00
|
|
|
self._tcfg.update(cfg)
|
|
|
|
self._tcfg.update(self._ocfg)
|
|
|
|
self._ucfg.update(cfg)
|
|
|
|
self._ucfg.update(self._ocfg)
|
2009-04-24 00:40:10 +04:00
|
|
|
|
2006-10-11 01:43:20 +04:00
|
|
|
if root is None:
|
|
|
|
root = os.path.expanduser('~')
|
|
|
|
self.fixconfig(root=root)
|
2006-08-23 05:49:30 +04:00
|
|
|
|
2010-10-18 09:10:36 +04:00
|
|
|
def fixconfig(self, root=None, section=None):
|
|
|
|
if section in (None, 'paths'):
|
|
|
|
# expand vars and ~
|
|
|
|
# translate paths relative to root (or home) into absolute paths
|
|
|
|
root = root or os.getcwd()
|
|
|
|
for c in self._tcfg, self._ucfg, self._ocfg:
|
|
|
|
for n, p in c.items('paths'):
|
|
|
|
if not p:
|
|
|
|
continue
|
|
|
|
if '%%' in p:
|
|
|
|
self.warn(_("(deprecated '%%' in path %s=%s from %s)\n")
|
|
|
|
% (n, p, self.configsource('paths', n)))
|
|
|
|
p = p.replace('%%', '%')
|
|
|
|
p = util.expandpath(p)
|
2011-04-30 20:43:20 +04:00
|
|
|
if not util.hasscheme(p) and not os.path.isabs(p):
|
2010-10-18 09:10:36 +04:00
|
|
|
p = os.path.normpath(os.path.join(root, p))
|
|
|
|
c.set("paths", n, p)
|
|
|
|
|
|
|
|
if section in (None, 'ui'):
|
|
|
|
# update ui options
|
|
|
|
self.debugflag = self.configbool('ui', 'debug')
|
|
|
|
self.verbose = self.debugflag or self.configbool('ui', 'verbose')
|
|
|
|
self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
|
|
|
|
if self.verbose and self.quiet:
|
|
|
|
self.quiet = self.verbose = False
|
2011-02-26 08:26:24 +03:00
|
|
|
self._reportuntrusted = self.debugflag or self.configbool("ui",
|
|
|
|
"report_untrusted", True)
|
2010-10-18 09:10:36 +04:00
|
|
|
self.tracebackflag = self.configbool('ui', 'traceback', False)
|
|
|
|
|
|
|
|
if section in (None, 'trusted'):
|
|
|
|
# update trust information
|
|
|
|
self._trustusers.update(self.configlist('trusted', 'users'))
|
|
|
|
self._trustgroups.update(self.configlist('trusted', 'groups'))
|
2006-10-26 21:25:44 +04:00
|
|
|
|
2012-01-18 19:52:26 +04:00
|
|
|
def backupconfig(self, section, item):
|
|
|
|
return (self._ocfg.backup(section, item),
|
|
|
|
self._tcfg.backup(section, item),
|
|
|
|
self._ucfg.backup(section, item),)
|
|
|
|
def restoreconfig(self, data):
|
|
|
|
self._ocfg.restore(data[0])
|
|
|
|
self._tcfg.restore(data[1])
|
|
|
|
self._ucfg.restore(data[2])
|
|
|
|
|
2010-08-18 04:43:45 +04:00
|
|
|
def setconfig(self, section, name, value, overlay=True):
|
|
|
|
if overlay:
|
|
|
|
self._ocfg.set(section, name, value)
|
|
|
|
self._tcfg.set(section, name, value)
|
|
|
|
self._ucfg.set(section, name, value)
|
2010-10-18 09:10:36 +04:00
|
|
|
self.fixconfig(section=section)
|
2005-08-20 09:25:25 +04:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
def _data(self, untrusted):
|
2009-04-27 01:50:44 +04:00
|
|
|
return untrusted and self._ucfg or self._tcfg
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2009-04-27 01:50:43 +04:00
|
|
|
def configsource(self, section, name, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
return self._data(untrusted).source(section, name) or 'none'
|
2009-04-27 01:50:43 +04:00
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
def config(self, section, name, default=None, untrusted=False):
|
2011-08-11 23:34:03 +04:00
|
|
|
if isinstance(name, list):
|
|
|
|
alternates = name
|
|
|
|
else:
|
|
|
|
alternates = [name]
|
|
|
|
|
|
|
|
for n in alternates:
|
|
|
|
value = self._data(untrusted).get(section, name, None)
|
|
|
|
if value is not None:
|
|
|
|
name = n
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
value = default
|
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
if self.debugflag and not untrusted and self._reportuntrusted:
|
2009-04-27 01:50:44 +04:00
|
|
|
uvalue = self._ucfg.get(section, name)
|
2006-10-26 21:25:45 +04:00
|
|
|
if uvalue is not None and uvalue != value:
|
2011-06-21 20:35:13 +04:00
|
|
|
self.debug("ignoring untrusted configuration option "
|
|
|
|
"%s.%s = %s\n" % (section, name, uvalue))
|
2006-10-26 21:25:45 +04:00
|
|
|
return value
|
|
|
|
|
2011-01-07 02:04:33 +03:00
|
|
|
def configpath(self, section, name, default=None, untrusted=False):
|
2011-07-23 08:09:14 +04:00
|
|
|
'get a path config item, expanded relative to repo root or config file'
|
2011-01-07 02:04:33 +03:00
|
|
|
v = self.config(section, name, default, untrusted)
|
2011-07-23 08:08:52 +04:00
|
|
|
if v is None:
|
|
|
|
return None
|
2011-01-07 02:04:33 +03:00
|
|
|
if not os.path.isabs(v) or "://" not in v:
|
|
|
|
src = self.configsource(section, name, untrusted)
|
|
|
|
if ':' in src:
|
2011-07-23 08:08:49 +04:00
|
|
|
base = os.path.dirname(src.rsplit(':')[0])
|
2011-01-07 02:04:33 +03:00
|
|
|
v = os.path.join(base, os.path.expanduser(v))
|
|
|
|
return v
|
|
|
|
|
2006-10-26 21:25:45 +04:00
|
|
|
def configbool(self, section, name, default=False, untrusted=False):
|
2011-05-04 00:28:08 +04:00
|
|
|
"""parse a configuration element as a boolean
|
|
|
|
|
|
|
|
>>> u = ui(); s = 'foo'
|
|
|
|
>>> u.setconfig(s, 'true', 'yes')
|
|
|
|
>>> u.configbool(s, 'true')
|
|
|
|
True
|
|
|
|
>>> u.setconfig(s, 'false', 'no')
|
|
|
|
>>> u.configbool(s, 'false')
|
|
|
|
False
|
|
|
|
>>> u.configbool(s, 'unknown')
|
|
|
|
False
|
|
|
|
>>> u.configbool(s, 'unknown', True)
|
|
|
|
True
|
|
|
|
>>> u.setconfig(s, 'invalid', 'somevalue')
|
|
|
|
>>> u.configbool(s, 'invalid')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ConfigError: foo.invalid is not a boolean ('somevalue')
|
|
|
|
"""
|
|
|
|
|
2009-04-24 00:40:10 +04:00
|
|
|
v = self.config(section, name, None, untrusted)
|
2009-05-20 02:52:46 +04:00
|
|
|
if v is None:
|
2009-04-24 00:40:10 +04:00
|
|
|
return default
|
2010-01-13 13:53:55 +03:00
|
|
|
if isinstance(v, bool):
|
|
|
|
return v
|
2010-08-29 06:50:35 +04:00
|
|
|
b = util.parsebool(v)
|
|
|
|
if b is None:
|
2011-05-04 00:28:08 +04:00
|
|
|
raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
|
2009-04-24 00:40:10 +04:00
|
|
|
% (section, name, v))
|
2010-08-29 06:50:35 +04:00
|
|
|
return b
|
2006-10-26 21:25:45 +04:00
|
|
|
|
2011-05-04 00:28:08 +04:00
|
|
|
def configint(self, section, name, default=None, untrusted=False):
|
|
|
|
"""parse a configuration element as an integer
|
|
|
|
|
|
|
|
>>> u = ui(); s = 'foo'
|
|
|
|
>>> u.setconfig(s, 'int1', '42')
|
|
|
|
>>> u.configint(s, 'int1')
|
|
|
|
42
|
|
|
|
>>> u.setconfig(s, 'int2', '-42')
|
|
|
|
>>> u.configint(s, 'int2')
|
|
|
|
-42
|
|
|
|
>>> u.configint(s, 'unknown', 7)
|
|
|
|
7
|
|
|
|
>>> u.setconfig(s, 'invalid', 'somevalue')
|
|
|
|
>>> u.configint(s, 'invalid')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ConfigError: foo.invalid is not an integer ('somevalue')
|
|
|
|
"""
|
|
|
|
|
|
|
|
v = self.config(section, name, None, untrusted)
|
|
|
|
if v is None:
|
|
|
|
return default
|
|
|
|
try:
|
|
|
|
return int(v)
|
|
|
|
except ValueError:
|
|
|
|
raise error.ConfigError(_("%s.%s is not an integer ('%s')")
|
|
|
|
% (section, name, v))
|
|
|
|
|
2006-10-26 21:25:45 +04:00
|
|
|
def configlist(self, section, name, default=None, untrusted=False):
|
2011-05-04 00:28:08 +04:00
|
|
|
"""parse a configuration element as a list of comma/space separated
|
|
|
|
strings
|
|
|
|
|
|
|
|
>>> u = ui(); s = 'foo'
|
|
|
|
>>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
|
|
|
|
>>> u.configlist(s, 'list1')
|
|
|
|
['this', 'is', 'a small', 'test']
|
|
|
|
"""
|
2010-04-25 19:38:41 +04:00
|
|
|
|
|
|
|
def _parse_plain(parts, s, offset):
|
|
|
|
whitespace = False
|
|
|
|
while offset < len(s) and (s[offset].isspace() or s[offset] == ','):
|
|
|
|
whitespace = True
|
|
|
|
offset += 1
|
|
|
|
if offset >= len(s):
|
|
|
|
return None, parts, offset
|
|
|
|
if whitespace:
|
|
|
|
parts.append('')
|
|
|
|
if s[offset] == '"' and not parts[-1]:
|
|
|
|
return _parse_quote, parts, offset + 1
|
|
|
|
elif s[offset] == '"' and parts[-1][-1] == '\\':
|
|
|
|
parts[-1] = parts[-1][:-1] + s[offset]
|
|
|
|
return _parse_plain, parts, offset + 1
|
|
|
|
parts[-1] += s[offset]
|
|
|
|
return _parse_plain, parts, offset + 1
|
|
|
|
|
|
|
|
def _parse_quote(parts, s, offset):
|
|
|
|
if offset < len(s) and s[offset] == '"': # ""
|
|
|
|
parts.append('')
|
|
|
|
offset += 1
|
|
|
|
while offset < len(s) and (s[offset].isspace() or
|
|
|
|
s[offset] == ','):
|
|
|
|
offset += 1
|
|
|
|
return _parse_plain, parts, offset
|
|
|
|
|
|
|
|
while offset < len(s) and s[offset] != '"':
|
2010-04-28 23:00:07 +04:00
|
|
|
if (s[offset] == '\\' and offset + 1 < len(s)
|
|
|
|
and s[offset + 1] == '"'):
|
2010-04-25 19:38:41 +04:00
|
|
|
offset += 1
|
|
|
|
parts[-1] += '"'
|
|
|
|
else:
|
|
|
|
parts[-1] += s[offset]
|
|
|
|
offset += 1
|
|
|
|
|
|
|
|
if offset >= len(s):
|
|
|
|
real_parts = _configlist(parts[-1])
|
|
|
|
if not real_parts:
|
|
|
|
parts[-1] = '"'
|
|
|
|
else:
|
|
|
|
real_parts[0] = '"' + real_parts[0]
|
|
|
|
parts = parts[:-1]
|
|
|
|
parts.extend(real_parts)
|
|
|
|
return None, parts, offset
|
|
|
|
|
|
|
|
offset += 1
|
|
|
|
while offset < len(s) and s[offset] in [' ', ',']:
|
|
|
|
offset += 1
|
|
|
|
|
|
|
|
if offset < len(s):
|
|
|
|
if offset + 1 == len(s) and s[offset] == '"':
|
|
|
|
parts[-1] += '"'
|
|
|
|
offset += 1
|
|
|
|
else:
|
|
|
|
parts.append('')
|
|
|
|
else:
|
|
|
|
return None, parts, offset
|
|
|
|
|
|
|
|
return _parse_plain, parts, offset
|
|
|
|
|
|
|
|
def _configlist(s):
|
|
|
|
s = s.rstrip(' ,')
|
|
|
|
if not s:
|
2010-08-11 16:28:39 +04:00
|
|
|
return []
|
2010-04-25 19:38:41 +04:00
|
|
|
parser, parts, offset = _parse_plain, [''], 0
|
|
|
|
while parser:
|
|
|
|
parser, parts, offset = parser(parts, s, offset)
|
|
|
|
return parts
|
|
|
|
|
2006-10-26 21:25:45 +04:00
|
|
|
result = self.config(section, name, untrusted=untrusted)
|
2006-06-26 16:52:26 +04:00
|
|
|
if result is None:
|
2006-06-27 00:44:48 +04:00
|
|
|
result = default or []
|
|
|
|
if isinstance(result, basestring):
|
2010-06-08 23:09:41 +04:00
|
|
|
result = _configlist(result.lstrip(' ,\n'))
|
2010-04-25 19:38:41 +04:00
|
|
|
if result is None:
|
|
|
|
result = default or []
|
2006-06-27 00:44:48 +04:00
|
|
|
return result
|
2006-06-26 16:52:26 +04:00
|
|
|
|
2007-05-28 00:50:59 +04:00
|
|
|
def has_section(self, section, untrusted=False):
|
2006-05-24 01:57:45 +04:00
|
|
|
'''tell whether section exists in config.'''
|
2009-04-27 01:50:44 +04:00
|
|
|
return section in self._data(untrusted)
|
2006-10-26 21:25:45 +04:00
|
|
|
|
|
|
|
def configitems(self, section, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
items = self._data(untrusted).items(section)
|
2009-04-27 01:50:44 +04:00
|
|
|
if self.debugflag and not untrusted and self._reportuntrusted:
|
2009-04-27 14:37:19 +04:00
|
|
|
for k, v in self._ucfg.items(section):
|
2009-04-27 01:50:44 +04:00
|
|
|
if self._tcfg.get(section, k) != v:
|
2011-06-21 20:35:13 +04:00
|
|
|
self.debug("ignoring untrusted configuration option "
|
|
|
|
"%s.%s = %s\n" % (section, k, v))
|
2009-04-24 00:40:10 +04:00
|
|
|
return items
|
2006-03-06 19:47:41 +03:00
|
|
|
|
2006-10-26 21:25:45 +04:00
|
|
|
def walkconfig(self, untrusted=False):
|
2009-04-27 01:50:44 +04:00
|
|
|
cfg = self._data(untrusted)
|
|
|
|
for section in cfg.sections():
|
2006-10-26 21:25:45 +04:00
|
|
|
for name, value in self.configitems(section, untrusted):
|
2011-03-10 18:49:37 +03:00
|
|
|
yield section, name, value
|
2005-08-24 08:30:12 +04:00
|
|
|
|
2011-05-17 02:08:51 +04:00
|
|
|
def plain(self, feature=None):
|
2010-06-07 18:14:12 +04:00
|
|
|
'''is plain mode active?
|
|
|
|
|
2011-01-04 16:18:36 +03:00
|
|
|
Plain mode means that all configuration variables which affect
|
|
|
|
the behavior and output of Mercurial should be
|
|
|
|
ignored. Additionally, the output should be stable,
|
|
|
|
reproducible and suitable for use in scripts or applications.
|
2010-06-07 18:14:12 +04:00
|
|
|
|
2011-01-04 16:18:36 +03:00
|
|
|
The only way to trigger plain mode is by setting either the
|
|
|
|
`HGPLAIN' or `HGPLAINEXCEPT' environment variables.
|
|
|
|
|
2011-05-17 02:08:51 +04:00
|
|
|
The return value can either be
|
|
|
|
- False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
|
|
|
|
- True otherwise
|
2010-06-07 18:14:12 +04:00
|
|
|
'''
|
2011-01-04 16:18:36 +03:00
|
|
|
if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
|
|
|
|
return False
|
|
|
|
exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
|
2011-05-17 02:08:51 +04:00
|
|
|
if feature and exceptions:
|
|
|
|
return feature not in exceptions
|
|
|
|
return True
|
2010-02-07 16:56:18 +03:00
|
|
|
|
2005-07-04 09:51:09 +04:00
|
|
|
def username(self):
|
2006-03-21 17:33:29 +03:00
|
|
|
"""Return default username to be used in commits.
|
|
|
|
|
|
|
|
Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
|
|
|
|
and stop searching if one of these is set.
|
2008-08-06 17:10:05 +04:00
|
|
|
If not found and ui.askusername is True, ask the user, else use
|
|
|
|
($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
|
2006-03-21 17:33:29 +03:00
|
|
|
"""
|
|
|
|
user = os.environ.get("HGUSER")
|
|
|
|
if user is None:
|
|
|
|
user = self.config("ui", "username")
|
2010-05-27 01:03:29 +04:00
|
|
|
if user is not None:
|
|
|
|
user = os.path.expandvars(user)
|
2006-03-21 17:33:29 +03:00
|
|
|
if user is None:
|
|
|
|
user = os.environ.get("EMAIL")
|
2008-08-06 17:10:05 +04:00
|
|
|
if user is None and self.configbool("ui", "askusername"):
|
2009-01-03 19:15:21 +03:00
|
|
|
user = self.prompt(_("enter a commit username:"), default=None)
|
2009-10-20 02:17:36 +04:00
|
|
|
if user is None and not self.interactive():
|
2006-11-28 23:16:05 +03:00
|
|
|
try:
|
|
|
|
user = '%s@%s' % (util.getuser(), socket.getfqdn())
|
2007-01-25 01:04:51 +03:00
|
|
|
self.warn(_("No username found, using '%s' instead\n") % user)
|
2006-11-28 23:16:05 +03:00
|
|
|
except KeyError:
|
2007-01-25 01:04:51 +03:00
|
|
|
pass
|
|
|
|
if not user:
|
2009-11-08 00:13:09 +03:00
|
|
|
raise util.Abort(_('no username supplied (see "hg help config")'))
|
2008-03-22 21:50:59 +03:00
|
|
|
if "\n" in user:
|
2008-12-06 16:27:31 +03:00
|
|
|
raise util.Abort(_("username %s contains a newline\n") % repr(user))
|
2006-03-21 17:33:29 +03:00
|
|
|
return user
|
2005-07-04 09:51:09 +04:00
|
|
|
|
2005-08-28 19:29:28 +04:00
|
|
|
def shortuser(self, user):
|
|
|
|
"""Return a short representation of a user name or email address."""
|
2010-01-25 09:05:27 +03:00
|
|
|
if not self.verbose:
|
|
|
|
user = util.shortuser(user)
|
2005-08-28 19:29:28 +04:00
|
|
|
return user
|
|
|
|
|
2006-06-24 05:09:44 +04:00
|
|
|
def expandpath(self, loc, default=None):
|
2006-03-12 20:05:13 +03:00
|
|
|
"""Return repository location relative to cwd or from [paths]"""
|
2011-04-30 20:43:20 +04:00
|
|
|
if util.hasscheme(loc) or os.path.isdir(os.path.join(loc, '.hg')):
|
2006-03-12 20:05:13 +03:00
|
|
|
return loc
|
2005-06-28 14:30:36 +04:00
|
|
|
|
2010-10-09 21:28:16 +04:00
|
|
|
path = self.config('paths', loc)
|
2006-06-24 05:23:32 +04:00
|
|
|
if not path and default is not None:
|
2010-10-09 21:28:16 +04:00
|
|
|
path = self.config('paths', default)
|
2006-06-26 16:49:18 +04:00
|
|
|
return path or loc
|
2005-06-28 14:30:36 +04:00
|
|
|
|
2006-12-01 10:28:18 +03:00
|
|
|
def pushbuffer(self):
|
2009-04-27 01:50:44 +04:00
|
|
|
self._buffers.append([])
|
2006-12-01 10:28:18 +03:00
|
|
|
|
2010-04-03 00:22:00 +04:00
|
|
|
def popbuffer(self, labeled=False):
|
|
|
|
'''pop the last buffer and return the buffered output
|
|
|
|
|
|
|
|
If labeled is True, any labels associated with buffered
|
|
|
|
output will be handled. By default, this has no effect
|
|
|
|
on the output returned, but extensions and GUI tools may
|
|
|
|
handle this argument and returned styled output. If output
|
|
|
|
is being buffered so it can be captured and parsed or
|
|
|
|
processed, labeled should not be set to True.
|
|
|
|
'''
|
2009-04-27 01:50:44 +04:00
|
|
|
return "".join(self._buffers.pop())
|
2006-12-01 10:28:18 +03:00
|
|
|
|
2010-04-03 00:22:00 +04:00
|
|
|
def write(self, *args, **opts):
|
|
|
|
'''write args to output
|
|
|
|
|
|
|
|
By default, this method simply writes to the buffer or stdout,
|
|
|
|
but extensions or GUI tools may override this method,
|
|
|
|
write_err(), popbuffer(), and label() to style output from
|
|
|
|
various parts of hg.
|
|
|
|
|
|
|
|
An optional keyword argument, "label", can be passed in.
|
|
|
|
This should be a string containing label names separated by
|
|
|
|
space. Label names take the form of "topic.type". For example,
|
|
|
|
ui.debug() issues a label of "ui.debug".
|
|
|
|
|
|
|
|
When labeling output for a specific command, a label of
|
|
|
|
"cmdname.type" is recommended. For example, status issues
|
|
|
|
a label of "status.modified" for modified files.
|
|
|
|
'''
|
2009-04-27 01:50:44 +04:00
|
|
|
if self._buffers:
|
|
|
|
self._buffers[-1].extend([str(a) for a in args])
|
2006-12-01 10:28:18 +03:00
|
|
|
else:
|
|
|
|
for a in args:
|
2011-06-08 02:39:20 +04:00
|
|
|
self.fout.write(str(a))
|
2005-07-02 02:18:02 +04:00
|
|
|
|
2010-04-03 00:22:00 +04:00
|
|
|
def write_err(self, *args, **opts):
|
2006-03-24 01:16:41 +03:00
|
|
|
try:
|
2011-06-08 02:39:20 +04:00
|
|
|
if not getattr(self.fout, 'closed', False):
|
|
|
|
self.fout.flush()
|
2006-03-24 01:16:41 +03:00
|
|
|
for a in args:
|
2011-06-08 02:39:20 +04:00
|
|
|
self.ferr.write(str(a))
|
2007-01-10 22:35:27 +03:00
|
|
|
# stderr may be buffered under win32 when redirected to files,
|
|
|
|
# including stdout.
|
2011-06-08 02:39:20 +04:00
|
|
|
if not getattr(self.ferr, 'closed', False):
|
|
|
|
self.ferr.flush()
|
2006-03-24 01:16:41 +03:00
|
|
|
except IOError, inst:
|
2012-04-04 21:46:54 +04:00
|
|
|
if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
|
2006-03-24 01:16:41 +03:00
|
|
|
raise
|
2005-07-02 02:18:02 +04:00
|
|
|
|
2006-03-06 19:35:15 +03:00
|
|
|
def flush(self):
|
2011-06-08 02:39:20 +04:00
|
|
|
try: self.fout.flush()
|
2006-03-28 21:24:29 +04:00
|
|
|
except: pass
|
2011-06-08 02:39:20 +04:00
|
|
|
try: self.ferr.flush()
|
2006-03-28 21:24:29 +04:00
|
|
|
except: pass
|
2006-03-06 19:35:15 +03:00
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
def interactive(self):
|
2010-06-07 18:14:12 +04:00
|
|
|
'''is interactive input allowed?
|
|
|
|
|
|
|
|
An interactive session is a session where input can be reasonably read
|
|
|
|
from `sys.stdin'. If this function returns false, any attempt to read
|
|
|
|
from stdin should fail with an error, unless a sensible default has been
|
|
|
|
specified.
|
|
|
|
|
|
|
|
Interactiveness is triggered by the value of the `ui.interactive'
|
|
|
|
configuration variable or - if it is unset - when `sys.stdin' points
|
|
|
|
to a terminal device.
|
|
|
|
|
|
|
|
This function refers to input only; for output, see `ui.formatted()'.
|
|
|
|
'''
|
2009-05-21 22:59:36 +04:00
|
|
|
i = self.configbool("ui", "interactive", None)
|
|
|
|
if i is None:
|
2011-06-02 01:43:34 +04:00
|
|
|
# some environments replace stdin without implementing isatty
|
|
|
|
# usually those are non-interactive
|
2011-06-08 02:39:20 +04:00
|
|
|
return util.isatty(self.fin)
|
2009-12-13 20:29:16 +03:00
|
|
|
|
2009-05-21 22:59:36 +04:00
|
|
|
return i
|
2009-04-27 01:50:44 +04:00
|
|
|
|
2010-10-10 19:06:36 +04:00
|
|
|
def termwidth(self):
|
|
|
|
'''how wide is the terminal in columns?
|
|
|
|
'''
|
|
|
|
if 'COLUMNS' in os.environ:
|
|
|
|
try:
|
|
|
|
return int(os.environ['COLUMNS'])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return util.termwidth()
|
|
|
|
|
2010-06-07 16:59:52 +04:00
|
|
|
def formatted(self):
|
2010-06-07 18:14:12 +04:00
|
|
|
'''should formatted output be used?
|
|
|
|
|
|
|
|
It is often desirable to format the output to suite the output medium.
|
|
|
|
Examples of this are truncating long lines or colorizing messages.
|
|
|
|
However, this is not often not desirable when piping output into other
|
|
|
|
utilities, e.g. `grep'.
|
|
|
|
|
|
|
|
Formatted output is triggered by the value of the `ui.formatted'
|
|
|
|
configuration variable or - if it is unset - when `sys.stdout' points
|
|
|
|
to a terminal device. Please note that `ui.formatted' should be
|
|
|
|
considered an implementation detail; it is not intended for use outside
|
|
|
|
Mercurial or its extensions.
|
|
|
|
|
|
|
|
This function refers to output only; for input, see `ui.interactive()'.
|
|
|
|
This function always returns false when in plain mode, see `ui.plain()'.
|
|
|
|
'''
|
2010-06-07 16:59:52 +04:00
|
|
|
if self.plain():
|
|
|
|
return False
|
|
|
|
|
|
|
|
i = self.configbool("ui", "formatted", None)
|
|
|
|
if i is None:
|
2011-06-02 01:43:34 +04:00
|
|
|
# some environments replace stdout without implementing isatty
|
|
|
|
# usually those are non-interactive
|
2011-06-08 02:39:20 +04:00
|
|
|
return util.isatty(self.fout)
|
2010-06-07 16:59:52 +04:00
|
|
|
|
|
|
|
return i
|
|
|
|
|
2007-09-25 21:05:34 +04:00
|
|
|
def _readline(self, prompt=''):
|
2011-06-08 02:39:20 +04:00
|
|
|
if util.isatty(self.fin):
|
2007-08-01 03:28:05 +04:00
|
|
|
try:
|
|
|
|
# magically add command line editing support, where
|
|
|
|
# available
|
|
|
|
import readline
|
|
|
|
# force demandimport to really load the module
|
|
|
|
readline.read_history_file
|
2008-12-10 04:01:14 +03:00
|
|
|
# windows sometimes raises something other than ImportError
|
|
|
|
except Exception:
|
2007-08-01 03:28:05 +04:00
|
|
|
pass
|
2011-06-08 02:39:20 +04:00
|
|
|
|
2011-08-01 20:53:00 +04:00
|
|
|
# call write() so output goes through subclassed implementation
|
|
|
|
# e.g. color extension on Windows
|
|
|
|
self.write(prompt)
|
|
|
|
|
2011-08-30 16:18:58 +04:00
|
|
|
# instead of trying to emulate raw_input, swap (self.fin,
|
|
|
|
# self.fout) with (sys.stdin, sys.stdout)
|
|
|
|
oldin = sys.stdin
|
|
|
|
oldout = sys.stdout
|
2011-08-01 20:53:00 +04:00
|
|
|
sys.stdin = self.fin
|
2011-08-30 16:18:58 +04:00
|
|
|
sys.stdout = self.fout
|
2011-08-25 23:06:03 +04:00
|
|
|
line = raw_input(' ')
|
2011-08-30 16:18:58 +04:00
|
|
|
sys.stdin = oldin
|
|
|
|
sys.stdout = oldout
|
2011-06-08 02:39:20 +04:00
|
|
|
|
2007-12-04 02:28:26 +03:00
|
|
|
# When stdin is in binary mode on Windows, it can cause
|
|
|
|
# raw_input() to emit an extra trailing carriage return
|
|
|
|
if os.linesep == '\r\n' and line and line[-1] == '\r':
|
|
|
|
line = line[:-1]
|
|
|
|
return line
|
2007-08-01 03:28:05 +04:00
|
|
|
|
2009-06-21 03:13:19 +04:00
|
|
|
def prompt(self, msg, default="y"):
|
|
|
|
"""Prompt user with msg, read response.
|
|
|
|
If ui is not interactive, the default is returned.
|
2007-12-28 09:03:55 +03:00
|
|
|
"""
|
2009-04-27 01:50:44 +04:00
|
|
|
if not self.interactive():
|
2009-06-21 05:13:38 +04:00
|
|
|
self.write(msg, ' ', default, "\n")
|
2008-11-07 12:27:39 +03:00
|
|
|
return default
|
2009-06-21 03:13:19 +04:00
|
|
|
try:
|
2011-08-25 23:06:03 +04:00
|
|
|
r = self._readline(self.label(msg, 'ui.prompt'))
|
2009-06-21 03:13:19 +04:00
|
|
|
if not r:
|
|
|
|
return default
|
|
|
|
return r
|
|
|
|
except EOFError:
|
|
|
|
raise util.Abort(_('response expected'))
|
|
|
|
|
|
|
|
def promptchoice(self, msg, choices, default=0):
|
|
|
|
"""Prompt user with msg, read response, and ensure it matches
|
|
|
|
one of the provided choices. The index of the choice is returned.
|
|
|
|
choices is a sequence of acceptable responses with the format:
|
2009-08-05 19:08:28 +04:00
|
|
|
('&None', 'E&xec', 'Sym&link') Responses are case insensitive.
|
2009-06-21 03:13:19 +04:00
|
|
|
If ui is not interactive, the default is returned.
|
|
|
|
"""
|
|
|
|
resps = [s[s.index('&')+1].lower() for s in choices]
|
2007-12-27 12:32:33 +03:00
|
|
|
while True:
|
2009-06-21 03:13:19 +04:00
|
|
|
r = self.prompt(msg, resps[default])
|
|
|
|
if r.lower() in resps:
|
|
|
|
return resps.index(r.lower())
|
|
|
|
self.write(_("unrecognized response\n"))
|
|
|
|
|
2006-05-15 04:37:17 +04:00
|
|
|
def getpass(self, prompt=None, default=None):
|
2010-01-25 09:05:27 +03:00
|
|
|
if not self.interactive():
|
|
|
|
return default
|
2009-02-22 02:46:06 +03:00
|
|
|
try:
|
|
|
|
return getpass.getpass(prompt or _('password: '))
|
|
|
|
except EOFError:
|
|
|
|
raise util.Abort(_('response expected'))
|
2010-04-03 00:22:00 +04:00
|
|
|
def status(self, *msg, **opts):
|
|
|
|
'''write status message to output (if ui.quiet is False)
|
|
|
|
|
|
|
|
This adds an output label of "ui.status".
|
|
|
|
'''
|
2010-01-25 09:05:27 +03:00
|
|
|
if not self.quiet:
|
2010-04-03 00:22:00 +04:00
|
|
|
opts['label'] = opts.get('label', '') + ' ui.status'
|
|
|
|
self.write(*msg, **opts)
|
|
|
|
def warn(self, *msg, **opts):
|
|
|
|
'''write warning message to output (stderr)
|
|
|
|
|
|
|
|
This adds an output label of "ui.warning".
|
|
|
|
'''
|
|
|
|
opts['label'] = opts.get('label', '') + ' ui.warning'
|
|
|
|
self.write_err(*msg, **opts)
|
|
|
|
def note(self, *msg, **opts):
|
|
|
|
'''write note to output (if ui.verbose is True)
|
|
|
|
|
|
|
|
This adds an output label of "ui.note".
|
|
|
|
'''
|
2010-01-25 09:05:27 +03:00
|
|
|
if self.verbose:
|
2010-04-03 00:22:00 +04:00
|
|
|
opts['label'] = opts.get('label', '') + ' ui.note'
|
|
|
|
self.write(*msg, **opts)
|
|
|
|
def debug(self, *msg, **opts):
|
|
|
|
'''write debug message to output (if ui.debugflag is True)
|
|
|
|
|
|
|
|
This adds an output label of "ui.debug".
|
|
|
|
'''
|
2010-01-25 09:05:27 +03:00
|
|
|
if self.debugflag:
|
2010-04-03 00:22:00 +04:00
|
|
|
opts['label'] = opts.get('label', '') + ' ui.debug'
|
|
|
|
self.write(*msg, **opts)
|
2006-03-21 14:45:27 +03:00
|
|
|
def edit(self, text, user):
|
2006-05-05 02:42:14 +04:00
|
|
|
(fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt",
|
|
|
|
text=True)
|
2006-03-21 14:54:32 +03:00
|
|
|
try:
|
|
|
|
f = os.fdopen(fd, "w")
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
|
|
|
|
2007-12-05 14:40:01 +03:00
|
|
|
editor = self.geteditor()
|
2006-03-21 14:54:32 +03:00
|
|
|
|
|
|
|
util.system("%s \"%s\"" % (editor, name),
|
|
|
|
environ={'HGUSER': user},
|
2011-06-24 18:04:37 +04:00
|
|
|
onerr=util.Abort, errprefix=_("edit failed"),
|
|
|
|
out=self.fout)
|
2006-03-21 14:54:32 +03:00
|
|
|
|
|
|
|
f = open(name)
|
|
|
|
t = f.read()
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
os.unlink(name)
|
2005-07-11 04:00:17 +04:00
|
|
|
|
2005-06-01 10:57:53 +04:00
|
|
|
return t
|
2006-05-04 23:23:01 +04:00
|
|
|
|
2009-11-13 01:05:52 +03:00
|
|
|
def traceback(self, exc=None):
|
2006-05-22 19:47:53 +04:00
|
|
|
'''print exception traceback if traceback printing enabled.
|
|
|
|
only to call in exception handler. returns true if traceback
|
|
|
|
printed.'''
|
2009-11-13 01:05:52 +03:00
|
|
|
if self.tracebackflag:
|
|
|
|
if exc:
|
2011-09-11 16:42:30 +04:00
|
|
|
traceback.print_exception(exc[0], exc[1], exc[2], file=self.ferr)
|
2009-11-13 01:05:52 +03:00
|
|
|
else:
|
2011-09-11 16:42:30 +04:00
|
|
|
traceback.print_exc(file=self.ferr)
|
2009-11-13 01:05:52 +03:00
|
|
|
return self.tracebackflag
|
2007-12-05 14:40:01 +03:00
|
|
|
|
|
|
|
def geteditor(self):
|
|
|
|
'''return editor to use'''
|
2012-04-08 23:43:41 +04:00
|
|
|
if sys.platform == 'plan9':
|
|
|
|
# vi is the MIPS instruction simulator on Plan 9. We
|
|
|
|
# instead default to E to plumb commit messages to
|
|
|
|
# avoid confusion.
|
|
|
|
editor = 'E'
|
|
|
|
else:
|
|
|
|
editor = 'vi'
|
2007-12-05 14:40:01 +03:00
|
|
|
return (os.environ.get("HGEDITOR") or
|
|
|
|
self.config("ui", "editor") or
|
|
|
|
os.environ.get("VISUAL") or
|
2012-04-08 23:43:41 +04:00
|
|
|
os.environ.get("EDITOR", editor))
|
2009-07-16 23:49:52 +04:00
|
|
|
|
|
|
|
def progress(self, topic, pos, item="", unit="", total=None):
|
|
|
|
'''show a progress message
|
|
|
|
|
|
|
|
With stock hg, this is simply a debug message that is hidden
|
|
|
|
by default, but with extensions or GUI tools it may be
|
|
|
|
visible. 'topic' is the current operation, 'item' is a
|
|
|
|
non-numeric marker of the current position (ie the currently
|
|
|
|
in-process file), 'pos' is the current numeric position (ie
|
2009-08-18 22:47:41 +04:00
|
|
|
revision, bytes, etc.), unit is a corresponding unit label,
|
2009-07-16 23:49:52 +04:00
|
|
|
and total is the highest expected pos.
|
|
|
|
|
2010-02-07 19:25:18 +03:00
|
|
|
Multiple nested topics may be active at a time.
|
|
|
|
|
|
|
|
All topics should be marked closed by setting pos to None at
|
|
|
|
termination.
|
2009-07-16 23:49:52 +04:00
|
|
|
'''
|
|
|
|
|
2010-11-22 20:15:58 +03:00
|
|
|
if pos is None or not self.debugflag:
|
2009-07-16 23:49:52 +04:00
|
|
|
return
|
|
|
|
|
2009-08-18 22:47:41 +04:00
|
|
|
if unit:
|
|
|
|
unit = ' ' + unit
|
2009-07-16 23:49:52 +04:00
|
|
|
if item:
|
|
|
|
item = ' ' + item
|
|
|
|
|
|
|
|
if total:
|
|
|
|
pct = 100.0 * pos / total
|
2010-01-09 01:15:24 +03:00
|
|
|
self.debug('%s:%s %s/%s%s (%4.2f%%)\n'
|
2009-08-18 22:47:41 +04:00
|
|
|
% (topic, item, pos, total, unit, pct))
|
2009-07-16 23:49:52 +04:00
|
|
|
else:
|
2009-11-06 00:40:48 +03:00
|
|
|
self.debug('%s:%s %s%s\n' % (topic, item, pos, unit))
|
2010-04-03 00:22:00 +04:00
|
|
|
|
2010-08-19 20:14:02 +04:00
|
|
|
def log(self, service, message):
|
|
|
|
'''hook for logging facility extensions
|
|
|
|
|
|
|
|
service should be a readily-identifiable subsystem, which will
|
|
|
|
allow filtering.
|
|
|
|
message should be a newline-terminated string to log.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2010-04-03 00:22:00 +04:00
|
|
|
def label(self, msg, label):
|
|
|
|
'''style msg based on supplied label
|
|
|
|
|
|
|
|
Like ui.write(), this just returns msg unchanged, but extensions
|
|
|
|
and GUI tools can override it to allow styling output without
|
|
|
|
writing it.
|
|
|
|
|
|
|
|
ui.write(s, 'label') is equivalent to
|
|
|
|
ui.write(ui.label(s, 'label')).
|
|
|
|
'''
|
|
|
|
return msg
|