Skip to content

Commit

Permalink
Add a check for invalid path in get_repo_id function
Browse files Browse the repository at this point in the history
closes containerbuildsystem#653

Signed-off-by: Michal Šoltis <[email protected]>
  • Loading branch information
slimreaper35 committed Oct 17, 2024
1 parent 7eddec2 commit eeacd2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cachi2/core/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import NamedTuple, Union
from urllib.parse import ParseResult, SplitResult, urlparse, urlsplit

from git.exc import InvalidGitRepositoryError, NoSuchPathError
from git.repo import Repo

from cachi2.core.errors import FetchError, UnsupportedFeature
Expand Down Expand Up @@ -42,7 +43,15 @@ def get_repo_id(repo: Union[str, PathLike[str], Repo]) -> RepoID:
See `man git-clone` (GIT URLS) for some of the url formats that git supports.
"""
if isinstance(repo, (str, PathLike)):
repo = Repo(repo, search_parent_directories=True)
try:
repo = Repo(repo, search_parent_directories=True)
except (InvalidGitRepositoryError, NoSuchPathError):
raise UnsupportedFeature(
"Cachi2 cannot process provided path as a git repository",
solution=(
"Please ensure that the path is correct and that it is a valid git repository."
),
)

try:
origin = repo.remote("origin")
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def test_get_repo_id_no_origin(self, golang_repo_path: Path) -> None:
):
get_repo_id(golang_repo_path)

def test_get_repo_id_invalid_path(self, tmp_path: Path) -> None:
with pytest.raises(
UnsupportedFeature,
match="Cachi2 cannot process provided path as a git repository",
):
get_repo_id(tmp_path)

def test_as_vcs_url_qualifier(self) -> None:
origin_url = "ssh://[email protected]/foo/bar.git"
commit_id = "abcdef1234"
Expand Down

0 comments on commit eeacd2f

Please sign in to comment.