diff --git a/scm.py b/scm.py index 25410d165..837740c94 100644 --- a/scm.py +++ b/scm.py @@ -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 diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 1f07a549c..6f4d34023 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -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',