Skip to content

Commit 19888de

Browse files
authored
Merge pull request openSUSE#1974 from dmach/git-scm-store-check-parent-project-branch
Check that the git package has the same branch as the parent project
2 parents 6d8a8ad + f94787b commit 19888de

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

osc/git_scm/store.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import fnmatch
33
import json
44
import os
5-
from pathlib import Path
5+
import sys
66
import typing
77
import urllib.parse
8+
from pathlib import Path
89
from typing import Dict
910
from typing import List
1011
from typing import Optional
@@ -88,6 +89,8 @@ class LocalGitStore:
8889
It is not supposed to be used directly, it's a base class for GitStore that adds logic for resolving the values from multiple places.
8990
"""
9091

92+
_BRANCH_MISMATCH_WARNING_PRINTED = set()
93+
9194
@classmethod
9295
def is_project_dir(cls, path):
9396
try:
@@ -289,6 +292,23 @@ def assert_is_package(self):
289292
msg = f"Directory '{self.abspath}' is not a Git SCM working copy of a package"
290293
raise oscerr.NoWorkingCopy(msg)
291294

295+
if self.project_store and hasattr(self.project_store, "_git") and self.project_store._git.current_branch != self._git.current_branch:
296+
key = (self.project_store._git.current_branch, self._git.current_branch)
297+
if key not in self.__class__._BRANCH_MISMATCH_WARNING_PRINTED:
298+
from osc.output import tty
299+
300+
# print the warning only once and store the information in the class
301+
self.__class__._BRANCH_MISMATCH_WARNING_PRINTED.add(key)
302+
msg = (
303+
f"{tty.colorize('WARNING', 'yellow,bold')}: "
304+
"Git SCM package working copy is switched to a different branch than it's corresponding parent project\n"
305+
f" - Package branch: {self._git.current_branch}\n"
306+
f" - Project branch: {self.project_store._git.current_branch}\n"
307+
f" - Package path: {self.topdir}\n"
308+
f" - Project path: {self.project_store.topdir}"
309+
)
310+
print(msg, file=sys.stderr)
311+
292312
missing = []
293313
for name in ["apiurl", "project", "package"]:
294314
if not getattr(self, name):

tests/test_git_scm_store.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def tearDown(self):
9797
except OSError:
9898
pass
9999

100-
def _git_init(self, path, *, separate_git_dir=None):
100+
def _git_init(self, path, *, branch="factory", separate_git_dir=None):
101101
os.makedirs(path, exist_ok=True)
102102
git_init_cmd = ["git", "init", "-q"]
103103
if separate_git_dir:
@@ -106,7 +106,7 @@ def _git_init(self, path, *, separate_git_dir=None):
106106
subprocess.check_output(["git", "config", "user.email", "[email protected]"], cwd=path)
107107
subprocess.check_output(["git", "config", "user.name", "User Name"], cwd=path)
108108
subprocess.check_output(["git", "commit", "-m", "empty", "--allow-empty"], cwd=path)
109-
subprocess.check_output(["git", "checkout", "-b", "factory", "-q"], cwd=path)
109+
subprocess.check_output(["git", "checkout", "-b", branch, "-q"], cwd=path)
110110
subprocess.check_output(["git", "remote", "add", "origin", "https://example.com/packages/my-package.git"], cwd=path)
111111

112112
def _setup_project(self, path, *, apiurl="https://api.example.com", project=None):
@@ -350,6 +350,24 @@ def test_pkg_git_in_submodule(self):
350350
self.assertEqual(store.project, "PROJ")
351351
self.assertEqual(store.package, "pkg")
352352

353+
def test_project_with_different_branch(self):
354+
prj_path = os.path.join(self.tmpdir, "project")
355+
self._git_init(prj_path, branch="project-foo")
356+
manifest_data = {
357+
"obs_apiurl": "https://api.example.com",
358+
"obs_project": "PROJ",
359+
}
360+
361+
self._write(os.path.join(prj_path, "_manifest"), osc_yaml.yaml_dumps(manifest_data))
362+
363+
pkg_path = os.path.join(prj_path, "package")
364+
self._git_init(pkg_path, branch="project-bar")
365+
366+
stderr = io.StringIO()
367+
with contextlib.redirect_stderr(stderr):
368+
GitStore(pkg_path)
369+
self.assertIn("WARNING", stderr.getvalue())
370+
353371

354372
if __name__ == "__main__":
355373
unittest.main()

0 commit comments

Comments
 (0)