Skip to content

Commit 3c28978

Browse files
authored
Avoid 500 error messages when current working directory in Jupyter Lab is not a git repo (#855)
* Avoid 500 error messages when cwd is not git repo Avoid long 500 error messages in terminal when current working directory in Jupyter Lab is not a git repository. Instead, print a message to terminal (`self.log.info`) of the git error message `fatal: not a git repository (or any of the parent directories):` Discussed: #842
1 parent f9efa96 commit 3c28978

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

jupyterlab_git/git.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@ async def show_top_level(self, current_path):
682682
if code == 0:
683683
return {"code": code, "top_repo_path": my_output.strip("\n")}
684684
else:
685+
# Handle special case where cwd not inside a git repo
686+
lower_error = my_error.lower()
687+
if "fatal: not a git repository" in lower_error:
688+
return {"code": 0, "top_repo_path": None}
689+
685690
return {
686691
"code": code,
687692
"command": " ".join(cmd),

jupyterlab_git/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async def post(self):
7474
history_count = body["history_count"]
7575

7676
show_top_level = await self.git.show_top_level(current_path)
77-
if show_top_level["code"] != 0:
77+
if show_top_level.get("top_repo_path") is None:
7878
self.set_status(500)
7979
self.finish(json.dumps(show_top_level))
8080
else:

jupyterlab_git/tests/test_handlers.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_mapping_added():
2828
@patch("jupyterlab_git.handlers.GitAllHistoryHandler.git", spec=Git)
2929
async def test_all_history_handler_localbranch(mock_git, jp_fetch):
3030
# Given
31-
show_top_level = {"code": 0, "foo": "top_level"}
31+
show_top_level = {"code": 0, "top_repo_path": "foo"}
3232
branch = "branch_foo"
3333
log = "log_foo"
3434
status = "status_foo"
@@ -61,6 +61,66 @@ async def test_all_history_handler_localbranch(mock_git, jp_fetch):
6161
}
6262

6363

64+
@patch("jupyterlab_git.git.execute")
65+
async def test_git_show_top_level(mock_execute, jp_fetch, jp_root_dir):
66+
# Given
67+
top_repo_path = "path/to/repo"
68+
69+
mock_execute.return_value = maybe_future((0, str(top_repo_path), ""))
70+
71+
# When
72+
body = {
73+
"current_path": top_repo_path + "/subfolder",
74+
}
75+
response = await jp_fetch(
76+
NS, "show_top_level", body=json.dumps(body), method="POST"
77+
)
78+
79+
# Then
80+
assert response.code == 200
81+
payload = json.loads(response.body)
82+
assert payload["top_repo_path"] == top_repo_path
83+
mock_execute.assert_has_calls(
84+
[
85+
call(
86+
["git", "rev-parse", "--show-toplevel"],
87+
cwd=str(jp_root_dir / top_repo_path / "subfolder"),
88+
),
89+
]
90+
)
91+
92+
93+
@patch("jupyterlab_git.git.execute")
94+
async def test_git_show_top_level_not_a_git_repo(mock_execute, jp_fetch, jp_root_dir):
95+
# Given
96+
top_repo_path = "path/to/repo"
97+
98+
mock_execute.return_value = maybe_future(
99+
(128, "", "fatal: not a git repository (or any")
100+
)
101+
102+
# When
103+
body = {
104+
"current_path": top_repo_path + "/subfolder",
105+
}
106+
response = await jp_fetch(
107+
NS, "show_top_level", body=json.dumps(body), method="POST"
108+
)
109+
110+
# Then
111+
assert response.code == 200
112+
payload = json.loads(response.body)
113+
assert payload["top_repo_path"] is None
114+
mock_execute.assert_has_calls(
115+
[
116+
call(
117+
["git", "rev-parse", "--show-toplevel"],
118+
cwd=str(jp_root_dir / top_repo_path / "subfolder"),
119+
),
120+
]
121+
)
122+
123+
64124
@patch("jupyterlab_git.handlers.GitBranchHandler.git", spec=Git)
65125
async def test_branch_handler_localbranch(mock_git, jp_fetch):
66126
# Given

0 commit comments

Comments
 (0)