diff --git a/src/centpkg/__init__.py b/src/centpkg/__init__.py index 1ed0757..588fc23 100644 --- a/src/centpkg/__init__.py +++ b/src/centpkg/__init__.py @@ -19,22 +19,31 @@ import re import warnings +import git from pyrpkg import Commands, rpkgError +from pyrpkg.utils import cached_property + # doc/centpkg_man_page.py uses the 'cli' import from . import cli # noqa from .lookaside import StreamLookasideCache, SIGLookasideCache -from pyrpkg.utils import cached_property + + +_DEFAULT_VERSION = '9' class DistGitDirectory(object): signame = None - centosversion = None + centosversion = _DEFAULT_VERSION projectname = None releasename = None distrobranch = False + repo = None + git_origin_substr = 'git@gitlab.com/redhat/centos-stream' - def __init__(self, branchtext): + def __init__(self, branchtext, repo_path=None): + if repo_path: + self.repo = git.cmd.Git(repo_path) sigtobranchre = r'c(?P\d+[s]?)-sig-(?P\w+)-?(?P\w+)?-?(?P\w+)?' distrobranchre = r'c(?P\d+)-?(?P\w+)?' oldbranchre = r'(?P\w+)(?P\d)' @@ -65,7 +74,34 @@ class DistGitDirectory(object): warnings.warn("This branch is deprecated and will be removed soon", DeprecationWarning) else: - raise ValueError("Branchname: {0} is not valid".format(branchtext)) + if not self.is_fork(): + raise ValueError("Branchname: {0} is not valid".format(branchtext)) + else: + self.distrobranch = True + self.signame = 'centos' + self.projectname = self.get_origin().split('_')[-1].replace('.git', '') + + warnings.warn('Remote "origin" was detected as a fork, ignoring branch name checking') + + def get_origin(self): + if self.repo is None: + return '' + return self.repo.execute('git config --get remote.origin.url'.split()) + + def is_fork(self): + """ + Check if origin remote repository is using a fork url. + + Returns + bool + A boolean flag indicating if origin remote url is using + a forked repository url. + """ + # git+ssh://git@gitlab.com/redhat/centos-stream/rpms/binutils.git + if self.repo is None: + return False + return self.git_origin_substr not in self.get_origin() + @property def target(self): @@ -105,7 +141,7 @@ class Commands(Commands): @property def distgitdir(self): - return DistGitDirectory(self.branch_merge) + return DistGitDirectory(self.branch_merge, repo_path=self.path) @cached_property def lookasidecache(self): diff --git a/tests/test_distgit.py b/tests/test_distgit.py index 1b921c8..5b1ad05 100644 --- a/tests/test_distgit.py +++ b/tests/test_distgit.py @@ -1,7 +1,10 @@ import unittest +import unittest.mock from .mixins import CatchWarningsMixin from centpkg import DistGitDirectory +from centpkg import git as centpkg_git + class TestDistGitNothing(unittest.TestCase): def test_distgit_emptystring(self): @@ -180,3 +183,21 @@ class TestOldGitBranch(unittest.TestCase, CatchWarningsMixin): with self.assertWarns(DeprecationWarning): branchstring = 'virt7' d = DistGitDirectory(branchstring) + +class TestIsFork(unittest.TestCase): + def setUp(self): + self.branchstring = 'c9s' + + def test_none(self): + d = DistGitDirectory(self.branchstring) + self.assertFalse(d.is_fork()) + + @unittest.mock.patch.object(centpkg_git.cmd.Git, 'execute', new=lambda s, c: 'git@gitlab.com:user/centos_rpms_binutils.git') + def test_fork_url(self): + d = DistGitDirectory(self.branchstring, 'binutils') + self.assertTrue(d.is_fork()) + + @unittest.mock.patch.object(centpkg_git.cmd.Git, 'execute', new=lambda s, c: 'git+ssh://git@gitlab.com/redhat/centos-stream/rpms/binutils.git') + def test_upstream_url(self): + d = DistGitDirectory(self.branchstring, 'binutils') + self.assertFalse(d.is_fork()) \ No newline at end of file