changeset 31681:294728f2a908

rcutil: extract rc directory listing logic The logic of listing a ".rc" directory is duplicated in two functions, extract it to a single function to make the code cleaner.
author Jun Wu <quark@fb.com>
date Sun, 26 Mar 2017 20:46:05 -0700
parents 448889f9a36c
children 07d62fa518a4
files mercurial/rcutil.py
diffstat 1 files changed, 10 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/rcutil.py	Sun Mar 26 20:21:32 2017 -0700
+++ b/mercurial/rcutil.py	Sun Mar 26 20:46:05 2017 -0700
@@ -24,14 +24,20 @@
 systemrcpath = scmplatform.systemrcpath
 userrcpath = scmplatform.userrcpath
 
+def _expandrcpath(path):
+    '''path could be a file or a directory. return a list of file paths'''
+    p = util.expandpath(path)
+    if os.path.isdir(p):
+        join = os.path.join
+        return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
+    return [p]
+
 def defaultrcpath():
     '''return rc paths in default.d'''
     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 = _expandrcpath(defaultpath)
     return path
 
 _rcpath = None
@@ -49,13 +55,7 @@
             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)
+                _rcpath.extend(_expandrcpath(p))
         else:
             paths = defaultrcpath() + systemrcpath() + userrcpath()
             _rcpath = pycompat.maplist(os.path.normpath, paths)