Skip to content

Commit 09bf406

Browse files
authored
Merge pull request #766 from fcollonval/fix/text-diff
Fix text diff for 1st commit and deleted files
2 parents a4d5970 + 24d991f commit 09bf406

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

jupyterlab_git/git.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ async def show(self, filename, ref, top_repo_path):
10851085
"fatal: Path '{}' does not exist (neither on disk nor in the index)".format(
10861086
filename
10871087
),
1088+
"fatal: Path '{}' does not exist in '{}'".format(filename, ref),
10881089
],
10891090
)
10901091
lower_error = error.lower()
@@ -1104,20 +1105,34 @@ def get_content(self, filename, top_repo_path):
11041105
Get the file content of filename.
11051106
"""
11061107
relative_repo = os.path.relpath(top_repo_path, self.root_dir)
1107-
model = self.contents_manager.get(path=os.path.join(relative_repo, filename))
1108+
try:
1109+
model = self.contents_manager.get(
1110+
path=os.path.join(relative_repo, filename)
1111+
)
1112+
except tornado.web.HTTPError as error:
1113+
# Handle versioned file being deleted case
1114+
if error.status_code == 404 and error.log_message.startswith(
1115+
"No such file or directory: "
1116+
):
1117+
return ""
1118+
raise error
11081119
return model["content"]
11091120

11101121
async def diff_content(self, filename, prev_ref, curr_ref, top_repo_path):
11111122
"""
11121123
Collect get content of prev and curr and return.
11131124
"""
1114-
is_binary = await self._is_binary(filename, prev_ref["git"], top_repo_path)
1115-
if is_binary:
1116-
raise tornado.web.HTTPError(
1117-
log_message="Error occurred while executing command to retrieve plaintext diff as file is not UTF-8."
1118-
)
1125+
if prev_ref["git"]:
1126+
is_binary = await self._is_binary(filename, prev_ref["git"], top_repo_path)
1127+
if is_binary:
1128+
raise tornado.web.HTTPError(
1129+
log_message="Error occurred while executing command to retrieve plaintext diff as file is not UTF-8."
1130+
)
1131+
1132+
prev_content = await self.show(filename, prev_ref["git"], top_repo_path)
1133+
else:
1134+
prev_content = ""
11191135

1120-
prev_content = await self.show(filename, prev_ref["git"], top_repo_path)
11211136
if "special" in curr_ref:
11221137
if curr_ref["special"] == "WORKING":
11231138
curr_content = self.get_content(filename, top_repo_path)

jupyterlab_git/tests/test_handlers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def test_diffcontent_show_unhandled_error(self, mock_execute):
679679
self.tester.post(["diffcontent"], body=body)
680680

681681
@patch("jupyterlab_git.git.execute")
682-
def test_diffcontent_getcontent_error(self, mock_execute):
682+
def test_diffcontent_getcontent_deleted_file(self, mock_execute):
683683
# Given
684684
top_repo_path = "path/to/repo"
685685
filename = "my/absent_file"
@@ -688,7 +688,6 @@ def test_diffcontent_getcontent_error(self, mock_execute):
688688
mock_execute.side_effect = [
689689
maybe_future((0, "1\t1\t{}".format(filename), "")),
690690
maybe_future((0, content, "")),
691-
maybe_future((0, content, "")),
692691
]
693692

694693
# When
@@ -699,5 +698,18 @@ def test_diffcontent_getcontent_error(self, mock_execute):
699698
"top_repo_path": top_repo_path,
700699
}
701700
# Then
702-
with assert_http_error(404, msg="No such file or directory"):
703-
self.tester.post(["diffcontent"], body=body)
701+
response = self.tester.post(["diffcontent"], body=body)
702+
703+
# Then
704+
assert response.status_code == 200
705+
payload = response.json()
706+
assert payload["prev_content"] == content
707+
assert payload["curr_content"] == ""
708+
mock_execute.assert_has_calls(
709+
[
710+
call(
711+
["git", "show", "{}:{}".format("previous", filename)],
712+
cwd=os.path.join(self.notebook_dir, top_repo_path),
713+
)
714+
]
715+
)

0 commit comments

Comments
 (0)