diff --git a/pegleg/engine/util/git.py b/pegleg/engine/util/git.py index aafa7b15..7dbbf8ed 100644 --- a/pegleg/engine/util/git.py +++ b/pegleg/engine/util/git.py @@ -317,7 +317,9 @@ def is_repository(path, *args, **kwargs): :param str path: Directory path to check. :returns: True if ``path`` is a repo, else False. :rtype: boolean + """ + try: Repo(path, *args, **kwargs).git_dir return True @@ -337,6 +339,9 @@ def is_equal(first_repo, other_repo): """ + if not is_repository(first_repo) or not is_repository(other_repo): + return False + try: # Compare whether the first reference from each repository is the # same: by doing so we know the repositories are the same. @@ -360,6 +365,9 @@ def repo_name(repo_url_or_path): """ + if not is_repository(repo_url_or_path): + raise exceptions.GitConfigException(repo_url=repo_url_or_path) + repo = Repo(repo_url_or_path, search_parent_directories=True) config_reader = repo.config_reader() section = 'remote "origin"' @@ -367,7 +375,10 @@ def repo_name(repo_url_or_path): if config_reader.has_section(section): repo_url = config_reader.get_value(section, option) - return repo_url.split('/')[-1].split('.git')[0] + try: + return repo_url.split('/')[-1].split('.git')[0] + except Exception: + raise exceptions.GitConfigException(repo_url=repo_url_or_path) raise exceptions.GitConfigException(repo_url=repo_url_or_path) diff --git a/tests/unit/engine/util/test_git.py b/tests/unit/engine/util/test_git.py index d4b46f05..a4da2f9a 100644 --- a/tests/unit/engine/util/test_git.py +++ b/tests/unit/engine/util/test_git.py @@ -536,3 +536,33 @@ def test_is_repository(): def test_is_repository_negative(): assert not git.is_repository(tempfile.mkdtemp()) + + +@pytest.mark.skipif( + not is_connected(), reason='git clone requires network connectivity.') +def test_repo_name(): + url = "http://github.com/openstack/airship-pegleg" + git_dir = git.git_handler(url, ref="master") + _validate_git_clone(git_dir) + + name = git.repo_name(git_dir) + expected = "airship-pegleg" + assert name == expected + + +@pytest.mark.skipif( + not is_connected(), reason='git clone requires network connectivity.') +def test_is_equal(): + """Tests whether 2 repositories are equal => reference same remote repo.""" + + url = "http://github.com/openstack/airship-pegleg" + git_dir1 = git.git_handler(url, ref="master") + _validate_git_clone(git_dir1) + + # Re-clone the same repo using a different ref. + url = "http://github.com/openstack/airship-pegleg" + git_dir2 = git.git_handler(url, ref="refs/changes/40/604640/4") + _validate_git_clone(git_dir2) + + # Check whether both repos are equal. + assert git.is_equal(git_dir1, git_dir2)