Skip to content

Commit

Permalink
Fix a bug that occurs when checking out with a tag (#212)
Browse files Browse the repository at this point in the history
* Modify the git download func
* Update branch name for testing
* Remove default checkout version
---------

Signed-off-by: soim.kim <[email protected]>
Co-authored-by: jiyeong.seok <[email protected]>
  • Loading branch information
soimkim and dd-jy authored Feb 7, 2025
1 parent ccc3bcb commit 5f3b0c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
18 changes: 7 additions & 11 deletions src/fosslight_util/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
is_rubygems = src_info.get("rubygems", False)

# General download (git clone, wget)
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to,
tag, branch, ssh_key, id, git_token)
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir,
checkout_to,
tag, branch,
ssh_key, id, git_token)
link = change_ssh_link_to_https(link)
if (not is_rubygems) and (not success_git):
if os.path.isfile(target_dir):
Expand Down Expand Up @@ -205,27 +207,21 @@ def get_github_token(git_url):
def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
success = False
oss_version = ""
clone_default_branch_flag = False

logger.info(f"Download git url :{git_url}")
if refs_to_checkout:
try:
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
success = True
oss_version = refs_to_checkout
except GitCommandError as error:
logger.debug(f"Git checkout error:{error}")
success = False

if not success:
repo = Repo.clone_from(git_url, target_dir)
clone_default_branch_flag = True
Repo.clone_from(git_url, target_dir)
success = True

if refs_to_checkout != tag or clone_default_branch_flag:
oss_version = repo.active_branch.name
else:
oss_version = repo.git.describe('--tags')
return success, oss_version


Expand Down
16 changes: 8 additions & 8 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_download_from_github():


@pytest.mark.parametrize("git_url",
["git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;branch=ci-test",
["git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;branch=hash-stat2",
"git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;tag=v32"])
def test_download_from_github_with_branch_or_tag(git_url):
# given
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_download_git_clone_with_branch():
# given
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
branch_name = "ci-test"
branch_name = "hash-stat2"

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, "", "", branch_name)
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_download_main_branch_when_any_branch_or_tag_not_entered():
# given
git_url = "https://github.com/LGE-OSS/example"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
expected_oss_name = "main"
expected_oss_ver = ""

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir)
Expand All @@ -103,15 +103,15 @@ def test_download_main_branch_when_any_branch_or_tag_not_entered():
assert success is True
assert len(os.listdir(target_dir)) > 0
assert oss_name == 'LGE-OSS-example'
assert oss_version == expected_oss_name
assert oss_version == expected_oss_ver


def test_download_main_branch_when_non_existent_branch_entered():
# given
git_url = "https://github.com/LGE-OSS/example"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
branch_name = "non-existent-branch"
expected_oss_name = "main"
expected_oss_ver = ""

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, "", "", branch_name)
Expand All @@ -120,15 +120,15 @@ def test_download_main_branch_when_non_existent_branch_entered():
assert success is True
assert len(os.listdir(target_dir)) > 0
assert oss_name == 'LGE-OSS-example'
assert oss_version == expected_oss_name
assert oss_version == expected_oss_ver


def test_download_main_branch_when_non_existent_tag_entered():
# given
git_url = "https://github.com/LGE-OSS/example"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
tag_name = "non-existent-tag"
expected_oss_name = "main"
expected_oss_ver = ""

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, "", tag_name)
Expand All @@ -137,4 +137,4 @@ def test_download_main_branch_when_non_existent_tag_entered():
assert success is True
assert len(os.listdir(target_dir)) > 0
assert oss_name == 'LGE-OSS-example'
assert oss_version == expected_oss_name
assert oss_version == expected_oss_ver

0 comments on commit 5f3b0c4

Please sign in to comment.