Skip to content

Commit e838614

Browse files
committed
Use regex to capture git options
1 parent 88e740d commit e838614

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

jupyterlab_git/git.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
Module for executing git commands, sending results back to the handlers
33
"""
4-
import logging
54
import os
5+
import re
66
import subprocess
77
from subprocess import Popen, PIPE, CalledProcessError
88

@@ -11,10 +11,10 @@
1111
from tornado.web import HTTPError
1212

1313

14-
logger = logging.getLogger(__name__)
15-
16-
14+
# Git configuration options exposed through the REST API
1715
ALLOWED_OPTIONS = ['user.name', 'user.email']
16+
# Regex pattern to capture (key, value) of Git configuration options
17+
CONFIG_PATTERN = re.compile(r"(?:^|\n)(\S+)\=")
1818

1919

2020
class GitAuthInputWrapper:
@@ -106,18 +106,8 @@ def config(self, top_repo_path, **kwargs):
106106
response["message"] = error.decode("utf-8").strip()
107107
else:
108108
raw = output.decode("utf-8").strip()
109-
response["options"] = dict()
110-
previous_key = None
111-
for l in raw.split("\n"):
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))
109+
s = CONFIG_PATTERN.split(raw)
110+
response["options"] = {k:v for k, v in zip(s[1::2], s[2::2]) if k in ALLOWED_OPTIONS}
121111

122112
return response
123113

jupyterlab_git/tests/test_config.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def test_git_get_config_multiline(self, popen):
5959
return_value=(
6060
b"user.name=John Snow\n"
6161
b"user.email=john.snow@iscoming.com\n"
62+
b'alias.summary=!f() { printf "Summary of this branch...\n'
63+
b'"; printf "%s\n'
64+
b'" $(git rev-parse --abbrev-ref HEAD); printf "\n'
65+
b"Most-active files, with churn count\n"
66+
b'"; git churn | head -7; }; f\n'
6267
b'alias.topic-base-branch-name=!f(){ printf "master\n'
6368
b'"; };f\n'
6469
b'alias.topic-start=!f(){ topic_branch="$1"; git topic-create "$topic_branch"; git topic-push; };f',
@@ -94,7 +99,10 @@ def test_git_get_config_multiline(self, popen):
9499
}
95100

96101
@patch("subprocess.Popen")
97-
@patch("jupyterlab_git.git.ALLOWED_OPTIONS", ["user.name", "alias.topic-base-branch-name"])
102+
@patch(
103+
"jupyterlab_git.git.ALLOWED_OPTIONS",
104+
["alias.summary", "alias.topic-base-branch-name"],
105+
)
98106
def test_git_get_config_accepted_multiline(self, popen):
99107
# Given
100108
process_mock = Mock()
@@ -103,6 +111,11 @@ def test_git_get_config_accepted_multiline(self, popen):
103111
return_value=(
104112
b"user.name=John Snow\n"
105113
b"user.email=john.snow@iscoming.com\n"
114+
b'alias.summary=!f() { printf "Summary of this branch...\n'
115+
b'"; printf "%s\n'
116+
b'" $(git rev-parse --abbrev-ref HEAD); printf "\n'
117+
b"Most-active files, with churn count\n"
118+
b'"; git churn | head -7; }; f\n'
106119
b'alias.topic-base-branch-name=!f(){ printf "master\n'
107120
b'"; };f\n'
108121
b'alias.topic-start=!f(){ topic_branch="$1"; git topic-create "$topic_branch"; git topic-push; };f',
@@ -132,8 +145,12 @@ def test_git_get_config_accepted_multiline(self, popen):
132145
assert payload == {
133146
"code": 0,
134147
"options": {
135-
"user.name": "John Snow",
136-
"alias.topic-base-branch-name": '!f(){ printf "master"; };f',
148+
"alias.summary": '!f() { printf "Summary of this branch...\n'
149+
'"; printf "%s\n'
150+
'" $(git rev-parse --abbrev-ref HEAD); printf "\n'
151+
"Most-active files, with churn count\n"
152+
'"; git churn | head -7; }; f',
153+
"alias.topic-base-branch-name": '!f(){ printf "master\n"; };f',
137154
},
138155
}
139156

0 commit comments

Comments
 (0)