@@ -127,6 +127,9 @@ class Repo(typing.NamedTuple):
127127
128128VENDOR_HEADER_SUFFIXES = (".h" , ".hpp" , ".inc" , ".def" )
129129
130+ # A ref pinned by commit rather than by tag.
131+ COMMIT_SHA = re .compile (r"[0-9a-f]{40}" )
132+
130133# The probe package this script injects into the extraction workspace.
131134PROBE_PACKAGE = "pv_extract"
132135
@@ -157,6 +160,12 @@ def run(cmd: list[str], cwd: Path, *, capture: bool = False) -> str:
157160 return ""
158161
159162
163+ def try_run (cmd : list [str ], cwd : Path ) -> bool :
164+ """Runs a command that is allowed to fail, reporting whether it worked."""
165+ print (f" $ { ' ' .join (cmd [:6 ])} { ' ...' if len (cmd ) > 6 else '' } " , flush = True )
166+ return subprocess .run (cmd , cwd = cwd , check = False ).returncode == 0
167+
168+
160169class Aquery :
161170 """Indexed view over one `bazel aquery --output=jsonproto` result."""
162171
@@ -767,22 +776,22 @@ def resolve_in_submodule(path: Path, ref: str) -> str:
767776 found = git (path , "rev-parse" , "--verify" , "--quiet" , candidate , check = False )
768777 if found :
769778 return found
770- # Not present locally yet so fetch first.
771- for fetch_args in (["origin" , "tag" , ref ], ["origin" , ref ]):
772- run (
779+ # Not present locally yet, so fetch first.
780+ fetches = [["origin" , ref ]]
781+ if not COMMIT_SHA .fullmatch (ref ):
782+ fetches .insert (0 , ["origin" , "tag" , ref ])
783+ for fetch_args in fetches :
784+ if not try_run (
773785 ["git" , "-C" , str (path ), "fetch" , "--depth" , "1" , * fetch_args ],
774786 cwd = REPO_ROOT ,
775- )
776- found = git (
777- path , "rev-parse" , "--verify" , "--quiet" , f"{ ref } ^{{commit}}" , check = False
778- )
779- if found :
780- return found
781- found = git (
782- path , "rev-parse" , "--verify" , "--quiet" , "FETCH_HEAD^{commit}" , check = False
783- )
784- if found :
785- return found
787+ ):
788+ continue
789+ for candidate in (f"{ ref } ^{{commit}}" , "FETCH_HEAD^{commit}" ):
790+ found = git (
791+ path , "rev-parse" , "--verify" , "--quiet" , candidate , check = False
792+ )
793+ if found :
794+ return found
786795 message = f"could not resolve { ref } in { path } "
787796 raise SystemExit (message )
788797
0 commit comments