Skip to content

Commit

Permalink
Fix Git history of relocated files (jupyterlab#1039)
Browse files Browse the repository at this point in the history
* Fix Git history of relocated files (jupyterlab#1038)

* Add relocated file commit case

* Use previous path when commit relocates file

* Fix relocated file commits for repository history

- Adds better diff labels
  • Loading branch information
navn-r authored Oct 4, 2021
1 parent 10a627e commit b4590f8
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 363 deletions.
51 changes: 35 additions & 16 deletions jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ async def log(self, path, history_count=10, follow_path=None):
("-%d" % history_count),
]
if is_single_file:
cmd = cmd + [
cmd += [
"-z",
"--numstat",
"--follow",
"--",
Expand All @@ -485,13 +486,19 @@ async def log(self, path, history_count=10, follow_path=None):
return {"code": code, "command": " ".join(cmd), "message": my_error}

result = []
if is_single_file:
# an extra newline get outputted when --numstat is used
my_output = my_output.replace("\n\n", "\n")
line_array = my_output.splitlines()
i = 0

if is_single_file:
parsed_lines = []
for line in line_array:
parsed_lines.extend(
re.sub(r"\t\0|\0", "\t", l)
for l in line.strip("\0\t").split("\0\0", maxsplit=1)
)
line_array = parsed_lines

PREVIOUS_COMMIT_OFFSET = 5 if is_single_file else 4
while i < len(line_array):
for i in range(0, len(line_array), PREVIOUS_COMMIT_OFFSET):
commit = {
"commit": line_array[i],
"author": line_array[i + 1],
Expand All @@ -503,11 +510,18 @@ async def log(self, path, history_count=10, follow_path=None):
if is_single_file:
commit["is_binary"] = line_array[i + 4].startswith("-\t-\t")

# [insertions, deletions, previous_file_path?, current_file_path]
file_info = line_array[i + 4].split()

if len(file_info) == 4:
commit["previous_file_path"] = file_info[2]
commit["file_path"] = file_info[-1]

if i + PREVIOUS_COMMIT_OFFSET < len(line_array):
commit["pre_commit"] = line_array[i + PREVIOUS_COMMIT_OFFSET]

result.append(commit)
i += PREVIOUS_COMMIT_OFFSET

return {"code": code, "commits": result}

async def detailed_log(self, selected_hash, path):
Expand All @@ -530,6 +544,7 @@ async def detailed_log(self, selected_hash, path):
line_iterable = iter(strip_and_split(my_output)[1:])
for line in line_iterable:
is_binary = line.startswith("-\t-\t")
previous_file_path = ""
insertions, deletions, file = line.split("\t")
insertions = insertions.replace("-", "0")
deletions = deletions.replace("-", "0")
Expand All @@ -538,21 +553,25 @@ async def detailed_log(self, selected_hash, path):
# file was renamed or moved, we need next two lines of output
from_path = next(line_iterable)
to_path = next(line_iterable)
previous_file_path = from_path
modified_file_name = from_path + " => " + to_path
modified_file_path = to_path
else:
modified_file_name = file.split("/")[-1]
modified_file_path = file

result.append(
{
"modified_file_path": modified_file_path,
"modified_file_name": modified_file_name,
"insertion": insertions,
"deletion": deletions,
"is_binary": is_binary,
}
)
file_info = {
"modified_file_path": modified_file_path,
"modified_file_name": modified_file_name,
"insertion": insertions,
"deletion": deletions,
"is_binary": is_binary,
}

if previous_file_path:
file_info["previous_file_path"] = previous_file_path

result.append(file_info)
total_insertions += int(insertions)
total_deletions += int(deletions)

Expand Down
1 change: 1 addition & 0 deletions jupyterlab_git/tests/test_detailed_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async def test_detailed_log():
{
"modified_file_path": "folder2/file with spaces.py",
"modified_file_name": "folder1/file with spaces and λ.py => folder2/file with spaces.py",
"previous_file_path": "folder1/file with spaces and λ.py",
"insertion": "0",
"deletion": "0",
"is_binary": False,
Expand Down
Loading

0 comments on commit b4590f8

Please sign in to comment.