[scm] Fix _load_config to support multi-line

Bug: 1501984, 324449654
Change-Id: Ibe64317e92731735f4ab23c8491eb42e960f9432
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5279207
Auto-Submit: Aravind Vasudevan <aravindvasudev@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Aravind Vasudevan <aravindvasudev@google.com>
changes/07/5279207/5
Aravind Vasudevan 1 year ago committed by LUCI CQ
parent 4d2864f3a1
commit 05048d2cb0

@ -121,15 +121,17 @@ class GIT(object):
"""
if cwd not in GIT._CONFIG_CACHE:
try:
rawConfig = GIT.Capture(['config', '--list'],
rawConfig = GIT.Capture(['config', '--list', '-z'],
cwd=cwd,
strip_out=False)
except subprocess2.CalledProcessError:
return {}
cfg = defaultdict(list)
for line in rawConfig.splitlines():
key, value = map(str.strip, line.split('=', 1))
# Splitting by '\x00' gets an additional empty string at the end.
for line in rawConfig.split('\x00')[:-1]:
key, value = map(str.strip, line.split('\n', 1))
cfg[key].append(value)
GIT._CONFIG_CACHE[cwd] = cfg

@ -15,6 +15,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from testing_support import fake_repos
import scm
import subprocess
import subprocess2
@ -29,9 +30,9 @@ class GitWrapperTestCase(unittest.TestCase):
@mock.patch('scm.GIT.Capture')
def testGetEmail(self, mockCapture):
mockCapture.return_value = 'user.email = mini@me.com'
mockCapture.return_value = 'user.email\nmini@me.com\x00'
self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
mockCapture.assert_called_with(['config', '--list'],
mockCapture.assert_called_with(['config', '--list', '-z'],
cwd=self.root_dir,
strip_out=False)
@ -207,6 +208,14 @@ class RealGitTest(fake_repos.FakeReposTestBase):
self.assertEqual('set-value',
scm.GIT.GetConfig(self.cwd, key, 'default-value'))
scm.GIT._clear_config(self.cwd)
subprocess.run(['git', 'config', key, 'line 1\nline 2\nline 3'],
cwd=self.cwd)
self.assertEqual('line 1\nline 2\nline 3',
scm.GIT.GetConfig(self.cwd, key))
self.assertEqual('line 1\nline 2\nline 3',
scm.GIT.GetConfig(self.cwd, key, 'default-value'))
scm.GIT.SetConfig(self.cwd, key)
self.assertIsNone(scm.GIT.GetConfig(self.cwd, key))
self.assertEqual('default-value',

Loading…
Cancel
Save