Skip to content

Commit

Permalink
Fixed error switching remote branch, fixes #741 (#1066)
Browse files Browse the repository at this point in the history
Co-authored-by: Piyush Jain <[email protected]>
  • Loading branch information
3coins and Piyush Jain authored Jan 14, 2022
1 parent b0f4765 commit 6579bc0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ async def checkout_branch(self, branchname, path):
is_remote_branch = self._is_remote_branch(reference_name)

if is_remote_branch:
cmd = ["git", "checkout", "--track", branchname]
local_branchname = branchname.split("/")[-1]
cmd = ["git", "checkout", "-B", local_branchname, branchname]
else:
cmd = ["git", "checkout", branchname]

Expand Down
10 changes: 6 additions & 4 deletions jupyterlab_git/tests/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ async def test_checkout_branch_noref_failure():

@pytest.mark.asyncio
async def test_checkout_branch_remoteref_success():
branch = "test-branch"
branch = "origin/test-branch"
local_branch = "test-branch"
curr_path = str(Path("/bin/test_curr_path"))
stdout_message = "checkout output from git"
stderr_message = ""
Expand All @@ -145,7 +146,7 @@ async def test_checkout_branch_remoteref_success():
# Then
mock__get_branch_reference.assert_has_calls([call(branch, curr_path)])

cmd = ["git", "checkout", "--track", branch]
cmd = ["git", "checkout", "-B", local_branch, branch]
mock_execute.assert_called_once_with(
cmd,
cwd=str(Path("/bin") / "test_curr_path"),
Expand Down Expand Up @@ -229,7 +230,8 @@ async def test_checkout_branch_headsref_success():

@pytest.mark.asyncio
async def test_checkout_branch_remoteref_failure():
branch = "test-branch"
branch = "origin/test-branch"
local_branch = "test-branch"
stdout_message = ""
stderr_message = (
"error: pathspec '{}' did not match any file(s) known to git".format(branch)
Expand All @@ -253,7 +255,7 @@ async def test_checkout_branch_remoteref_failure():
)

# Then
cmd = ["git", "checkout", "--track", branch]
cmd = ["git", "checkout", "-B", local_branch, branch]
mock_execute.assert_called_once_with(
cmd,
cwd=str(Path("/bin") / "test_curr_path"),
Expand Down

0 comments on commit 6579bc0

Please sign in to comment.