##// END OF EJS Templates
windows: continue looking at `%HOME%` for user config files with py3.8+...
Matt Harbison -
r46709:224af780 default
parent child Browse files
Show More
@@ -1,81 +1,113 b''
1 from __future__ import absolute_import
1 from __future__ import absolute_import
2
2
3 import os
3 import os
4
4
5 from . import (
5 from . import (
6 encoding,
6 encoding,
7 pycompat,
7 pycompat,
8 util,
8 util,
9 win32,
9 win32,
10 )
10 )
11
11
12 try:
12 try:
13 import _winreg as winreg # pytype: disable=import-error
13 import _winreg as winreg # pytype: disable=import-error
14
14
15 winreg.CloseKey
15 winreg.CloseKey
16 except ImportError:
16 except ImportError:
17 # py2 only
17 # py2 only
18 import winreg # pytype: disable=import-error
18 import winreg # pytype: disable=import-error
19
19
20 # MS-DOS 'more' is the only pager available by default on Windows.
20 # MS-DOS 'more' is the only pager available by default on Windows.
21 fallbackpager = b'more'
21 fallbackpager = b'more'
22
22
23
23
24 def systemrcpath():
24 def systemrcpath():
25 '''return default os-specific hgrc search path'''
25 '''return default os-specific hgrc search path'''
26 rcpath = []
26 rcpath = []
27 filename = win32.executablepath()
27 filename = win32.executablepath()
28 # Use mercurial.ini found in directory with hg.exe
28 # Use mercurial.ini found in directory with hg.exe
29 progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini')
29 progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini')
30 rcpath.append(progrc)
30 rcpath.append(progrc)
31
31
32 def _processdir(progrcd):
32 def _processdir(progrcd):
33 if os.path.isdir(progrcd):
33 if os.path.isdir(progrcd):
34 for f, kind in sorted(util.listdir(progrcd)):
34 for f, kind in sorted(util.listdir(progrcd)):
35 if f.endswith(b'.rc'):
35 if f.endswith(b'.rc'):
36 rcpath.append(os.path.join(progrcd, f))
36 rcpath.append(os.path.join(progrcd, f))
37
37
38 # Use hgrc.d found in directory with hg.exe
38 # Use hgrc.d found in directory with hg.exe
39 _processdir(os.path.join(os.path.dirname(filename), b'hgrc.d'))
39 _processdir(os.path.join(os.path.dirname(filename), b'hgrc.d'))
40
40
41 # treat a PROGRAMDATA directory as equivalent to /etc/mercurial
41 # treat a PROGRAMDATA directory as equivalent to /etc/mercurial
42 programdata = encoding.environ.get(b'PROGRAMDATA')
42 programdata = encoding.environ.get(b'PROGRAMDATA')
43 if programdata:
43 if programdata:
44 programdata = os.path.join(programdata, b'Mercurial')
44 programdata = os.path.join(programdata, b'Mercurial')
45 _processdir(os.path.join(programdata, b'hgrc.d'))
45 _processdir(os.path.join(programdata, b'hgrc.d'))
46
46
47 ini = os.path.join(programdata, b'mercurial.ini')
47 ini = os.path.join(programdata, b'mercurial.ini')
48 if os.path.isfile(ini):
48 if os.path.isfile(ini):
49 rcpath.append(ini)
49 rcpath.append(ini)
50
50
51 ini = os.path.join(programdata, b'hgrc')
51 ini = os.path.join(programdata, b'hgrc')
52 if os.path.isfile(ini):
52 if os.path.isfile(ini):
53 rcpath.append(ini)
53 rcpath.append(ini)
54
54
55 # next look for a system rcpath in the registry
55 # next look for a system rcpath in the registry
56 value = util.lookupreg(
56 value = util.lookupreg(
57 b'SOFTWARE\\Mercurial', None, winreg.HKEY_LOCAL_MACHINE
57 b'SOFTWARE\\Mercurial', None, winreg.HKEY_LOCAL_MACHINE
58 )
58 )
59 if value and isinstance(value, bytes):
59 if value and isinstance(value, bytes):
60 value = util.localpath(value)
60 value = util.localpath(value)
61 for p in value.split(pycompat.ospathsep):
61 for p in value.split(pycompat.ospathsep):
62 if p.lower().endswith(b'mercurial.ini'):
62 if p.lower().endswith(b'mercurial.ini'):
63 rcpath.append(p)
63 rcpath.append(p)
64 else:
64 else:
65 _processdir(p)
65 _processdir(p)
66 return rcpath
66 return rcpath
67
67
68
68
69 def userrcpath():
69 def userrcpath():
70 '''return os-specific hgrc search path to the user dir'''
70 '''return os-specific hgrc search path to the user dir'''
71 home = os.path.expanduser(b'~')
71 home = _legacy_expanduser(b'~')
72 path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')]
72 path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')]
73 userprofile = encoding.environ.get(b'USERPROFILE')
73 userprofile = encoding.environ.get(b'USERPROFILE')
74 if userprofile and userprofile != home:
74 if userprofile and userprofile != home:
75 path.append(os.path.join(userprofile, b'mercurial.ini'))
75 path.append(os.path.join(userprofile, b'mercurial.ini'))
76 path.append(os.path.join(userprofile, b'.hgrc'))
76 path.append(os.path.join(userprofile, b'.hgrc'))
77 return path
77 return path
78
78
79
79
80 def _legacy_expanduser(path):
81 """Expand ~ and ~user constructs in the pre 3.8 style"""
82
83 # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See
84 # https://bugs.python.org/issue36264. It also seems to capitalize the drive
85 # letter, as though it was processed through os.path.realpath().
86 if not path.startswith(b'~'):
87 return path
88
89 i, n = 1, len(path)
90 while i < n and path[i] not in b'\\/':
91 i += 1
92
93 if b'HOME' in encoding.environ:
94 userhome = encoding.environ[b'HOME']
95 elif b'USERPROFILE' in encoding.environ:
96 userhome = encoding.environ[b'USERPROFILE']
97 elif b'HOMEPATH' not in encoding.environ:
98 return path
99 else:
100 try:
101 drive = encoding.environ[b'HOMEDRIVE']
102 except KeyError:
103 drive = b''
104 userhome = os.path.join(drive, encoding.environ[b'HOMEPATH'])
105
106 if i != 1: # ~user
107 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
108
109 return userhome + path[i:]
110
111
80 def termsize(ui):
112 def termsize(ui):
81 return win32.termsize()
113 return win32.termsize()
General Comments 0
You need to be logged in to leave comments. Login now