Skip to content

Commit b4590f8

Browse files
authored
Fix Git history of relocated files (#1039)
* Fix Git history of relocated files (#1038) * Add relocated file commit case * Use previous path when commit relocates file * Fix relocated file commits for repository history - Adds better diff labels
1 parent 10a627e commit b4590f8

File tree

7 files changed

+117
-363
lines changed

7 files changed

+117
-363
lines changed

jupyterlab_git/git.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ async def log(self, path, history_count=10, follow_path=None):
471471
("-%d" % history_count),
472472
]
473473
if is_single_file:
474-
cmd = cmd + [
474+
cmd += [
475+
"-z",
475476
"--numstat",
476477
"--follow",
477478
"--",
@@ -485,13 +486,19 @@ async def log(self, path, history_count=10, follow_path=None):
485486
return {"code": code, "command": " ".join(cmd), "message": my_error}
486487

487488
result = []
488-
if is_single_file:
489-
# an extra newline get outputted when --numstat is used
490-
my_output = my_output.replace("\n\n", "\n")
491489
line_array = my_output.splitlines()
492-
i = 0
490+
491+
if is_single_file:
492+
parsed_lines = []
493+
for line in line_array:
494+
parsed_lines.extend(
495+
re.sub(r"\t\0|\0", "\t", l)
496+
for l in line.strip("\0\t").split("\0\0", maxsplit=1)
497+
)
498+
line_array = parsed_lines
499+
493500
PREVIOUS_COMMIT_OFFSET = 5 if is_single_file else 4
494-
while i < len(line_array):
501+
for i in range(0, len(line_array), PREVIOUS_COMMIT_OFFSET):
495502
commit = {
496503
"commit": line_array[i],
497504
"author": line_array[i + 1],
@@ -503,11 +510,18 @@ async def log(self, path, history_count=10, follow_path=None):
503510
if is_single_file:
504511
commit["is_binary"] = line_array[i + 4].startswith("-\t-\t")
505512

513+
# [insertions, deletions, previous_file_path?, current_file_path]
514+
file_info = line_array[i + 4].split()
515+
516+
if len(file_info) == 4:
517+
commit["previous_file_path"] = file_info[2]
518+
commit["file_path"] = file_info[-1]
519+
506520
if i + PREVIOUS_COMMIT_OFFSET < len(line_array):
507521
commit["pre_commit"] = line_array[i + PREVIOUS_COMMIT_OFFSET]
508522

509523
result.append(commit)
510-
i += PREVIOUS_COMMIT_OFFSET
524+
511525
return {"code": code, "commits": result}
512526

513527
async def detailed_log(self, selected_hash, path):
@@ -530,6 +544,7 @@ async def detailed_log(self, selected_hash, path):
530544
line_iterable = iter(strip_and_split(my_output)[1:])
531545
for line in line_iterable:
532546
is_binary = line.startswith("-\t-\t")
547+
previous_file_path = ""
533548
insertions, deletions, file = line.split("\t")
534549
insertions = insertions.replace("-", "0")
535550
deletions = deletions.replace("-", "0")
@@ -538,21 +553,25 @@ async def detailed_log(self, selected_hash, path):
538553
# file was renamed or moved, we need next two lines of output
539554
from_path = next(line_iterable)
540555
to_path = next(line_iterable)
556+
previous_file_path = from_path
541557
modified_file_name = from_path + " => " + to_path
542558
modified_file_path = to_path
543559
else:
544560
modified_file_name = file.split("/")[-1]
545561
modified_file_path = file
546562

547-
result.append(
548-
{
549-
"modified_file_path": modified_file_path,
550-
"modified_file_name": modified_file_name,
551-
"insertion": insertions,
552-
"deletion": deletions,
553-
"is_binary": is_binary,
554-
}
555-
)
563+
file_info = {
564+
"modified_file_path": modified_file_path,
565+
"modified_file_name": modified_file_name,
566+
"insertion": insertions,
567+
"deletion": deletions,
568+
"is_binary": is_binary,
569+
}
570+
571+
if previous_file_path:
572+
file_info["previous_file_path"] = previous_file_path
573+
574+
result.append(file_info)
556575
total_insertions += int(insertions)
557576
total_deletions += int(deletions)
558577

jupyterlab_git/tests/test_detailed_log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async def test_detailed_log():
7474
{
7575
"modified_file_path": "folder2/file with spaces.py",
7676
"modified_file_name": "folder1/file with spaces and λ.py => folder2/file with spaces.py",
77+
"previous_file_path": "folder1/file with spaces and λ.py",
7778
"insertion": "0",
7879
"deletion": "0",
7980
"is_binary": False,

0 commit comments

Comments
 (0)