##// END OF EJS Templates
rcutil: move scmutil.*rcpath to rcutil (API)...
Jun Wu -
r31679:0f8ba0bc default
parent child Browse files
Show More
@@ -0,0 +1,64 b''
1 # rcutil.py - utilities about config paths, special config sections etc.
2 #
3 # Copyright Mercurial Contributors
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 from __future__ import absolute_import
9
10 import os
11
12 from . import (
13 encoding,
14 osutil,
15 pycompat,
16 util,
17 )
18
19 if pycompat.osname == 'nt':
20 from . import scmwindows as scmplatform
21 else:
22 from . import scmposix as scmplatform
23
24 systemrcpath = scmplatform.systemrcpath
25 userrcpath = scmplatform.userrcpath
26
27 def osrcpath():
28 '''return default os-specific hgrc search path'''
29 path = []
30 defaultpath = os.path.join(util.datapath, 'default.d')
31 if os.path.isdir(defaultpath):
32 for f, kind in osutil.listdir(defaultpath):
33 if f.endswith('.rc'):
34 path.append(os.path.join(defaultpath, f))
35 path.extend(systemrcpath())
36 path.extend(userrcpath())
37 path = [os.path.normpath(f) for f in path]
38 return path
39
40 _rcpath = None
41
42 def rcpath():
43 '''return hgrc search path. if env var HGRCPATH is set, use it.
44 for each item in path, if directory, use files ending in .rc,
45 else use item.
46 make HGRCPATH empty to only look in .hg/hgrc of current repo.
47 if no HGRCPATH, use default os-specific path.'''
48 global _rcpath
49 if _rcpath is None:
50 if 'HGRCPATH' in encoding.environ:
51 _rcpath = []
52 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
53 if not p:
54 continue
55 p = util.expandpath(p)
56 if os.path.isdir(p):
57 for f, kind in osutil.listdir(p):
58 if f.endswith('.rc'):
59 _rcpath.append(os.path.join(p, f))
60 else:
61 _rcpath.append(p)
62 else:
63 _rcpath = osrcpath()
64 return _rcpath
@@ -43,6 +43,7 b' from . import ('
43 patch,
43 patch,
44 phases,
44 phases,
45 pycompat,
45 pycompat,
46 rcutil,
46 revsetlang,
47 revsetlang,
47 scmutil,
48 scmutil,
48 server,
49 server,
@@ -1776,9 +1777,9 b' def config(ui, repo, *values, **opts):'
1776 raise error.Abort(_("can't use --local outside a repository"))
1777 raise error.Abort(_("can't use --local outside a repository"))
1777 paths = [repo.vfs.join('hgrc')]
1778 paths = [repo.vfs.join('hgrc')]
1778 elif opts.get('global'):
1779 elif opts.get('global'):
1779 paths = scmutil.systemrcpath()
1780 paths = rcutil.systemrcpath()
1780 else:
1781 else:
1781 paths = scmutil.userrcpath()
1782 paths = rcutil.userrcpath()
1782
1783
1783 for f in paths:
1784 for f in paths:
1784 if os.path.exists(f):
1785 if os.path.exists(f):
@@ -1803,7 +1804,7 b' def config(ui, repo, *values, **opts):'
1803 return
1804 return
1804 ui.pager('config')
1805 ui.pager('config')
1805 fm = ui.formatter('config', opts)
1806 fm = ui.formatter('config', opts)
1806 for f in scmutil.rcpath():
1807 for f in rcutil.rcpath():
1807 ui.debug('read config from: %s\n' % f)
1808 ui.debug('read config from: %s\n' % f)
1808 untrusted = bool(opts.get('untrusted'))
1809 untrusted = bool(opts.get('untrusted'))
1809 if values:
1810 if values:
@@ -20,7 +20,6 b' from . import ('
20 encoding,
20 encoding,
21 error,
21 error,
22 match as matchmod,
22 match as matchmod,
23 osutil,
24 pathutil,
23 pathutil,
25 phases,
24 phases,
26 pycompat,
25 pycompat,
@@ -35,8 +34,6 b" if pycompat.osname == 'nt':"
35 else:
34 else:
36 from . import scmposix as scmplatform
35 from . import scmposix as scmplatform
37
36
38 systemrcpath = scmplatform.systemrcpath
39 userrcpath = scmplatform.userrcpath
40 termsize = scmplatform.termsize
37 termsize = scmplatform.termsize
41
38
42 class status(tuple):
39 class status(tuple):
@@ -391,45 +388,6 b' def walkrepos(path, followsym=False, see'
391 newdirs.append(d)
388 newdirs.append(d)
392 dirs[:] = newdirs
389 dirs[:] = newdirs
393
390
394 def osrcpath():
395 '''return default os-specific hgrc search path'''
396 path = []
397 defaultpath = os.path.join(util.datapath, 'default.d')
398 if os.path.isdir(defaultpath):
399 for f, kind in osutil.listdir(defaultpath):
400 if f.endswith('.rc'):
401 path.append(os.path.join(defaultpath, f))
402 path.extend(systemrcpath())
403 path.extend(userrcpath())
404 path = [os.path.normpath(f) for f in path]
405 return path
406
407 _rcpath = None
408
409 def rcpath():
410 '''return hgrc search path. if env var HGRCPATH is set, use it.
411 for each item in path, if directory, use files ending in .rc,
412 else use item.
413 make HGRCPATH empty to only look in .hg/hgrc of current repo.
414 if no HGRCPATH, use default os-specific path.'''
415 global _rcpath
416 if _rcpath is None:
417 if 'HGRCPATH' in encoding.environ:
418 _rcpath = []
419 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
420 if not p:
421 continue
422 p = util.expandpath(p)
423 if os.path.isdir(p):
424 for f, kind in osutil.listdir(p):
425 if f.endswith('.rc'):
426 _rcpath.append(os.path.join(p, f))
427 else:
428 _rcpath.append(p)
429 else:
430 _rcpath = osrcpath()
431 return _rcpath
432
433 def intrev(rev):
391 def intrev(rev):
434 """Return integer for a given revision that can be used in comparison or
392 """Return integer for a given revision that can be used in comparison or
435 arithmetic operation"""
393 arithmetic operation"""
@@ -33,6 +33,7 b' from . import ('
33 formatter,
33 formatter,
34 progress,
34 progress,
35 pycompat,
35 pycompat,
36 rcutil,
36 scmutil,
37 scmutil,
37 util,
38 util,
38 )
39 )
@@ -211,7 +212,7 b' class ui(object):'
211 """Create a ui and load global and user configs"""
212 """Create a ui and load global and user configs"""
212 u = cls()
213 u = cls()
213 # we always trust global config files
214 # we always trust global config files
214 for f in scmutil.rcpath():
215 for f in rcutil.rcpath():
215 u.readconfig(f, trust=True)
216 u.readconfig(f, trust=True)
216 return u
217 return u
217
218
General Comments 0
You need to be logged in to leave comments. Login now