Skip to content

Commit 8ef8793

Browse files
committed
fix non-ascii characters with -z
No more using undocumented functions.
1 parent 6f4d215 commit 8ef8793

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

jupyterlab_git/git.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import re
66
import subprocess
77
from urllib.parse import unquote
8-
from codecs import decode, escape_decode
98

109
import pexpect
1110
import tornado
@@ -169,14 +168,14 @@ async def changed_files(self, base=None, remote=None, single_commit=None):
169168
}
170169
"""
171170
if single_commit:
172-
cmd = ["git", "diff", "{}^!".format(single_commit), "--name-only"]
171+
cmd = ["git", "diff", "{}^!".format(single_commit), "--name-only", "-z"]
173172
elif base and remote:
174173
if base == "WORKING":
175-
cmd = ["git", "diff", remote, "--name-only"]
174+
cmd = ["git", "diff", remote, "--name-only", "-z"]
176175
elif base == "INDEX":
177-
cmd = ["git", "diff", "--staged", remote, "--name-only"]
176+
cmd = ["git", "diff", "--staged", remote, "--name-only", "-z"]
178177
else:
179-
cmd = ["git", "diff", base, remote, "--name-only"]
178+
cmd = ["git", "diff", base, remote, "--name-only", "-z"]
180179
else:
181180
raise tornado.web.HTTPError(
182181
400, "Either single_commit or (base and remote) must be provided"
@@ -194,7 +193,7 @@ async def changed_files(self, base=None, remote=None, single_commit=None):
194193
response["command"] = " ".join(cmd)
195194
response["message"] = error
196195
else:
197-
response["files"] = output.strip().split("\n")
196+
response["files"] = output.strip("\x00").split("\x00")
198197

199198
return response
200199

@@ -237,7 +236,7 @@ async def status(self, current_path):
237236
"""
238237
Execute git status command & return the result.
239238
"""
240-
cmd = ["git", "status", "--porcelain", "-u"]
239+
cmd = ["git", "status", "--porcelain", "-u", "-z"]
241240
code, my_output, my_error = await execute(
242241
cmd, cwd=os.path.join(self.root_dir, current_path),
243242
)
@@ -250,22 +249,24 @@ async def status(self, current_path):
250249
}
251250

252251
result = []
253-
line_array = my_output.splitlines()
254-
for line in line_array:
255-
to1 = None
256-
from_path = line[3:]
257-
if line[0] == "R":
258-
to0 = line[3:].split(" -> ")
259-
to1 = to0[len(to0) - 1]
252+
line_iterable = iter(my_output.strip("\x00").split('\x00'))
253+
result = []
254+
for line in line_iterable:
255+
x = line[0]
256+
y = line[1]
257+
if line[0]=='R':
258+
#If file was renamed then we need both this line
259+
#and the next line, then we want to move onto the subsequent
260+
#line. We can accomplish this by calling next on the iterable
261+
to = line[3:]
262+
from_path = next(line_iterable)
260263
else:
261-
to1 = line[3:]
262-
if to1.startswith('"'):
263-
to1 = to1[1:]
264-
if to1.endswith('"'):
265-
to1 = to1[:-1]
266-
to1 = decode(escape_decode(to1)[0],'utf-8')
267-
from_path = decode(escape_decode(from_path)[0],'utf-8')
268-
result.append({"x": line[0], "y": line[1], "to": to1, "from": from_path})
264+
#to and from_path are the same
265+
from_path = line[3:]
266+
to = line[3:]
267+
result.append({"x": x, "y": y, "to": to, "from": from_path})
268+
269+
269270
return {"code": code, "files": result}
270271

271272
async def log(self, current_path, history_count=10):
@@ -317,7 +318,7 @@ async def detailed_log(self, selected_hash, current_path):
317318
Execute git log -1 --stat --numstat --oneline command (used to get
318319
insertions & deletions per file) & return the result.
319320
"""
320-
cmd = ["git", "log", "-1", "--stat", "--numstat", "--oneline", selected_hash]
321+
cmd = ["git", "log", "-1", "--stat", "--numstat", "--oneline", "-z", selected_hash]
321322
code, my_output, my_error = await execute(
322323
cmd, cwd=os.path.join(self.root_dir, current_path),
323324
)
@@ -329,7 +330,7 @@ async def detailed_log(self, selected_hash, current_path):
329330
note = [0] * 3
330331
count = 0
331332
temp = ""
332-
line_array = my_output.splitlines()
333+
line_array = re.split("\x00|\n|\r\n|\r", my_output)
333334
length = len(line_array)
334335
INSERTION_INDEX = 0
335336
DELETION_INDEX = 1
@@ -342,7 +343,7 @@ async def detailed_log(self, selected_hash, current_path):
342343
note[count] = words[i]
343344
count += 1
344345
for num in range(1, int(length / 2)):
345-
line_info = line_array[num].split(maxsplit=2)
346+
line_info = line_array[num].split('\t', maxsplit=2)
346347
words = line_info[2].split("/")
347348
length = len(words)
348349
result.append(
@@ -373,14 +374,14 @@ async def diff(self, top_repo_path):
373374
"""
374375
Execute git diff command & return the result.
375376
"""
376-
cmd = ["git", "diff", "--numstat"]
377+
cmd = ["git", "diff", "--numstat", "-z"]
377378
code, my_output, my_error = await execute(cmd, cwd=top_repo_path)
378379

379380
if code != 0:
380381
return {"code": code, "command": " ".join(cmd), "message": my_error}
381382

382383
result = []
383-
line_array = my_output.splitlines()
384+
line_array = my_output.strip('\x00').split('\x00')
384385
for line in line_array:
385386
linesplit = line.split()
386387
result.append(

0 commit comments

Comments
 (0)