comparison mercurial/encoding.py @ 47621:d6ee6456bd5f

windows: enforce upper case drive letter for getcwd in mercurial too This is affecting code that checks if a pull/push destination is the same as a configured one. For example the one creating divergent bookmark. Doing this fixes will help fixing `test-bookmarks.t` and `test-bookflow.t` on Windows. However, we also need to fix `abspath` invocation. Differential Revision: https://phab.mercurial-scm.org/D11058
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Sat, 10 Jul 2021 13:10:18 +0200
parents af633293a5bd
children 28c62f83b652
comparison
equal deleted inserted replaced
47620:724066f23e2d 47621:d6ee6456bd5f
7 7
8 from __future__ import absolute_import, print_function 8 from __future__ import absolute_import, print_function
9 9
10 import locale 10 import locale
11 import os 11 import os
12 import re
12 import unicodedata 13 import unicodedata
13 14
14 from .pycompat import getattr 15 from .pycompat import getattr
15 from . import ( 16 from . import (
16 error, 17 error,
350 351
351 for k, v in os.environ.items(): # re-exports 352 for k, v in os.environ.items(): # re-exports
352 environ[tolocal(k.encode('utf-8'))] = tolocal(v.encode('utf-8')) 353 environ[tolocal(k.encode('utf-8'))] = tolocal(v.encode('utf-8'))
353 354
354 355
356 DRIVE_RE = re.compile(b'^[a-z]:')
357
355 if pycompat.ispy3: 358 if pycompat.ispy3:
356 # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which 359 # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
357 # returns bytes. 360 # returns bytes.
358 if pycompat.iswindows: 361 if pycompat.iswindows:
359 # Python 3 on Windows issues a DeprecationWarning about using the bytes 362 # Python 3 on Windows issues a DeprecationWarning about using the bytes
361 # 364 #
362 # Additionally, py3.8+ uppercases the drive letter when calling 365 # Additionally, py3.8+ uppercases the drive letter when calling
363 # os.path.realpath(), which is used on ``repo.root``. Since those 366 # os.path.realpath(), which is used on ``repo.root``. Since those
364 # strings are compared in various places as simple strings, also call 367 # strings are compared in various places as simple strings, also call
365 # realpath here. See https://bugs.python.org/issue40368 368 # realpath here. See https://bugs.python.org/issue40368
366 getcwd = lambda: strtolocal(os.path.realpath(os.getcwd())) # re-exports 369 #
370 # However this is not reliable, so lets explicitly make this drive
371 # letter upper case.
372 #
373 # note: we should consider dropping realpath here since it seems to
374 # change the semantic of `getcwd`.
375
376 def getcwd():
377 cwd = os.getcwd() # re-exports
378 cwd = os.path.realpath(cwd)
379 cwd = strtolocal(cwd)
380 if DRIVE_RE.match(cwd):
381 cwd = cwd[0:1].upper() + cwd[1:]
382 return cwd
383
367 else: 384 else:
368 getcwd = os.getcwdb # re-exports 385 getcwd = os.getcwdb # re-exports
369 else: 386 else:
370 getcwd = os.getcwd # re-exports 387 getcwd = os.getcwd # re-exports
371 388