Skip to content

Commit 9b13aad

Browse files
committed
allow pushing of branches with a / in the name
also modified get_upstream_branch to return the remote short name as well. Shouldn't affect anything else as this is the only place this is used
1 parent 2a05b76 commit 9b13aad

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

jupyterlab_git/git.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -890,12 +890,12 @@ async def get_current_branch(self, current_path):
890890
to the `branch` command to get the name.
891891
See https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
892892
"""
893-
command = ["git", "symbolic-ref", "HEAD"]
893+
command = ["git", "symbolic-ref", "--short", "HEAD"]
894894
code, output, error = await execute(
895895
command, cwd=os.path.join(self.root_dir, current_path)
896896
)
897897
if code == 0:
898-
return output.split("/")[-1].strip()
898+
return output.strip()
899899
elif "not a symbolic ref" in error.lower():
900900
current_branch = await self._get_current_branch_detached(current_path)
901901
return current_branch
@@ -939,18 +939,22 @@ async def get_upstream_branch(self, current_path, branch_name):
939939
code, output, error = await execute(
940940
command, cwd=os.path.join(self.root_dir, current_path)
941941
)
942-
if code == 0:
943-
return output.strip()
944-
elif "fatal: no upstream configured for branch" in error.lower():
945-
return None
946-
elif "unknown revision or path not in the working tree" in error.lower():
947-
return None
948-
else:
949-
raise Exception(
950-
"Error [{}] occurred while executing [{}] command to get upstream branch.".format(
951-
error, " ".join(command)
952-
)
953-
)
942+
if code != 0:
943+
return {"code": code, "command": " ".join(cmd), "message": error}
944+
rev_parse_output = output.strip()
945+
946+
command = ["git", "config", "--local", f"branch.{branch_name}.remote"]
947+
code, output, error = await execute(
948+
command, cwd=os.path.join(self.root_dir, current_path)
949+
)
950+
if code != 0:
951+
return {"code": code, "command": " ".join(cmd), "message": error}
952+
953+
remote_name = output.strip()
954+
remote_branch = rev_parse_output.strip().lstrip(remote_name+"/")
955+
return {"code": code, "remote_short_name": remote_name, "remote_branch": remote_branch}
956+
957+
954958

955959
async def _get_tag(self, current_path, commit_sha):
956960
"""Execute 'git describe commit_sha' to get

jupyterlab_git/handlers.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -437,32 +437,28 @@ async def post(self):
437437
current_path = data["current_path"]
438438

439439
current_local_branch = await self.git.get_current_branch(current_path)
440-
current_upstream_branch = await self.git.get_upstream_branch(
440+
upstream = await self.git.get_upstream_branch(
441441
current_path, current_local_branch
442442
)
443443

444-
if current_upstream_branch and current_upstream_branch.strip():
445-
upstream = current_upstream_branch.split("/")
446-
if len(upstream) == 1:
447-
# If upstream is a local branch
448-
remote = "."
449-
branch = ":".join(["HEAD", upstream[0]])
450-
else:
451-
# If upstream is a remote branch
452-
remote = upstream[0]
453-
branch = ":".join(["HEAD", upstream[1]])
454-
444+
if upstream['code'] == 0:
445+
branch = ":".join(["HEAD", upstream['remote_branch']])
455446
response = await self.git.push(
456-
remote, branch, current_path, data.get("auth", None)
447+
upstream['remote_short_name'], branch, current_path, data.get("auth", None)
457448
)
458449

459450
else:
460-
response = {
461-
"code": 128,
462-
"message": "fatal: The current branch {} has no upstream branch.".format(
463-
current_local_branch
464-
),
465-
}
451+
if ("no upstream configured for branch" in upstream['message'].lower()
452+
or 'unknown revision or path' in upstream['message'].lower()):
453+
response = {
454+
"code": 128,
455+
"message": "fatal: The current branch {} has no upstream branch.".format(
456+
current_local_branch
457+
),
458+
}
459+
else:
460+
self.set_status(500)
461+
466462
self.finish(json.dumps(response))
467463

468464

0 commit comments

Comments
 (0)