Skip to content

Commit

Permalink
Merge pull request #240 from hatrg/enhance-get-repo-name-by-url-method
Browse files Browse the repository at this point in the history
enhance method _get_repo_name_from_url to handle case when .git is part of the repo name
  • Loading branch information
ishepard committed Dec 7, 2022
2 parents 9271160 + ff7c6de commit 1dd9606
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pydriller/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,18 @@ def _split_in_chunks(full_list: List[Commit], num_workers: int) -> List[List[Com
@staticmethod
def _get_repo_name_from_url(url: str) -> str:
last_slash_index = url.rfind("/")
last_suffix_index = url.rfind(".git")
if last_suffix_index < 0:
last_suffix_index = len(url)
len_url = len(url)

if last_slash_index < 0 or last_suffix_index <= last_slash_index:
if last_slash_index < 0 or last_slash_index >= len_url - 1:
raise MalformedUrl(f"Badly formatted url {url}")

last_dot_index = url.rfind(".")

if url[last_dot_index:] == ".git":
last_suffix_index = last_dot_index
else:
last_suffix_index = len_url

return url[last_slash_index + 1:last_suffix_index]


Expand Down
19 changes: 19 additions & 0 deletions tests/test_repository_mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ def test_projectname_multiple_repos_remote():
assert commit.project_name == 'pydriller'


def test_get_repo_name_from_url():
# with .git in the middle of the name
url_set_a = [
"https://github.com/academicpages/academicpages.github.io",
"https://github.com/academicpages/academicpages.github.io.git",
]

url_set_b = [
"https://github.com/ishepard/pydriller",
"https://github.com/ishepard/pydriller.git",
]

for url in url_set_a:
assert Repository._get_repo_name_from_url(url) == "academicpages.github.io"

for url in url_set_b:
assert Repository._get_repo_name_from_url(url) == "pydriller"


@pytest.mark.skipif(sys.version_info < (3, 8) and sys.platform == "win32", reason="requires Python3.8 or greater on Windows")
def test_deletion_remotes():
repos = [
Expand Down

0 comments on commit 1dd9606

Please sign in to comment.