Skip to content

Commit 36a4b50

Browse files
committed
respond to review
Incorporate suggestions by alex and frederic to move strip_and_split and simplify the status method.
1 parent dbd3069 commit 36a4b50

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

jupyterlab_git/git.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def execute(
2525
password: "Optional[str]" = None,
2626
) -> "Tuple[int, str, str]":
2727
"""Asynchronously execute a command.
28-
28+
2929
Args:
3030
cmdline (List[str]): Command line to be executed
3131
cwd (Optional[str]): Current working directory
@@ -101,6 +101,12 @@ def call_subprocess(
101101

102102
return code, output, error
103103

104+
def strip_and_split(s):
105+
"""strip trailing \x00 and split on \x00
106+
107+
Useful for parsing output of git commands with -z flag.
108+
"""
109+
return s.strip("\x00").split("\x00")
104110

105111
class Git:
106112
"""
@@ -110,17 +116,10 @@ class Git:
110116
def __init__(self, contents_manager):
111117
self.contents_manager = contents_manager
112118
self.root_dir = os.path.expanduser(contents_manager.root_dir)
113-
114-
def strip_and_split(self, s):
115-
"""strip trailing \x00 and split on \x00
116-
117-
Useful for parsing output of git commands with -z flag.
118-
"""
119-
return s.strip("\x00").split("\x00")
120119

121120
async def config(self, top_repo_path, **kwargs):
122121
"""Get or set Git options.
123-
122+
124123
If no kwargs, all options are returned. Otherwise kwargs are set.
125124
"""
126125
response = {"code": 1}
@@ -161,12 +160,12 @@ async def changed_files(self, base=None, remote=None, single_commit=None):
161160
There are two reserved "refs" for the base
162161
1. WORKING : Represents the Git working tree
163162
2. INDEX: Represents the Git staging area / index
164-
163+
165164
Keyword Arguments:
166165
single_commit {string} -- The single commit ref
167166
base {string} -- the base Git ref
168167
remote {string} -- the remote Git ref
169-
168+
170169
Returns:
171170
dict -- the response of format {
172171
"code": int, # Command status code
@@ -200,7 +199,7 @@ async def changed_files(self, base=None, remote=None, single_commit=None):
200199
response["command"] = " ".join(cmd)
201200
response["message"] = error
202201
else:
203-
response["files"] = self.strip_and_split(output)
202+
response["files"] = strip_and_split(output)
204203

205204
return response
206205

@@ -256,23 +255,15 @@ async def status(self, current_path):
256255
}
257256

258257
result = []
259-
line_iterable = iter(self.strip_and_split(my_output))
260-
result = []
258+
line_iterable = iter(strip_and_split(my_output))
261259
for line in line_iterable:
262-
x = line[0]
263-
y = line[1]
260+
from_path = line[3:]
264261
if line[0]=='R':
265262
#If file was renamed then we need both this line
266263
#and the next line, then we want to move onto the subsequent
267264
#line. We can accomplish this by calling next on the iterable
268-
to = line[3:]
269265
from_path = next(line_iterable)
270-
else:
271-
#to and from_path are the same
272-
from_path = line[3:]
273-
to = line[3:]
274-
result.append({"x": x, "y": y, "to": to, "from": from_path})
275-
266+
result.append({"x": line[0], "y": line[1], "to": line[3:], "from": from_path})
276267

277268
return {"code": code, "files": result}
278269

@@ -336,7 +327,7 @@ async def detailed_log(self, selected_hash, current_path):
336327
total_insertions = 0
337328
total_deletions = 0
338329
result = []
339-
line_iterable = iter(self.strip_and_split(my_output)[1:])
330+
line_iterable = iter(strip_and_split(my_output)[1:])
340331
for line in line_iterable:
341332
insertions, deletions, file = line.split('\t')
342333

0 commit comments

Comments
 (0)