From 8cc57e04fbfc4a218dd8ef2814d3cc0ee26f873e 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 | 3 +++ osc/git_scm/store.py | 9 ++++++++- osc/obs_scm/package.py | 6 +++--- osc/store.py | 9 ++++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index 987e25e914..6f7986c4e7 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7404,6 +7404,9 @@ def do_build(self, subcmd, opts, *args): except oscerr.NoWorkingCopy: store = None + if isinstance(store, git_scm.store.GitStore): + opts.local_package = True + # HACK: avoid calling some underlying store_*() functions from parse_repoarchdescr() method # We'll fix parse_repoarchdescr() later because it requires a larger change if not opts.alternative_project and isinstance(store, git_scm.GitStore): diff --git a/osc/git_scm/store.py b/osc/git_scm/store.py index e72ef0dff5..9f3a1fe4d7 100644 --- a/osc/git_scm/store.py +++ b/osc/git_scm/store.py @@ -32,7 +32,7 @@ def __init__(self, path, check=True): # 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 = os.path.exists(os.path.join(self.abspath, ".git")) or os.path.exists(os.path.join(self.abspath, "../.git")) self._package = None self._project = None @@ -68,11 +68,18 @@ def apiurl(self): @property def project(self): + if self._project is None: + toplevel = self._run_git(["rev-parse", "--show-toplevel"]) + if toplevel: + with open(os.path.join(toplevel, '.osc/_project')) as f: + self._project = f.readline().strip() + 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/obs_scm/package.py b/osc/obs_scm/package.py index 6fd1a0ece7..6fa904cce4 100644 --- a/osc/obs_scm/package.py +++ b/osc/obs_scm/package.py @@ -1031,7 +1031,7 @@ def status(self, n): state = '?' else: # this case shouldn't happen (except there was a typo in the filename etc.) - raise oscerr.OscIOError(None, f'osc: \'{n}\' is not under version control') + raise oscerr.OscIOError(None, f'osc: \'{n}\' is not under version control L2') return state @@ -1116,7 +1116,7 @@ def diff_add_delete(fname, add, revision): raise oscerr.OscIOError(None, 'file \'%s\' is marked as \'A\' but does not exist\n' '(either add the missing file or revert it)' % fname) elif not ignoreUnversioned: - raise oscerr.OscIOError(None, f'file \'{fname}\' is not under version control') + raise oscerr.OscIOError(None, f'file \'{fname}\' is not under version control L1') else: fm = self.get_files_meta(revision=revision) root = ET.fromstring(fm) @@ -1551,7 +1551,7 @@ def run_source_services(self, mode=None, singleservice=None, verbose=None): def revert(self, filename): if filename not in self.filenamelist and filename not in self.to_be_added: - raise oscerr.OscIOError(None, f'file \'{filename}\' is not under version control') + raise oscerr.OscIOError(None, f'file \'{filename}\' is not under version control L3') elif filename in self.skipped: raise oscerr.OscIOError(None, f'file \'{filename}\' is marked as skipped and cannot be reverted') if filename in self.filenamelist and not self.store.sources_is_file(filename): diff --git a/osc/store.py b/osc/store.py index cf4d8bc352..a51b732294 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 @@ -28,7 +29,13 @@ def get_store(path, check=True, print_warnings=False): if print_warnings: git_scm.warn_experimental() else: - store = None + toplevel = subprocess.check_output(["git", "rev-parse", "--show-toplevel"], encoding="utf-8").strip() + if toplevel: + store = git_scm.GitStore(toplevel, check) + if print_warnings: + git_scm.warn_experimental() + else: + store = None if not store: msg = f"Directory '{path}' is not a working copy"