From 67db664778a0a11645c4c4bfa66d303f54e18d51 Mon Sep 17 00:00:00 2001 From: Xavier Deguillard Date: Thu, 18 Jun 2020 10:42:59 -0700 Subject: [PATCH] dynamicconfig: read file as bytes Summary: Somehow reading the file with `open("r")` will try to decode it as ascii, but that file may contain utf-8 characters, so open it as bytes, and then decode it. Reviewed By: DurhamG Differential Revision: D22105188 fbshipit-source-id: 0cfbd8c5417b637dd76391f86226e24ee663baf7 --- eden/scm/edenscm/mercurial/uiconfig.py | 8 +++---- eden/scm/tests/test-dynamicconfig-unicode.t | 25 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 eden/scm/tests/test-dynamicconfig-unicode.t diff --git a/eden/scm/edenscm/mercurial/uiconfig.py b/eden/scm/edenscm/mercurial/uiconfig.py index a1164677f8..d82f9f1dad 100644 --- a/eden/scm/edenscm/mercurial/uiconfig.py +++ b/eden/scm/edenscm/mercurial/uiconfig.py @@ -558,16 +558,16 @@ def loaddynamicconfig(ui, path): if ui.configbool("configs", "loaddynamicconfig"): sharedpathfile = os.path.join(path, "sharedpath") if os.path.exists(sharedpathfile): - with open(sharedpathfile, "r") as f: - path = f.read() + with open(sharedpathfile, "rb") as f: + path = pycompat.decodeutf8(f.read()) hgrcdyn = os.path.join(path, "hgrc.dynamic") # Check the version of the existing generated config. If it doesn't # match the current version, regenerate it immediately. try: - with open(hgrcdyn, "r") as f: - content = f.read() + with open(hgrcdyn, "rb") as f: + content = pycompat.decodeutf8(f.read()) matches = re.search("^# version=(.*)$", content, re.MULTILINE) version = matches.group(1) if matches else None except IOError: diff --git a/eden/scm/tests/test-dynamicconfig-unicode.t b/eden/scm/tests/test-dynamicconfig-unicode.t new file mode 100644 index 0000000000..6bf1011ff8 --- /dev/null +++ b/eden/scm/tests/test-dynamicconfig-unicode.t @@ -0,0 +1,25 @@ +#chg-compatible + + $ configure modern + + $ setconfig configs.loaddynamicconfig=True + $ export HG_TEST_DYNAMICCONFIG="$TESTTMP/test_hgrc" + $ cat > test_hgrc < [section] + > key=✓ + > EOF + + $ hg init client + $ cd client + +Verify it can be manually generated + + $ hg debugdynamicconfig + $ cat .hg/hgrc.dynamic + # version=4.4.2* (glob) + # Generated by `hg debugdynamicconfig` - DO NOT MODIFY + [section] + key=✓ + + $ hg config section.key + ✓