changeset 1951:696230e52e4d

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.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Tue, 14 Mar 2006 21:40:46 -0800
parents 5f581f337b05
children f4df34b6987f 379ab45b91b7
files doc/hg.1.txt mercurial/ui.py mercurial/util.py tests/run-tests
diffstat 4 files changed, 60 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/doc/hg.1.txt	Wed Mar 15 03:19:16 2006 +0100
+++ b/doc/hg.1.txt	Tue Mar 14 21:40:46 2006 -0800
@@ -146,6 +146,16 @@
 
     (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.
 
--- a/mercurial/ui.py	Wed Mar 15 03:19:16 2006 +0100
+++ b/mercurial/ui.py	Tue Mar 14 21:40:46 2006 -0800
@@ -18,7 +18,7 @@
             # 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")
--- a/mercurial/util.py	Wed Mar 15 03:19:16 2006 +0100
+++ b/mercurial/util.py	Tue Mar 14 21:40:46 2006 -0800
@@ -506,17 +506,18 @@
 
     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 @@
                         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 @@
                 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
--- a/tests/run-tests	Wed Mar 15 03:19:16 2006 +0100
+++ b/tests/run-tests	Tue Mar 14 21:40:46 2006 -0800
@@ -18,6 +18,7 @@
 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"