Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text diff for 1st commit and deleted files #766

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions jupyterlab_git/git.py
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
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),
)
]
)