Skip to content

Commit 464021d

Browse files
committed
refactor download logic with gitPython
Signed-off-by: MoonJeWoong <[email protected]>
1 parent f08c626 commit 464021d

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

src/fosslight_util/download.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import logging
1111
import argparse
1212
import shutil
13-
import pygit2 as git
13+
from git import Repo
1414
import bz2
1515
import contextlib
1616
from datetime import datetime
@@ -230,14 +230,9 @@ def get_github_token(git_url):
230230

231231

232232
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
233-
ref_to_checkout = decide_checkout(checkout_to, tag, branch)
234-
msg = ""
235233
oss_name = get_github_ossname(git_url)
236-
oss_version = ""
237-
github_token = get_github_token(git_url)
238-
callbacks = None
239-
if github_token != "":
240-
callbacks = git.RemoteCallbacks(credentials=git.UserPass("foo", github_token)) # username is not used, so set to dummy
234+
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
235+
msg = ""
241236

242237
try:
243238
if platform.system() != "Windows":
@@ -248,30 +243,27 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
248243
alarm.start()
249244

250245
Path(target_dir).mkdir(parents=True, exist_ok=True)
251-
repo = git.clone_repository(git_url, target_dir,
252-
bare=False, repository=None,
253-
remote=None, callbacks=callbacks)
246+
if refs_to_checkout != "":
247+
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
248+
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
249+
logger.info(f"git checkout: {refs_to_checkout}")
250+
else:
251+
repo = Repo.clone_from(git_url, target_dir)
252+
253+
if refs_to_checkout != tag:
254+
oss_version = repo.active_branch.name
255+
else:
256+
oss_version = repo.git.describe('--tags')
257+
254258
if platform.system() != "Windows":
255259
signal.alarm(0)
256260
else:
257261
del alarm
258262
except Exception as error:
259263
logger.warning(f"git clone - failed: {error}")
260264
msg = str(error)
261-
return False, msg, oss_name, oss_version
262-
try:
263-
if ref_to_checkout != "":
264-
ref_list = [x for x in repo.references]
265-
ref_to_checkout = get_ref_to_checkout(ref_to_checkout, ref_list)
266-
logger.info(f"git checkout: {ref_to_checkout}")
267-
repo.checkout(ref_to_checkout)
265+
return False, msg, oss_name, refs_to_checkout
268266

269-
for prefix_ref in prefix_refs:
270-
if ref_to_checkout.startswith(prefix_ref):
271-
oss_version = ref_to_checkout[len(prefix_ref):]
272-
273-
except Exception as error:
274-
logger.warning(f"git checkout to {ref_to_checkout} - failed: {error}")
275267
return True, msg, oss_name, oss_version
276268

277269

tests/test_download.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import os
55
import pytest
66

7-
from fosslight_util.download import cli_download_and_extract
7+
from fosslight_util.download import cli_download_and_extract, download_git_clone
88
from tests import constants
9-
from git import Repo
109

1110

1211
def test_download_from_github():
@@ -57,3 +56,35 @@ def test_download_from_wget(project_name, project_url):
5756
# then
5857
assert success is True
5958
assert len(os.listdir(target_dir)) > 0
59+
60+
61+
def test_download_git_clone_with_branch():
62+
# given
63+
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
64+
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
65+
branch_name = "ci-test"
66+
67+
# when
68+
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, branch=branch_name)
69+
70+
# then
71+
assert success is True
72+
assert len(os.listdir(target_dir)) > 0
73+
assert oss_name == ''
74+
assert oss_version == branch_name
75+
76+
77+
def test_download_git_clone_with_tag():
78+
# given
79+
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
80+
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
81+
tag_name = "v32"
82+
83+
# when
84+
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, tag=tag_name)
85+
86+
# then
87+
assert success is True
88+
assert len(os.listdir(target_dir)) > 0
89+
assert oss_name == ''
90+
assert oss_version == tag_name

0 commit comments

Comments
 (0)