Skip to content

Commit 40855ae

Browse files
author
dualc
committed
fix checkout remote branch when branch name contains slash
1 parent 861374f commit 40855ae

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

jupyterlab_git/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ async def checkout_branch(self, branchname, path):
11371137
is_remote_branch = self._is_remote_branch(reference_name)
11381138

11391139
if is_remote_branch:
1140-
local_branchname = branchname.split("/")[-1]
1140+
local_branchname = "/".join(branchname.split("/")[1:])
11411141
cmd = ["git", "checkout", "-B", local_branchname, branchname]
11421142
else:
11431143
cmd = ["git", "checkout", branchname]

jupyterlab_git/tests/test_branch.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,47 @@ async def test_checkout_branch_remoteref_success():
174174
assert {"code": rc, "message": stdout_message} == actual_response
175175

176176

177+
@pytest.mark.asyncio
178+
async def test_checkout_branch_remoteref_success_when_has_slash():
179+
branch = "origin/test-branch/test"
180+
local_branch = "test-branch/test"
181+
curr_path = str(Path("/bin/test_curr_path"))
182+
stdout_message = "checkout output from git"
183+
stderr_message = ""
184+
rc = 0
185+
186+
with patch("jupyterlab_git.git.execute") as mock_execute:
187+
with patch.object(
188+
Git,
189+
"_get_branch_reference",
190+
return_value=maybe_future("refs/remotes/remote_branch"),
191+
) as mock__get_branch_reference:
192+
# Given
193+
mock_execute.return_value = maybe_future(
194+
(rc, stdout_message, stderr_message)
195+
)
196+
197+
# When
198+
actual_response = await Git().checkout_branch(
199+
branchname=branch, path=curr_path
200+
)
201+
202+
# Then
203+
mock__get_branch_reference.assert_has_calls([call(branch, curr_path)])
204+
205+
cmd = ["git", "checkout", "-B", local_branch, branch]
206+
mock_execute.assert_called_once_with(
207+
cmd,
208+
cwd=str(Path("/bin") / "test_curr_path"),
209+
timeout=20,
210+
env=None,
211+
username=None,
212+
password=None,
213+
is_binary=False,
214+
)
215+
216+
assert {"code": rc, "message": stdout_message} == actual_response
217+
177218
@pytest.mark.asyncio
178219
async def test_checkout_branch_headsref_failure():
179220
branch = "test-branch"

0 commit comments

Comments
 (0)