[PATCH 01 of 10 V4] rcutil: move scmutil.*rcpath to rcutil (API)

Jun Wu quark at fb.com
Mon Mar 27 04:49:14 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1490584722 25200
#      Sun Mar 26 20:18:42 2017 -0700
# Node ID dcef7872ff35187d679a79dc6ad62cb9029923b1
# Parent  e86eb75e74ce1b0803c26d86a229b9b711f6d76a
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r dcef7872ff35
rcutil: move scmutil.*rcpath to rcutil (API)

As discussed at [1], the logic around "actual config"s seem to be
non-trivial enough that it's worth a new module.

This patch creates the module and move "scmutil.*rcpath" functions there as
the first step. More methods will be moved to the module in the future.

The module is different from config.py because the latter only cares about
data structure and parsing, and does not care about special case, or system
config paths, or environment variables.

[1]: https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-March/095503.html

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -44,4 +44,5 @@ from . import (
     phases,
     pycompat,
+    rcutil,
     revsetlang,
     scmutil,
@@ -1776,7 +1777,7 @@ def config(ui, repo, *values, **opts):
             paths = [repo.vfs.join('hgrc')]
         elif opts.get('global'):
-            paths = scmutil.systemrcpath()
+            paths = rcutil.systemrcpath()
         else:
-            paths = scmutil.userrcpath()
+            paths = rcutil.userrcpath()
 
         for f in paths:
@@ -1803,5 +1804,5 @@ def config(ui, repo, *values, **opts):
     ui.pager('config')
     fm = ui.formatter('config', opts)
-    for f in scmutil.rcpath():
+    for f in rcutil.rcpath():
         ui.debug('read config from: %s\n' % f)
     untrusted = bool(opts.get('untrusted'))
diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
new file mode 100644
--- /dev/null
+++ b/mercurial/rcutil.py
@@ -0,0 +1,64 @@
+# rcutil.py - utilities about config paths, special config sections etc.
+#
+#  Copyright Mercurial Contributors
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import os
+
+from . import (
+    encoding,
+    osutil,
+    pycompat,
+    util,
+)
+
+if pycompat.osname == 'nt':
+    from . import scmwindows as scmplatform
+else:
+    from . import scmposix as scmplatform
+
+systemrcpath = scmplatform.systemrcpath
+userrcpath = scmplatform.userrcpath
+
+def osrcpath():
+    '''return default os-specific hgrc search path'''
+    path = []
+    defaultpath = os.path.join(util.datapath, 'default.d')
+    if os.path.isdir(defaultpath):
+        for f, kind in osutil.listdir(defaultpath):
+            if f.endswith('.rc'):
+                path.append(os.path.join(defaultpath, f))
+    path.extend(systemrcpath())
+    path.extend(userrcpath())
+    path = [os.path.normpath(f) for f in path]
+    return path
+
+_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 encoding.environ:
+            _rcpath = []
+            for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
+                if not p:
+                    continue
+                p = util.expandpath(p)
+                if os.path.isdir(p):
+                    for f, kind in osutil.listdir(p):
+                        if f.endswith('.rc'):
+                            _rcpath.append(os.path.join(p, f))
+                else:
+                    _rcpath.append(p)
+        else:
+            _rcpath = osrcpath()
+    return _rcpath
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -21,5 +21,4 @@ from . import (
     error,
     match as matchmod,
-    osutil,
     pathutil,
     phases,
@@ -36,6 +35,4 @@ else:
     from . import scmposix as scmplatform
 
-systemrcpath = scmplatform.systemrcpath
-userrcpath = scmplatform.userrcpath
 termsize = scmplatform.termsize
 
@@ -392,43 +389,4 @@ def walkrepos(path, followsym=False, see
             dirs[:] = newdirs
 
-def osrcpath():
-    '''return default os-specific hgrc search path'''
-    path = []
-    defaultpath = os.path.join(util.datapath, 'default.d')
-    if os.path.isdir(defaultpath):
-        for f, kind in osutil.listdir(defaultpath):
-            if f.endswith('.rc'):
-                path.append(os.path.join(defaultpath, f))
-    path.extend(systemrcpath())
-    path.extend(userrcpath())
-    path = [os.path.normpath(f) for f in path]
-    return path
-
-_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 encoding.environ:
-            _rcpath = []
-            for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
-                if not p:
-                    continue
-                p = util.expandpath(p)
-                if os.path.isdir(p):
-                    for f, kind in osutil.listdir(p):
-                        if f.endswith('.rc'):
-                            _rcpath.append(os.path.join(p, f))
-                else:
-                    _rcpath.append(p)
-        else:
-            _rcpath = osrcpath()
-    return _rcpath
-
 def intrev(rev):
     """Return integer for a given revision that can be used in comparison or
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -34,4 +34,5 @@ from . import (
     progress,
     pycompat,
+    rcutil,
     scmutil,
     util,
@@ -212,5 +213,5 @@ class ui(object):
         u = cls()
         # we always trust global config files
-        for f in scmutil.rcpath():
+        for f in rcutil.rcpath():
             u.readconfig(f, trust=True)
         return u


More information about the Mercurial-devel mailing list