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 6d91083 commit a6db8ad
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

Check warning on line 7272 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L7272

Added line #L7272 was not covered by tests
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

Check warning on line 7308 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L7303-L7308

Added lines #L7303 - L7308 were not covered by tests
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

Check warning on line 7737 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L7736-L7737

Added lines #L7736 - L7737 were not covered by tests
else:
store.assert_is_package()

Check warning on line 7739 in osc/commandline.py

View check run for this annotation

Codecov / codecov/patch

osc/commandline.py#L7739

Added line #L7739 was not covered by tests

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

Check warning on line 48 in osc/git_scm/store.py

View check run for this annotation

Codecov / codecov/patch

osc/git_scm/store.py#L47-L48

Added lines #L47 - L48 were not covered by tests
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()

Check warning on line 92 in osc/git_scm/store.py

View check run for this annotation

Codecov / codecov/patch

osc/git_scm/store.py#L92

Added line #L92 was not covered by tests
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()

Check warning on line 35 in osc/store.py

View check run for this annotation

Codecov / codecov/patch

osc/store.py#L34-L35

Added lines #L34 - L35 were not covered by tests
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 a6db8ad

Please sign in to comment.