comparison mercurial/scmwindows.py @ 46093:224af78021de

windows: continue looking at `%HOME%` for user config files with py3.8+ The `%HOME%` variable is explicitly called out in `hg help config` as a location that is consulted when reading user files, but python stopped looking at it when expanding '~' in py3.8+.[1] Restore that old functionality by copying in the old implementation (and simplifying it to just use bytes). It could be simplfied further, since only '~' is passed, but I'm not sure yet if we need to make this a generic utility function on Windows. There are other uses of `os.path.expanduser()`, but this is the only case I know of that documents `%HOME%` usage. (The reason for removing it was that it typically isn't set, but it actually is set in MSYS and PowerShell, and `%HOME%` and `%USERPROFILE%` are different in MSYS. I could be convinced to just replace all uses with this as a general utility, so we don't have to think too hard about BC.) [1] https://bugs.python.org/issue36264 Differential Revision: https://phab.mercurial-scm.org/D9559
author Matt Harbison <matt_harbison@yahoo.com>
date Wed, 09 Dec 2020 18:21:16 -0500
parents 9ac96b9fa76e
children 6000f5b25c9b
comparison
equal deleted inserted replaced
46092:08fd76a553c9 46093:224af78021de
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()