[PATCH 05 of 11 V2] scmutil: extract path reading function from rccomponents

Jun Wu quark at fb.com
Wed Mar 22 03:50:54 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1490168676 25200
#      Wed Mar 22 00:44:36 2017 -0700
# Node ID c18d9f3e0dac6d1676bd58617477c64953be96a3
# Parent  75f661b31640a914dd513d42b2ce1389c9b61c0a
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r c18d9f3e0dac
scmutil: extract path reading function from rccomponents

This makes the code better structured. A side effect is "normpath" will be
called on paths in $HGRCPATH, which seems to be more correct.

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -452,4 +452,13 @@ def rcpath():
 _rccomponents = None
 
+def _readrcpath(path):
+    '''path could be a single file or a directory. return a list of paths'''
+    p = util.expandpath(path)
+    join = os.path.join
+    if os.path.isdir(p):
+        return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
+    else:
+        return [p]
+
 def rccomponents():
     '''return an ordered [(type, obj)] about where to load configs.
@@ -465,4 +474,8 @@ def rccomponents():
     '''
     global _rccomponents
+
+    def pathize(path):
+        return ('path', os.path.normpath(path))
+
     if _rccomponents is None:
         if 'HGRCPATH' in encoding.environ:
@@ -473,15 +486,6 @@ def rccomponents():
                 if not p:
                     continue
-                p = util.expandpath(p)
-                if os.path.isdir(p):
-                    for f, kind in osutil.listdir(p):
-                        if f.endswith('.rc'):
-                            _rccomponents.append(('path', os.path.join(p, f)))
-                else:
-                    _rccomponents.append(('path', p))
+                _rccomponents.extend(map(pathize, _readrcpath(p)))
         else:
-            def pathize(path):
-                return ('path', os.path.normpath(path))
-
             _rccomponents = map(pathize, defaultrcpath())
             _rccomponents.extend(map(pathize, systemrcpath()))


More information about the Mercurial-devel mailing list