Skip to content

Commit e30cd36

Browse files
authored
Merge pull request openSUSE#1994 from dmach/git-obs-meta-during-git-rebase
Fix working with meta during git rebase by determining the current branch from rebase head
2 parents f6de3e5 + 4c4ee1f commit e30cd36

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

osc/gitea_api/git.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def topdir(self) -> Optional[str]:
9999

100100
return path
101101

102+
@property
103+
def git_dir(self) -> Optional[str]:
104+
try:
105+
return self._run_git(["rev-parse", "--git-dir"])
106+
except subprocess.CalledProcessError:
107+
return None
108+
102109
def init(self, *, initial_branch: Optional[str] = None, quiet: bool = True, mute_stderr: bool = False):
103110
cmd = ["init"]
104111
if initial_branch:
@@ -131,9 +138,29 @@ def clone(self,
131138
@property
132139
def current_branch(self) -> Optional[str]:
133140
try:
134-
return self._run_git(["branch", "--show-current"], mute_stderr=True)
141+
result = self._run_git(["branch", "--show-current"], mute_stderr=True)
135142
except subprocess.CalledProcessError:
136-
return None
143+
result = None
144+
145+
if not result:
146+
# try to determine the branch during rebase
147+
git_dir = self.git_dir
148+
if git_dir:
149+
paths = [
150+
os.path.join(git_dir, "rebase-apply", "head-name"),
151+
os.path.join(git_dir, "rebase-merge", "head-name"),
152+
]
153+
for path in paths:
154+
try:
155+
with open(path, "r", encoding="utf-8") as f:
156+
line = f.readline()
157+
# parse "refs/heads/<branch>"
158+
result = line.strip().split("/", 2)[-1]
159+
break
160+
except FileNotFoundError:
161+
pass
162+
163+
return result
137164

138165
def branch(self, branch: str, set_upstream_to: Optional[str] = None):
139166
cmd = ["branch"]

0 commit comments

Comments
 (0)