Skip to content

Commit dbd3069

Browse files
committed
change detailed_log to allow for renamed files
I also changed the test to reflect that detailed_log not longer uses `--stat` and added a test case of a renamed file.
1 parent a4d0d7e commit dbd3069

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

jupyterlab_git/git.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -322,58 +322,55 @@ async def log(self, current_path, history_count=10):
322322

323323
async def detailed_log(self, selected_hash, current_path):
324324
"""
325-
Execute git log -1 --stat --numstat --oneline -z command (used to get
325+
Execute git log -1 --numstat --oneline -z command (used to get
326326
insertions & deletions per file) & return the result.
327327
"""
328-
cmd = ["git", "log", "-1", "--stat", "--numstat", "--oneline", "-z", selected_hash]
328+
cmd = ["git", "log", "-1", "--numstat", "--oneline", "-z", selected_hash]
329329
code, my_output, my_error = await execute(
330330
cmd, cwd=os.path.join(self.root_dir, current_path),
331331
)
332332

333333
if code != 0:
334334
return {"code": code, "command": " ".join(cmd), "message": my_error}
335335

336+
total_insertions = 0
337+
total_deletions = 0
336338
result = []
337-
note = [0] * 3
338-
count = 0
339-
temp = ""
340-
line_array = re.split("\x00|\n|\r\n|\r", my_output)
341-
length = len(line_array)
342-
INSERTION_INDEX = 0
343-
DELETION_INDEX = 1
344-
MODIFIED_FILE_PATH_INDEX = 2
345-
if length > 1:
346-
temp = line_array[length - 1]
347-
words = temp.split()
348-
for i in range(0, len(words)):
349-
if words[i].isdigit():
350-
note[count] = words[i]
351-
count += 1
352-
for num in range(1, int(length / 2)):
353-
line_info = line_array[num].split('\t', maxsplit=2)
354-
words = line_info[2].split("/")
355-
length = len(words)
356-
result.append(
357-
{
358-
"modified_file_path": line_info[MODIFIED_FILE_PATH_INDEX],
359-
"modified_file_name": words[length - 1],
360-
"insertion": line_info[INSERTION_INDEX],
361-
"deletion": line_info[DELETION_INDEX],
362-
}
363-
)
339+
line_iterable = iter(self.strip_and_split(my_output)[1:])
340+
for line in line_iterable:
341+
insertions, deletions, file = line.split('\t')
364342

365-
if note[2] == 0 and length > 1:
366-
if "-" in temp:
367-
exchange = note[1]
368-
note[1] = note[2]
369-
note[2] = exchange
343+
if file == '':
344+
# file was renamed or moved
345+
# we need next two lines of output
346+
from_path = next(line_iterable)
347+
to_path = next(line_iterable)
348+
modified_file_name = from_path + " ⮕ " + to_path
349+
modified_file_path = to_path
350+
else:
351+
modified_file_name = file.split("/")[-1]
352+
modified_file_path = file
353+
354+
result.append({
355+
"modified_file_path": modified_file_path,
356+
"modified_file_name": modified_file_name,
357+
"insertion": insertions,
358+
"deletion": deletions,
359+
})
360+
total_insertions += int(insertions)
361+
total_deletions += int(deletions)
362+
363+
modified_file_note = "{num_files} files changed, {insertions} insertions(+), {deletions} deletions(-)".format(
364+
num_files=len(result),
365+
insertions=total_insertions,
366+
deletions=total_deletions)
370367

371368
return {
372369
"code": code,
373-
"modified_file_note": temp,
374-
"modified_files_count": note[0],
375-
"number_of_insertions": note[1],
376-
"number_of_deletions": note[2],
370+
"modified_file_note": modified_file_note,
371+
"modified_files_count": str(len(result)),
372+
"number_of_insertions": str(total_insertions),
373+
"number_of_deletions": str(total_deletions),
377374
"modified_files": result,
378375
}
379376

jupyterlab_git/tests/test_detailed_log.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,28 @@
1515
async def test_detailed_log():
1616
with patch("jupyterlab_git.git.execute") as mock_execute:
1717
# Given
18-
process_output_first_half = [
18+
process_output = [
1919
"f29660a (HEAD, origin/feature) Commit message",
2020
"10\t3\tnotebook_without_spaces.ipynb",
2121
"11\t4\tNotebook with spaces.ipynb",
2222
"12\t5\tpath/notebook_without_spaces.ipynb",
2323
"13\t6\tpath/Notebook with spaces.ipynb",
2424
"14\t1\tpath/Notebook with λ.ipynb",
25+
"0\t0\t",
26+
"folder1/file with spaces and λ.py",
27+
"folder2/file with spaces.py"
2528
]
26-
process_output_second_half = [
27-
" notebook_without_spaces.ipynb | 13 ++++++++---",
28-
" Notebook with spaces.ipynb | 15 +++++++++----",
29-
" path/notebook_without_spaces.ipynb | 17 ++++++++++-----",
30-
" path/Notebook with spaces.ipynb | 19 +++++++++++------",
31-
" path/Notebook with \\316\\273.ipynb | 15 +++++++++++-",
32-
" 5 files changed, 50 insertions(+), 19 deletions(-)",
33-
]
34-
process_output_first_half = "\x00".join(process_output_first_half)
35-
process_output_second_half = "\n".join(process_output_second_half)
36-
process_output = process_output_first_half + "\x00" + process_output_second_half
29+
30+
3731
mock_execute._mock_return_value = tornado.gen.maybe_future(
38-
(0, process_output, "")
32+
(0, "\x00".join(process_output), "")
3933
)
4034

4135
expected_response = {
4236
"code": 0,
43-
"modified_file_note": " 5 files changed, 50 insertions(+), 19 deletions(-)",
44-
"modified_files_count": "5",
45-
"number_of_insertions": "50",
37+
"modified_file_note": "6 files changed, 60 insertions(+), 19 deletions(-)",
38+
"modified_files_count": "6",
39+
"number_of_insertions": "60",
4640
"number_of_deletions": "19",
4741
"modified_files": [
4842
{
@@ -75,11 +69,17 @@ async def test_detailed_log():
7569
"insertion": "14",
7670
"deletion": "1",
7771
},
72+
{
73+
"modified_file_path": "folder2/file with spaces.py",
74+
"modified_file_name": "folder1/file with spaces and λ.py ⮕ folder2/file with spaces.py",
75+
"insertion": "0",
76+
"deletion": "0",
77+
},
7878
],
7979
}
8080

8181
# When
82-
actual_response = (await
82+
actual_response = (await
8383
Git(FakeContentManager("/bin"))
8484
.detailed_log(
8585
selected_hash="f29660a2472e24164906af8653babeb48e4bf2ab",
@@ -93,7 +93,6 @@ async def test_detailed_log():
9393
"git",
9494
"log",
9595
"-1",
96-
"--stat",
9796
"--numstat",
9897
"--oneline",
9998
"-z",

0 commit comments

Comments
 (0)