11import pathlib
22import posixpath
3+ import re
34from typing import ClassVar , Dict , Optional
45from urllib .parse import quote_plus , urlparse
56
@@ -14,7 +15,6 @@ def ls_branches(repo_url: str) -> Dict[str, str]:
1415 """List branches in remote git repo"""
1516 import git
1617
17- git .cmd .Git ().ls_remote (repo_url )
1818 g = git .cmd .Git ()
1919 remote_refs : Dict [str , str ] = dict (
2020 tuple (reversed (ref .split ("\t " )[:2 ]))
@@ -54,6 +54,10 @@ def _ls_github_refs(org: str, repo: str, endpoint: str):
5454 return None
5555
5656
57+ def is_long_sha (sha : str ):
58+ return re .match (r"^[a-f\d]{40}$" , sha )
59+
60+
5761class GithubResolver (CloudGitResolver ):
5862 """Resolve https://github.com URLs"""
5963
@@ -87,18 +91,23 @@ def get_kwargs(cls, uri):
8791 return {"org" : org , "repo" : repo , "path" : "" }
8892 if path [0 ] == "tree" :
8993 sha = path [1 ]
90- refs = ls_github_branches (org , repo )
91- refs .update (ls_github_tags (org , repo ))
92- branches = {quote_plus (k ) for k in refs }
93- # match beginning of path with one of existing branches
94- # "" is hack for cases with empty path (like 'github.com/org/rep/tree/branch/')
95- for i , part in enumerate (path [2 :] + ["" ], start = 2 ):
96- if sha in branches :
97- path = path [i :]
98- break
99- sha = f"{ sha } %2F{ part } "
94+ if is_long_sha (sha ):
95+ path = path [2 :]
10096 else :
101- raise ValueError (f'Could not resolve branch from uri "{ uri } "' )
97+ refs = ls_github_branches (org , repo )
98+ refs .update (ls_github_tags (org , repo ))
99+ branches = {quote_plus (k ) for k in refs }
100+ # match beginning of path with one of existing branches
101+ # "" is hack for cases with empty path (like 'github.com/org/rep/tree/branch/')
102+ for i , part in enumerate (path [2 :] + ["" ], start = 2 ):
103+ if sha in branches :
104+ path = path [i :]
105+ break
106+ sha = f"{ sha } %2F{ part } "
107+ else :
108+ raise ValueError (
109+ f'Could not resolve branch from uri "{ uri } "'
110+ )
102111 else :
103112 sha = None
104113 return {
0 commit comments