Skip to content

Commit 481ecdf

Browse files
committed
Print branch name if checkout fails
1 parent 7c99357 commit 481ecdf

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/fosslight_util/download.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
102102
msg_wget = ""
103103
oss_name = ""
104104
oss_version = ""
105-
clone_default_branch_flag = False
106105
log_file_name = "fosslight_download_" + \
107106
datetime.now().strftime('%Y%m%d_%H-%M-%S')+".txt"
108107
logger, log_item = init_log(os.path.join(log_dir, log_file_name))
@@ -124,10 +123,10 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
124123
is_rubygems = src_info.get("rubygems", False)
125124

126125
# General download (git clone, wget)
127-
success_git, msg, oss_name, oss_version, clone_default_branch_flag = download_git_clone(link, target_dir,
128-
checkout_to,
129-
tag, branch,
130-
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)
131130
link = change_ssh_link_to_https(link)
132131
if (not is_rubygems) and (not success_git):
133132
if os.path.isfile(target_dir):
@@ -154,7 +153,7 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
154153
msg = str(error)
155154

156155
logger.info(f"\n* FOSSLight Downloader - Result: {success} ({msg})")
157-
return success, msg, oss_name, oss_version, clone_default_branch_flag
156+
return success, msg, oss_name, oss_version
158157

159158

160159
def get_ref_to_checkout(checkout_to, ref_list):
@@ -208,37 +207,28 @@ def get_github_token(git_url):
208207
def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
209208
success = False
210209
oss_version = ""
211-
clone_default_branch_flag = False
212210

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

223222
if not success:
224223
repo = Repo.clone_from(git_url, target_dir)
225-
clone_default_branch_flag = True
226224
success = True
227-
try:
228-
if clone_default_branch_flag:
229-
oss_version = repo.active_branch.name
230-
else:
231-
oss_version = repo.git.describe('--tags')
232-
except Exception as error:
233-
logger.debug(f"Get tag/branch error:{error}")
234-
235-
return success, oss_version, clone_default_branch_flag
225+
oss_version = repo.active_branch.name
226+
return success, oss_version
236227

237228

238229
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key="", id="", git_token=""):
239230
oss_name = get_github_ossname(git_url)
240231
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
241-
clone_default_branch_flag = False
242232
msg = ""
243233
success = True
244234

@@ -260,7 +250,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
260250
logger.info(f"Download git with ssh_key:{git_url}")
261251
git_ssh_cmd = f'ssh -i {ssh_key}'
262252
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
263-
success, oss_version, clone_default_branch_flag = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
253+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
264254
else:
265255
if id and git_token:
266256
try:
@@ -270,7 +260,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
270260
git_url = git_url.replace(protocol, f"{protocol}{id}:{git_token}@")
271261
except Exception as error:
272262
logger.info(f"Failed to insert id, token to git url:{error}")
273-
success, oss_version, clone_default_branch_flag = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
263+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
274264

275265
logger.info(f"git checkout: {oss_version}")
276266
refs_to_checkout = oss_version
@@ -284,7 +274,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
284274
logger.warning(f"git clone - failed: {error}")
285275
msg = str(error)
286276

287-
return success, msg, oss_name, refs_to_checkout, clone_default_branch_flag
277+
return success, msg, oss_name, refs_to_checkout
288278

289279

290280
def download_wget(link, target_dir, compressed_only):

0 commit comments

Comments
 (0)