Skip to content

Commit 172e827

Browse files
committed
Migrate git_scm.Store over to gitea_api.Git
1 parent 9dbd333 commit 172e827

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

osc/git_scm/store.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ def get_project_git_scm_store(self):
184184
return GitStore(path)
185185

186186
def __init__(self, path, check=True):
187+
from ..gitea_api import Git
188+
189+
self._git = Git(path)
187190
self.path = path
188191
self.abspath = os.path.abspath(self.path)
189192

@@ -225,7 +228,7 @@ def __init__(self, path, check=True):
225228
raise oscerr.NoWorkingCopy(msg)
226229

227230
# TODO: decide if we need explicit 'git lfs pull' or not
228-
# self._run_git(["lfs", "pull"])
231+
# self._git._run_git(["lfs", "pull"])
229232

230233
def assert_is_project(self):
231234
if not self.is_project:
@@ -237,9 +240,6 @@ def assert_is_package(self):
237240
msg = f"Directory '{self.path}' is not a Git SCM working copy of a package"
238241
raise oscerr.NoWorkingCopy(msg)
239242

240-
def _run_git(self, args):
241-
return subprocess.check_output(["git"] + args, encoding="utf-8", cwd=self.abspath).strip()
242-
243243
@property
244244
def apiurl(self):
245245
from ..obs_scm import Store
@@ -291,8 +291,8 @@ def project(self):
291291

292292
if not self._project:
293293
# read project from Gitea (identical owner, repo: _ObsPrj, file: project.build)
294-
origin = self._run_git(["remote", "get-url", self.current_remote])
295-
self._project = self.get_build_project(origin)
294+
remote_url = self._git.get_remote_url()
295+
self._project = self.get_build_project(remote_url)
296296

297297
else:
298298
# handle _project in a project
@@ -317,8 +317,8 @@ def project(self, value):
317317
@property
318318
def package(self):
319319
if self._package is None:
320-
origin = self._run_git(["remote", "get-url", self.current_remote])
321-
self._package = Path(urllib.parse.urlsplit(origin).path).stem
320+
remote_url = self._git.get_remote_url()
321+
self._package = Path(urllib.parse.urlsplit(remote_url).path).stem
322322
return self._package
323323

324324
@package.setter
@@ -327,7 +327,7 @@ def package(self, value):
327327

328328
def _get_option(self, name):
329329
try:
330-
result = self._run_git(["config", "--local", "--get", f"osc.{name}"])
330+
result = self._git._run_git(["config", "--local", "--get", f"osc.{name}"])
331331
except subprocess.CalledProcessError:
332332
result = None
333333
return result
@@ -337,11 +337,11 @@ def _check_type(self, name, value, expected_type):
337337
raise TypeError(f"The option '{name}' should be {expected_type.__name__}, not {type(value).__name__}")
338338

339339
def _set_option(self, name, value):
340-
self._run_git(["config", "--local", f"osc.{name}", value])
340+
self._git._run_git(["config", "--local", f"osc.{name}", value])
341341

342342
def _unset_option(self, name):
343343
try:
344-
self._run_git(["config", "--local", "--unset", f"osc.{name}"])
344+
self._git._run_git(["config", "--local", "--unset", f"osc.{name}"])
345345
except subprocess.CalledProcessError:
346346
pass
347347

@@ -423,20 +423,6 @@ def build_repositories(self, value):
423423
@property
424424
def scmurl(self):
425425
try:
426-
return self._run_git(["remote", "get-url", self.current_remote])
426+
return self._git.get_remote_url()
427427
except subprocess.CalledProcessError:
428428
return None
429-
430-
@property
431-
def current_remote(self):
432-
result = None
433-
try:
434-
result = self._run_git(["rev-parse", "--abbrev-ref", "@{u}"])
435-
if result:
436-
result = result.split("/")[0]
437-
except subprocess.CalledProcessError:
438-
pass
439-
440-
if result:
441-
return result
442-
return "origin"

osc/gitea_api/git.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,34 @@ def set_config(self, key: str, value: str):
136136

137137
# REMOTES
138138

139-
def get_remote_url(self, name: str = "origin") -> str:
139+
def get_remote_url(self, name: Optional[str] = None) -> str:
140+
if not name:
141+
name = self.get_current_remote()
140142
return self._run_git(["remote", "get-url", name])
141143

142144
def add_remote(self, name: str, url: str):
143145
self._run_git(["remote", "add", name, url])
144146

147+
def get_current_remote(self, fallback_to_origin: bool = True) -> Optional[str]:
148+
result = None
149+
try:
150+
result = self._run_git(["rev-parse", "--abbrev-ref", "@{u}"], mute_stderr=True)
151+
if result:
152+
result = result.split("/")[0]
153+
except subprocess.CalledProcessError:
154+
pass
155+
156+
# the tracking information isn't sometimes set
157+
# let's fall back to 'origin' if available
158+
if not result and fallback_to_origin:
159+
try:
160+
self._run_git(["remote", "get-url", "origin"], mute_stderr=True)
161+
result = "origin"
162+
except subprocess.CalledProcessError:
163+
pass
164+
165+
return result
166+
145167
def fetch(self, name: Optional[str] = None):
146168
if name:
147169
cmd = ["fetch", name]

0 commit comments

Comments
 (0)