1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-12-23 17:29:24 +01:00

gitea/git: handle local branch when fetching remote

If the current branch tracks a local branch that in turn is tracking a
remote branch, recurse to find out the remote, instead of simply
returning the local branch.

Signed-off-by: Danish Prakash <contact@danishpraka.sh>
This commit is contained in:
Danish Prakash
2025-11-14 14:18:30 +05:30
parent 3fcc00cf1b
commit ba0f9c1a99

View File

@@ -278,9 +278,29 @@ class Git:
def get_current_remote(self, fallback_to_origin: bool = True) -> Optional[str]:
result = None
try:
result = self._run_git(["rev-parse", "--abbrev-ref", "@{u}"], mute_stderr=True)
if result:
result = result.split("/")[0]
# get the upstream branch that the current branch is tracking:
# case 1: upstream is a remote-tracking branch origin/main
# case 2: upstream is a local branch slfo-main
upstream = self._run_git(
["rev-parse", "--abbrev-ref", "@{u}"], mute_stderr=True
)
if "/" in upstream:
result = upstream.split("/")[0]
try:
self._run_git(["remote", "get-url", result], mute_stderr=True)
except subprocess.CalledProcessError:
result = None
else:
# case 2: upstream is a local branch
# look up the remote that the local branch tracks
try:
remote_ref = self._run_git(
["config", f"branch.{upstream}.remote"], mute_stderr=True
)
if remote_ref:
result = remote_ref
except subprocess.CalledProcessError:
pass
except subprocess.CalledProcessError:
pass