Skip to content

Commit f607298

Browse files
authored
Add tag/branch resolution via ls-remote (#233)
Signed-off-by: 석지영/책임연구원/SW공학(연)Open Source TP <[email protected]>
1 parent 065aa6d commit f607298

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

src/fosslight_util/download.py

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,60 @@ def get_ref_to_checkout(checkout_to, ref_list):
195195
return ref_to_checkout
196196

197197

198-
def decide_checkout(checkout_to="", tag="", branch=""):
199-
if checkout_to:
200-
ref_to_checkout = checkout_to
201-
else:
202-
if branch:
203-
ref_to_checkout = branch
204-
else:
205-
ref_to_checkout = tag
206-
return ref_to_checkout
198+
def get_remote_refs(git_url: str):
199+
if not git_url:
200+
return {"tags": [], "branches": []}
201+
tags = []
202+
branches = []
203+
try:
204+
cp = subprocess.run(["git", "ls-remote", "--tags", "--heads", git_url], capture_output=True, text=True, timeout=30)
205+
if cp.returncode == 0:
206+
for line in cp.stdout.splitlines():
207+
parts = line.split('\t')
208+
if len(parts) != 2:
209+
continue
210+
ref = parts[1]
211+
if ref.startswith('refs/tags/'):
212+
tags.append(ref[len('refs/tags/'):])
213+
elif ref.startswith('refs/heads/'):
214+
branches.append(ref[len('refs/heads/'):])
215+
except Exception as e:
216+
logger.debug(f"get_remote_refs - failed: {e}")
217+
return {"tags": tags, "branches": branches}
218+
219+
220+
def decide_checkout(checkout_to="", tag="", branch="", git_url=""):
221+
base = checkout_to or tag or branch
222+
if not base:
223+
return ""
224+
225+
ref_dict = get_remote_refs(git_url)
226+
tag_set = set(ref_dict.get("tags", []))
227+
branch_set = set(ref_dict.get("branches", []))
228+
229+
ver_re = re.compile(r'^(?:v\.? ?)?' + re.escape(base) + r'$', re.IGNORECASE)
230+
231+
# tag: exact -> prefix variant -> endswith
232+
if base in tag_set:
233+
return base
234+
tag_candidates = [c for c in tag_set if ver_re.match(c)]
235+
if tag_candidates:
236+
return min(tag_candidates, key=lambda x: (len(x), x.lower()))
237+
tag_ends = [n for n in tag_set if n.endswith(base)]
238+
if tag_ends:
239+
return min(tag_ends, key=len)
240+
241+
# branch: exact -> prefix variant -> endswith
242+
if base in branch_set:
243+
return base
244+
branch_candidates = [c for c in branch_set if ver_re.match(c)]
245+
if branch_candidates:
246+
return min(branch_candidates, key=lambda x: (len(x), x.lower()))
247+
branch_ends = [n for n in branch_set if n.endswith(base)]
248+
if branch_ends:
249+
return min(branch_ends, key=len)
250+
251+
return base
207252

208253

209254
def get_github_ossname(link):
@@ -263,7 +308,7 @@ def download_git_repository(refs_to_checkout, git_url, target_dir, tag, called_c
263308
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="",
264309
ssh_key="", id="", git_token="", called_cli=True):
265310
oss_name = get_github_ossname(git_url)
266-
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
311+
refs_to_checkout = decide_checkout(checkout_to, tag, branch, git_url)
267312
msg = ""
268313
success = True
269314

0 commit comments

Comments
 (0)