Skip to content

Commit

Permalink
Merge pull request #766 from fcollonval/fix/text-diff
Browse files Browse the repository at this point in the history
Fix text diff for 1st commit and deleted files
  • Loading branch information
ianhi committed Sep 8, 2020
2 parents a4d5970 + 24d991f commit 09bf406
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
29 changes: 22 additions & 7 deletions jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ async def show(self, filename, ref, top_repo_path):
"fatal: Path '{}' does not exist (neither on disk nor in the index)".format(
filename
),
"fatal: Path '{}' does not exist in '{}'".format(filename, ref),
],
)
lower_error = error.lower()
Expand All @@ -1104,20 +1105,34 @@ def get_content(self, filename, top_repo_path):
Get the file content of filename.
"""
relative_repo = os.path.relpath(top_repo_path, self.root_dir)
model = self.contents_manager.get(path=os.path.join(relative_repo, filename))
try:
model = self.contents_manager.get(
path=os.path.join(relative_repo, filename)
)
except tornado.web.HTTPError as error:
# Handle versioned file being deleted case
if error.status_code == 404 and error.log_message.startswith(
"No such file or directory: "
):
return ""
raise error
return model["content"]

async def diff_content(self, filename, prev_ref, curr_ref, top_repo_path):
"""
Collect get content of prev and curr and return.
"""
is_binary = await self._is_binary(filename, prev_ref["git"], top_repo_path)
if is_binary:
raise tornado.web.HTTPError(
log_message="Error occurred while executing command to retrieve plaintext diff as file is not UTF-8."
)
if prev_ref["git"]:
is_binary = await self._is_binary(filename, prev_ref["git"], top_repo_path)
if is_binary:
raise tornado.web.HTTPError(
log_message="Error occurred while executing command to retrieve plaintext diff as file is not UTF-8."
)

prev_content = await self.show(filename, prev_ref["git"], top_repo_path)
else:
prev_content = ""

prev_content = await self.show(filename, prev_ref["git"], top_repo_path)
if "special" in curr_ref:
if curr_ref["special"] == "WORKING":
curr_content = self.get_content(filename, top_repo_path)
Expand Down
20 changes: 16 additions & 4 deletions jupyterlab_git/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_diffcontent_show_unhandled_error(self, mock_execute):
self.tester.post(["diffcontent"], body=body)

@patch("jupyterlab_git.git.execute")
def test_diffcontent_getcontent_error(self, mock_execute):
def test_diffcontent_getcontent_deleted_file(self, mock_execute):
# Given
top_repo_path = "path/to/repo"
filename = "my/absent_file"
Expand All @@ -688,7 +688,6 @@ def test_diffcontent_getcontent_error(self, mock_execute):
mock_execute.side_effect = [
maybe_future((0, "1\t1\t{}".format(filename), "")),
maybe_future((0, content, "")),
maybe_future((0, content, "")),
]

# When
Expand All @@ -699,5 +698,18 @@ def test_diffcontent_getcontent_error(self, mock_execute):
"top_repo_path": top_repo_path,
}
# Then
with assert_http_error(404, msg="No such file or directory"):
self.tester.post(["diffcontent"], body=body)
response = self.tester.post(["diffcontent"], body=body)

# Then
assert response.status_code == 200
payload = response.json()
assert payload["prev_content"] == content
assert payload["curr_content"] == ""
mock_execute.assert_has_calls(
[
call(
["git", "show", "{}:{}".format("previous", filename)],
cwd=os.path.join(self.notebook_dir, top_repo_path),
)
]
)

0 comments on commit 09bf406

Please sign in to comment.