Skip to content

Commit

Permalink
Fix local building in git projects
Browse files Browse the repository at this point in the history
osc did not find it's store and was unable to run a local build
in a project git
  • Loading branch information
adrianschroeter authored and dmach committed Nov 29, 2024
1 parent ae7ccdb commit 508ea4c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
17 changes: 11 additions & 6 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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:
Expand Down
28 changes: 27 additions & 1 deletion osc/git_scm/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
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:
Expand Down
20 changes: 13 additions & 7 deletions osc/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass

if not store:
try:
store = git_scm.GitStore(path, check)
if print_warnings:
git_scm.warn_experimental()
except oscerr.NoWorkingCopy:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass

if not store:
msg = f"Directory '{path}' is not a working copy"
Expand Down

0 comments on commit 508ea4c

Please sign in to comment.