From a6db8ad25c964d2164c831da8a184ba6f4603516 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 | 17 +++++++++++------ osc/git_scm/store.py | 28 +++++++++++++++++++++++++++- osc/store.py | 20 +++++++++++++------- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index 7e18ce141e..d83bf3c885 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7269,6 +7269,7 @@ def do_checkconstraints(self, subcmd, opts, *args): @cmdln.alias('repos') @cmdln.alias('platforms') def do_repositories(self, subcmd, opts, *args): + from . import store as osc_store from .core import build_table from .core import get_repos_of_project from .core import get_repositories_of_project @@ -7299,11 +7300,12 @@ def do_repositories(self, subcmd, opts, *args): project = self._process_project_name(args[0]) package = args[1] elif len(args) == 0: - if is_package_dir('.'): - package = store_read_package('.') - project = store_read_project('.') - elif is_project_dir('.'): - project = store_read_project('.') + store = osc_store.get_store(".") + if store.is_package: + package = store.package + project = store.project + elif store.is_project: + project = store.project else: raise oscerr.WrongArgs('Wrong number of arguments') @@ -7731,7 +7733,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..47d6b4eaee 100644 --- a/osc/git_scm/store.py +++ b/osc/git_scm/store.py @@ -29,10 +29,26 @@ def is_package_dir(cls, path): def __init__(self, path, check=True): self.path = path self.abspath = os.path.abspath(self.path) + try: + self.toplevel = self._run_git(["rev-parse", "--show-toplevel"]) + self.toplevel = os.path.abspath(self.toplevel) + except subprocess.CalledProcessError: + self.toplevel = None # 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")) + self.is_package = False + + if self.toplevel: + # NOTE: we have only one store in project-git for all packages + 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 +84,21 @@ def apiurl(self): @property def project(self): + if self._project is None: + try: + # NOTE: this never triggers if a store is retrieved from osc.store.get_store(), + # because obs_scm store takes precedence as .osc is present + 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..3582335ca4 100644 --- a/osc/store.py +++ b/osc/store.py @@ -21,14 +21,20 @@ def get_store(path, check=True, print_warnings=False): """ # if there are '.osc' and '.git' directories next to each other, '.osc' takes preference - if os.path.exists(os.path.join(path, ".osc")): + store = None + + try: store = Store(path, check) - elif os.path.exists(os.path.join(path, ".git")): - store = git_scm.GitStore(path, check) - if print_warnings: - git_scm.warn_experimental() - else: - store = None + except oscerr.NoWorkingCopy: + pass + + if not store: + try: + store = git_scm.GitStore(path, check) + if print_warnings: + git_scm.warn_experimental() + except oscerr.NoWorkingCopy: + pass if not store: msg = f"Directory '{path}' is not a working copy"