diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py --- a/mercurial/dispatch.py +++ b/mercurial/dispatch.py @@ -952,14 +952,22 @@ def _getlocal(ui, rpath, wd=None): Takes paths in [cwd]/.hg/hgrc into account." """ + try: + cwd = encoding.getcwd() + except OSError as e: + raise error.Abort( + _(b"error getting current working directory: %s") + % encoding.strtolocal(e.strerror) + ) + + # If using an alternate wd, temporarily switch to it so that relative + # paths are resolved correctly during config loading. + oldcwd = None if wd is None: - try: - wd = encoding.getcwd() - except OSError as e: - raise error.Abort( - _(b"error getting current working directory: %s") - % encoding.strtolocal(e.strerror) - ) + wd = cwd + else: + oldcwd = cwd + os.chdir(wd) path = cmdutil.findrepo(wd) or b"" if not path: @@ -979,6 +987,9 @@ def _getlocal(ui, rpath, wd=None): lui.readconfig(os.path.join(path, b".hg", b"hgrc"), path) lui.readconfig(os.path.join(path, b".hg", b"hgrc-not-shared"), path) + if oldcwd: + os.chdir(oldcwd) + return path, lui diff --git a/tests/test-chg.t b/tests/test-chg.t --- a/tests/test-chg.t +++ b/tests/test-chg.t @@ -432,6 +432,20 @@ check server log: YYYY/MM/DD HH:MM:SS (PID)> log -R cached YYYY/MM/DD HH:MM:SS (PID)> loaded repo into cache: $TESTTMP/cached (in ...s) +Test that -R is interpreted relative to --cwd. + + $ hg init repo1 + $ mkdir -p a/b + $ hg init a/b/repo2 + $ printf "[alias]\ntest=repo1\n" >> repo1/.hg/hgrc + $ printf "[alias]\ntest=repo2\n" >> a/b/repo2/.hg/hgrc + $ cd a + $ chg --cwd .. -R repo1 show alias.test + repo1 + $ chg --cwd . -R b/repo2 show alias.test + repo2 + $ cd .. + Test that chg works (sets to the user's actual LC_CTYPE) even when python "coerces" the locale (py3.7+)