Skip to content

Commit 88e740d

Browse files
committed
Fix #498
1 parent eac6a5d commit 88e740d

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

jupyterlab_git/git.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Module for executing git commands, sending results back to the handlers
33
"""
4+
import logging
45
import os
56
import subprocess
67
from subprocess import Popen, PIPE, CalledProcessError
@@ -10,6 +11,9 @@
1011
from tornado.web import HTTPError
1112

1213

14+
logger = logging.getLogger(__name__)
15+
16+
1317
ALLOWED_OPTIONS = ['user.name', 'user.email']
1418

1519

@@ -103,10 +107,17 @@ def config(self, top_repo_path, **kwargs):
103107
else:
104108
raw = output.decode("utf-8").strip()
105109
response["options"] = dict()
110+
previous_key = None
106111
for l in raw.split("\n"):
107-
k, v = l.split("=", maxsplit=1)
108-
if k in ALLOWED_OPTIONS:
109-
response["options"][k] = v
112+
if l.startswith('"') and previous_key in ALLOWED_OPTIONS:
113+
response["options"][previous_key] += l
114+
elif "=" in l:
115+
key, v = l.split("=", maxsplit=1)
116+
previous_key = key
117+
if key in ALLOWED_OPTIONS:
118+
response["options"][key] = v
119+
else:
120+
logger.debug("[jupyterlab-git] Unable to interpret git option {}.".format(l))
110121

111122
return response
112123

jupyterlab_git/tests/test_config.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,93 @@ def test_git_get_config_success(self, popen):
5050
},
5151
}
5252

53+
@patch("subprocess.Popen")
54+
def test_git_get_config_multiline(self, popen):
55+
# Given
56+
process_mock = Mock()
57+
attrs = {
58+
"communicate": Mock(
59+
return_value=(
60+
b"user.name=John Snow\n"
61+
b"user.email=john.snow@iscoming.com\n"
62+
b'alias.topic-base-branch-name=!f(){ printf "master\n'
63+
b'"; };f\n'
64+
b'alias.topic-start=!f(){ topic_branch="$1"; git topic-create "$topic_branch"; git topic-push; };f',
65+
b"",
66+
)
67+
),
68+
"returncode": 0,
69+
}
70+
process_mock.configure_mock(**attrs)
71+
popen.return_value = process_mock
72+
73+
# When
74+
body = {"path": "test_path"}
75+
response = self.tester.post(["config"], body=body)
76+
77+
# Then
78+
popen.assert_called_once_with(
79+
["git", "config", "--list"],
80+
stdout=subprocess.PIPE,
81+
stderr=subprocess.PIPE,
82+
cwd="test_path",
83+
)
84+
process_mock.communicate.assert_called_once_with()
85+
86+
assert response.status_code == 201
87+
payload = response.json()
88+
assert payload == {
89+
"code": 0,
90+
"options": {
91+
"user.name": "John Snow",
92+
"user.email": "john.snow@iscoming.com",
93+
},
94+
}
95+
96+
@patch("subprocess.Popen")
97+
@patch("jupyterlab_git.git.ALLOWED_OPTIONS", ["user.name", "alias.topic-base-branch-name"])
98+
def test_git_get_config_accepted_multiline(self, popen):
99+
# Given
100+
process_mock = Mock()
101+
attrs = {
102+
"communicate": Mock(
103+
return_value=(
104+
b"user.name=John Snow\n"
105+
b"user.email=john.snow@iscoming.com\n"
106+
b'alias.topic-base-branch-name=!f(){ printf "master\n'
107+
b'"; };f\n'
108+
b'alias.topic-start=!f(){ topic_branch="$1"; git topic-create "$topic_branch"; git topic-push; };f',
109+
b"",
110+
)
111+
),
112+
"returncode": 0,
113+
}
114+
process_mock.configure_mock(**attrs)
115+
popen.return_value = process_mock
116+
117+
# When
118+
body = {"path": "test_path"}
119+
response = self.tester.post(["config"], body=body)
120+
121+
# Then
122+
popen.assert_called_once_with(
123+
["git", "config", "--list"],
124+
stdout=subprocess.PIPE,
125+
stderr=subprocess.PIPE,
126+
cwd="test_path",
127+
)
128+
process_mock.communicate.assert_called_once_with()
129+
130+
assert response.status_code == 201
131+
payload = response.json()
132+
assert payload == {
133+
"code": 0,
134+
"options": {
135+
"user.name": "John Snow",
136+
"alias.topic-base-branch-name": '!f(){ printf "master"; };f',
137+
},
138+
}
139+
53140
@patch("subprocess.Popen")
54141
def test_git_set_config_success(self, popen):
55142
# Given

0 commit comments

Comments
 (0)