# HG changeset patch # User Vadim Gelfer # Date 1142401246 28800 # Node ID 696230e52e4dc9bb2b78e59c60b6c41027e5d974 # Parent 5f581f337b05b505353c13a0f15b9dc307826aa6 add HGRCPATH env var, list of places to look for hgrc files. if set, override default hgrc search path. if empty, only .hg/hgrc of current repo read. for each element, if directory, all entries in directory with end in ".rc" are added to path. else, element is added to path. big thing about this change is that user "~/.hgrc" and system hgrc not longer breaks tests. run-tests makes HGRCPATH empty now. diff --git a/doc/hg.1.txt b/doc/hg.1.txt --- a/doc/hg.1.txt +++ b/doc/hg.1.txt @@ -146,6 +146,16 @@ HGMERGE:: (deprecated, use .hgrc) +HGRCPATH:: + A list of files or directories to search for hgrc files. Item + separator is ":" on Unix, ";" on Windows. If HGRCPATH is not set, + platform default search path is used. If empty, only .hg/hgrc of + current repository is read. + + For each element in path, if a directory, all entries in directory + ending with ".rc" are added to path. Else, element itself is + added to path. + HGUSER:: This is the string used for the author of a commit. diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -18,7 +18,7 @@ class ui(object): # this is the parent of all ui children self.parentui = None self.cdata = ConfigParser.SafeConfigParser() - self.readconfig(util.rcpath) + self.readconfig(util.rcpath()) self.quiet = self.configbool("ui", "quiet") self.verbose = self.configbool("ui", "verbose") diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -506,17 +506,18 @@ if os.name == 'nt': sys.stdout = winstdout(sys.stdout) - try: - import win32api, win32process - filename = win32process.GetModuleFileNameEx(win32api.GetCurrentProcess(), 0) - systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini') + def os_rcpath(): + '''return default os-specific hgrc search path''' + try: + import win32api, win32process + proc = win32api.GetCurrentProcess() + filename = win32process.GetModuleFileNameEx(proc, 0) + systemrc = os.path.join(os.path.dirname(filename), 'mercurial.ini') + except ImportError: + systemrc = r'c:\mercurial\mercurial.ini' - except ImportError: - systemrc = r'c:\mercurial\mercurial.ini' - pass - - rcpath = (systemrc, - os.path.join(os.path.expanduser('~'), 'mercurial.ini')) + return [systemrc, + os.path.join(os.path.expanduser('~'), 'mercurial.ini')] def parse_patch_output(output_line): """parses the output produced by patch and returns the file name""" @@ -591,12 +592,17 @@ else: if f.endswith(".rc")]) except OSError, inst: pass return rcs - rcpath = [] - if len(sys.argv) > 0: - rcpath.extend(rcfiles(os.path.dirname(sys.argv[0]) + '/../etc/mercurial')) - rcpath.extend(rcfiles('/etc/mercurial')) - rcpath.append(os.path.expanduser('~/.hgrc')) - rcpath = [os.path.normpath(f) for f in rcpath] + + def os_rcpath(): + '''return default os-specific hgrc search path''' + path = [] + if len(sys.argv) > 0: + path.extend(rcfiles(os.path.dirname(sys.argv[0]) + + '/../etc/mercurial')) + path.extend(rcfiles('/etc/mercurial')) + path.append(os.path.expanduser('~/.hgrc')) + path = [os.path.normpath(f) for f in path] + return path def parse_patch_output(output_line): """parses the output produced by patch and returns the file name""" @@ -768,3 +774,29 @@ def walkrepos(path): yield root dirs[:] = [] break + +_rcpath = None + +def rcpath(): + '''return hgrc search path. if env var HGRCPATH is set, use it. + for each item in path, if directory, use files ending in .rc, + else use item. + make HGRCPATH empty to only look in .hg/hgrc of current repo. + if no HGRCPATH, use default os-specific path.''' + global _rcpath + if _rcpath is None: + if 'HGRCPATH' in os.environ: + _rcpath = [] + for p in os.environ['HGRCPATH'].split(os.pathsep): + if not p: continue + try: + for f in os.listdir(p): + if f.endswith('.rc'): + _rcpath.append(os.path.join(p, f)) + continue + except: + pass + _rcpath.append(p) + else: + _rcpath = os_rcpath() + return _rcpath diff --git a/tests/run-tests b/tests/run-tests --- a/tests/run-tests +++ b/tests/run-tests @@ -18,6 +18,7 @@ TZ=GMT; export TZ HGEDITOR=true; export HGEDITOR HGMERGE=true; export HGMERGE HGUSER="test"; export HGUSER +HGRCPATH=""; export HGRCPATH ECHO_N="echo -n" [ -x /usr/ucb/echo ] && ECHO_N="/usr/ucb/echo -n"