From 8a17b71eb0ca6a0dc4b5bb2572c7c55a2f11fc76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Schr=C3=B6ter?= Date: Thu, 29 Aug 2024 13:58:46 +0200 Subject: [PATCH] Fix local building in git projects osc did not find it's store and was unable to run a local build in a project git --- osc/commandline.py | 5 ++++- osc/git_scm/store.py | 22 ++++++++++++++++++++-- osc/store.py | 12 ++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index c4cabb48cc..4b4afe54a5 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7151,7 +7151,10 @@ def do_build(self, subcmd, opts, *args): if not opts.local_package: store = osc_store.get_store(Path.cwd(), print_warnings=True) - store.assert_is_package() + if isinstance(store, git_scm.store.GitStore): + opts.local_package = True + else: + store.assert_is_package() try: if opts.alternative_project and opts.alternative_project == store.project: diff --git a/osc/git_scm/store.py b/osc/git_scm/store.py index e72ef0dff5..a4925d206a 100644 --- a/osc/git_scm/store.py +++ b/osc/git_scm/store.py @@ -31,8 +31,17 @@ def __init__(self, path, check=True): self.abspath = os.path.abspath(self.path) # TODO: how to determine if the current git repo contains a project or a package? - self.is_project = False - self.is_package = os.path.exists(os.path.join(self.abspath, ".git")) + + # NOTE: we have only one store in project-git for all packages + self._toplevel = self._run_git(["rev-parse", "--show-toplevel"]) + config_path = os.path.join(self._toplevel, "_config") + pbuild_path = os.path.join(self._toplevel, "_pbuild") + if self._toplevel == self.abspath and (os.path.isfile(config_path) or os.path.isfile(pbuild_path)): + self.is_project = True + self.is_package = False + else: + self.is_project = False + self.is_package = True self._package = None self._project = None @@ -68,11 +77,20 @@ def apiurl(self): @property def project(self): + if self._project is None: + if self._toplevel: + try: + with open(os.path.join(self._toplevel, '.osc/_project')) as f: + self._project = f.readline().strip() + except FileNotFoundError: + pass + if self._project is None: # get project from the branch name branch = self._run_git(["branch", "--show-current"]) # HACK: replace hard-coded mapping with metadata from git or the build service + # NOTE: you never know which git repo is supposed to be used in which project if branch == "factory": self._project = "openSUSE:Factory" else: diff --git a/osc/store.py b/osc/store.py index cf4d8bc352..297f6db344 100644 --- a/osc/store.py +++ b/osc/store.py @@ -6,6 +6,7 @@ import os +import subprocess from xml.etree import ElementTree as ET from . import oscerr @@ -29,6 +30,17 @@ def get_store(path, check=True, print_warnings=False): git_scm.warn_experimental() else: store = None + try: + toplevel = subprocess.check_output(["git", "rev-parse", "--show-toplevel"], + encoding="utf-8", + stderr=subprocess.DEVNULL).strip() + if toplevel: + store = git_scm.GitStore(toplevel, check) + if print_warnings: + git_scm.warn_experimental() + except: + # we should distinguish between git cli fails or git is not installed + pass if not store: msg = f"Directory '{path}' is not a working copy"