Skip to content

Commit 5f3b0c4

Browse files
soimkimdd-jy
andauthored
Fix a bug that occurs when checking out with a tag (#212)
* 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]>
1 parent ccc3bcb commit 5f3b0c4

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/fosslight_util/download.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
123123
is_rubygems = src_info.get("rubygems", False)
124124

125125
# General download (git clone, wget)
126-
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to,
127-
tag, branch, ssh_key, id, git_token)
126+
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir,
127+
checkout_to,
128+
tag, branch,
129+
ssh_key, id, git_token)
128130
link = change_ssh_link_to_https(link)
129131
if (not is_rubygems) and (not success_git):
130132
if os.path.isfile(target_dir):
@@ -205,27 +207,21 @@ def get_github_token(git_url):
205207
def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
206208
success = False
207209
oss_version = ""
208-
clone_default_branch_flag = False
209210

210211
logger.info(f"Download git url :{git_url}")
211212
if refs_to_checkout:
212213
try:
213214
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
214-
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
215+
Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
215216
success = True
217+
oss_version = refs_to_checkout
216218
except GitCommandError as error:
217219
logger.debug(f"Git checkout error:{error}")
218220
success = False
219221

220222
if not success:
221-
repo = Repo.clone_from(git_url, target_dir)
222-
clone_default_branch_flag = True
223+
Repo.clone_from(git_url, target_dir)
223224
success = True
224-
225-
if refs_to_checkout != tag or clone_default_branch_flag:
226-
oss_version = repo.active_branch.name
227-
else:
228-
oss_version = repo.git.describe('--tags')
229225
return success, oss_version
230226

231227

tests/test_download.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_download_from_github():
2323

2424

2525
@pytest.mark.parametrize("git_url",
26-
["git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;branch=ci-test",
26+
["git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;branch=hash-stat2",
2727
"git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;tag=v32"])
2828
def test_download_from_github_with_branch_or_tag(git_url):
2929
# given
@@ -62,7 +62,7 @@ def test_download_git_clone_with_branch():
6262
# given
6363
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
6464
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
65-
branch_name = "ci-test"
65+
branch_name = "hash-stat2"
6666

6767
# when
6868
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():
9494
# given
9595
git_url = "https://github.com/LGE-OSS/example"
9696
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
97-
expected_oss_name = "main"
97+
expected_oss_ver = ""
9898

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

108108

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

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

125125

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

133133
# when
134134
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():
137137
assert success is True
138138
assert len(os.listdir(target_dir)) > 0
139139
assert oss_name == 'LGE-OSS-example'
140-
assert oss_version == expected_oss_name
140+
assert oss_version == expected_oss_ver

0 commit comments

Comments
 (0)