Skip to content

Commit 59b32b7

Browse files
committed
Fix to prevent prompt when calling git clone with API
Signed-off-by: 석지영/책임연구원/SW공학(연)Open Source TP <[email protected]>
1 parent d08bc1b commit 59b32b7

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/fosslight_util/download.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ def parse_src_link(src_link):
9595

9696
def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_to: str = "",
9797
compressed_only: bool = False, ssh_key: str = "",
98-
id: str = "", git_token: str = "") -> Tuple[bool, str, str, str]:
98+
id: str = "", git_token: str = "",
99+
called_cli: bool = True) -> Tuple[bool, str, str, str]:
99100
global logger
100101

101102
success = True
@@ -127,7 +128,8 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
127128
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir,
128129
checkout_to,
129130
tag, branch,
130-
ssh_key, id, git_token)
131+
ssh_key, id, git_token,
132+
called_cli)
131133
link = change_ssh_link_to_https(link)
132134
if (not is_rubygems) and (not success_git):
133135
if os.path.isfile(target_dir):
@@ -205,28 +207,44 @@ def get_github_token(git_url):
205207
return github_token
206208

207209

208-
def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
210+
def download_git_repository(refs_to_checkout, git_url, target_dir, tag, called_cli=True):
209211
success = False
210212
oss_version = ""
211213

212214
logger.info(f"Download git url :{git_url}")
215+
env = os.environ.copy()
216+
if not called_cli:
217+
env["GIT_TERMINAL_PROMPT"] = "0"
213218
if refs_to_checkout:
214219
try:
215220
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
216-
Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
217-
success = True
218-
oss_version = refs_to_checkout
221+
Repo.clone_from(git_url, target_dir, branch=refs_to_checkout, env=env)
222+
if any(Path(target_dir).iterdir()):
223+
success = True
224+
oss_version = refs_to_checkout
225+
logger.info(f"Files found in {target_dir} after clone.")
226+
else:
227+
logger.info(f"No files found in {target_dir} after clone.")
228+
success = False
219229
except GitCommandError as error:
220-
logger.debug(f"Git checkout error:{error}")
230+
logger.info(f"Git checkout error:{error}")
231+
success = False
232+
except Exception as e:
233+
logger.info(f"Repo.clone_from error:{e}")
221234
success = False
222235

223236
if not success:
224-
Repo.clone_from(git_url, target_dir)
225-
success = True
237+
Repo.clone_from(git_url, target_dir, env=env)
238+
if any(Path(target_dir).iterdir()):
239+
success = True
240+
else:
241+
logger.info(f"No files found in {target_dir} after clone.")
242+
success = False
226243
return success, oss_version
227244

228245

229-
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key="", id="", git_token=""):
246+
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="",
247+
ssh_key="", id="", git_token="", called_cli=True):
230248
oss_name = get_github_ossname(git_url)
231249
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
232250
msg = ""
@@ -250,7 +268,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
250268
logger.info(f"Download git with ssh_key:{git_url}")
251269
git_ssh_cmd = f'ssh -i {ssh_key}'
252270
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
253-
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
271+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag, called_cli)
254272
else:
255273
if id and git_token:
256274
try:
@@ -262,7 +280,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
262280
git_url = git_url.replace(protocol, f"{protocol}{encoded_id}:{encoded_git_token}@")
263281
except Exception as error:
264282
logger.info(f"Failed to insert id, token to git url:{error}")
265-
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
283+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag, called_cli)
266284

267285
logger.info(f"git checkout: {oss_version}")
268286
refs_to_checkout = oss_version

0 commit comments

Comments
 (0)