diff --git a/src/fosslight_util/download.py b/src/fosslight_util/download.py index 4a6e1bd..228b20c 100755 --- a/src/fosslight_util/download.py +++ b/src/fosslight_util/download.py @@ -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): @@ -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 diff --git a/tests/test_download.py b/tests/test_download.py index 4a45979..cc9982d 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -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 @@ -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) @@ -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) @@ -103,7 +103,7 @@ 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(): @@ -111,7 +111,7 @@ def test_download_main_branch_when_non_existent_branch_entered(): 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) @@ -120,7 +120,7 @@ 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(): @@ -128,7 +128,7 @@ def test_download_main_branch_when_non_existent_tag_entered(): 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) @@ -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