Skip to content

Commit 44ad13e

Browse files
jeanmonetfcollonval
authored andcommitted
Backport PR #855: Avoid 500 error messages when current working directory in Jupyter Lab is not a git repo
1 parent 90cfb96 commit 44ad13e

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ node_modules/
124124
*.swp
125125
.devcontainer/
126126
src/version.ts
127+
jupyterlab_git/labextension/

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: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestAllHistory(ServerTest):
3030
@patch("jupyterlab_git.handlers.GitAllHistoryHandler.git", spec=Git)
3131
def test_all_history_handler_localbranch(self, mock_git):
3232
# Given
33-
show_top_level = {"code": 0, "foo": "top_level"}
33+
show_top_level = {"code": 0, "top_repo_path": "foo"}
3434
branch = "branch_foo"
3535
log = "log_foo"
3636
status = "status_foo"
@@ -63,6 +63,62 @@ def test_all_history_handler_localbranch(self, mock_git):
6363
}
6464

6565

66+
class TestShowTopLevel(ServerTest):
67+
@patch("jupyterlab_git.git.execute")
68+
def test_git_show_top_level(self, mock_execute):
69+
# Given
70+
top_repo_path = "path/to/repo"
71+
72+
mock_execute.return_value = maybe_future((0, str(top_repo_path), ""))
73+
74+
# When
75+
body = {
76+
"current_path": top_repo_path + "/subfolder",
77+
}
78+
response = self.tester.post(["show_top_level"], body=body)
79+
80+
# Then
81+
assert response.status_code == 200
82+
payload = response.json()
83+
assert payload["top_repo_path"] == top_repo_path
84+
mock_execute.assert_has_calls(
85+
[
86+
call(
87+
["git", "rev-parse", "--show-toplevel"],
88+
cwd=os.path.join(self.notebook_dir, top_repo_path, "subfolder"),
89+
),
90+
]
91+
)
92+
93+
@patch("jupyterlab_git.git.execute")
94+
def test_git_show_top_level_not_a_git_repo(self, mock_execute):
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 = self.tester.post(["show_top_level"], body=body)
107+
108+
# Then
109+
assert response.status_code == 200
110+
payload = response.json()
111+
assert payload["top_repo_path"] is None
112+
mock_execute.assert_has_calls(
113+
[
114+
call(
115+
["git", "rev-parse", "--show-toplevel"],
116+
cwd=os.path.join(self.notebook_dir, top_repo_path, "subfolder"),
117+
),
118+
]
119+
)
120+
121+
66122
class TestBranch(ServerTest):
67123
@patch("jupyterlab_git.handlers.GitBranchHandler.git", spec=Git)
68124
def test_branch_handler_localbranch(self, mock_git):

0 commit comments

Comments
 (0)