Skip to content

Commit bc772a1

Browse files
committed
fix: avoid cold Homebrew dirty scan
1 parent 68b02c5 commit bc772a1

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

abxpkg/binprovider.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,8 +3401,64 @@ class EnvProvider(BinProvider):
34013401
"go": {
34023402
"version": ["go", "version"],
34033403
},
3404+
"brew": {
3405+
"version": "self.brew_version_handler",
3406+
},
34043407
}
34053408

3409+
def brew_version_handler(
3410+
self,
3411+
bin_name: BinName,
3412+
abspath: HostBinPath | None = None,
3413+
timeout: int | None = None,
3414+
**context,
3415+
) -> "VersionFuncReturnValue":
3416+
"""Read Homebrew's repository version without its expensive dirty scan."""
3417+
brew_candidate = abspath or self.get_abspath(bin_name, quiet=True)
3418+
if brew_candidate is None:
3419+
return None
3420+
3421+
brew_abspath = Path(brew_candidate)
3422+
brew_repository = brew_abspath.resolve().parent.parent
3423+
if not (brew_repository / ".git").exists():
3424+
return self.default_version_handler(
3425+
bin_name,
3426+
abspath=brew_candidate,
3427+
timeout=timeout,
3428+
)
3429+
3430+
git = EnvProvider(
3431+
install_root=self.install_root,
3432+
PATH=self.PATH,
3433+
postinstall_scripts=self.postinstall_scripts,
3434+
min_release_age=self.min_release_age,
3435+
).load("git")
3436+
if git is None or git.loaded_abspath is None:
3437+
return self.default_version_handler(
3438+
bin_name,
3439+
abspath=brew_candidate,
3440+
timeout=timeout,
3441+
)
3442+
3443+
proc = git.exec(
3444+
cmd=(
3445+
"-C",
3446+
brew_repository,
3447+
"describe",
3448+
"--tags",
3449+
"--abbrev=7",
3450+
),
3451+
timeout=self.version_timeout if timeout is None else timeout,
3452+
quiet=True,
3453+
)
3454+
if proc.returncode != 0:
3455+
return self.default_version_handler(
3456+
bin_name,
3457+
abspath=brew_candidate,
3458+
timeout=timeout,
3459+
)
3460+
return proc.stdout.strip() or proc.stderr.strip() or None
3461+
34063462
def setup_PATH(self, no_cache: bool = False) -> None:
34073463
"""Populate PATH lazily with install_root/bin ahead of the ambient PATH."""
34083464
if self.bin_dir is None and self.install_root is not None:

0 commit comments

Comments
 (0)