gitlock.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. """Stale git lock-file and aborted-fetch pack-debris recovery for update/check paths.
  2. A killed ``git fetch`` can leave ``.git/shallow.lock`` behind (every later fetch fails with "Unable to
  3. create '.../shallow.lock': File exists") and ``tmp_pack_*`` files git itself never cleans up."""
  4. from __future__ import annotations
  5. import logging
  6. import os
  7. import re
  8. import subprocess
  9. import time
  10. from pathlib import Path
  11. from typing import Callable, Iterable, List, Optional
  12. from hermes_cli._subprocess_compat import (
  13. NO_LAZY_FETCH_ENV,
  14. bounded_probe_run,
  15. noninteractive_git_env,
  16. windows_hide_flags,
  17. )
  18. from hermes_cli.update_custody import run_git
  19. logger = logging.getLogger(__name__)
  20. # Folding ~100 small packs takes seconds, but the checkouts this exists for (thousands of packs,
  21. # tens of GiB) need a full repack. A killed fold restarts from scratch on every update and never
  22. # converges, so the bound is generous and a timeout is reported, not swallowed.
  23. # Files younger than this are presumed live (a fetch may be in flight) and are never removed. Lock
  24. # files live for seconds and a healthy fetch completes in minutes; 10 minutes is abandoned.
  25. STALE_LOCK_MIN_AGE_SECONDS = 10 * 60
  26. STALE_TMP_PACK_MIN_AGE_SECONDS = STALE_LOCK_MIN_AGE_SECONDS
  27. # ``shallow.lock`` is the one observed in the wild; the others are the same class of failure
  28. # (interrupted git operation). Locks held by a live git process are protected by the process guard.
  29. LOCK_NAMES = ("shallow.lock", "index.lock", "HEAD.lock", "MERGE_HEAD.lock")
  30. # Object-store indexes a killed git (Hermes kills git trees on timeout) leaves locked. Each lock fails
  31. # every later write of that file, so the commit-graph that keeps ``git tag --merged`` and ancestry
  32. # walks fast stops refreshing for good (one sat on a real checkout for two months).
  33. OBJECT_STORE_LOCK_NAMES = (
  34. "objects/info/commit-graph.lock",
  35. "objects/info/commit-graphs/commit-graph-chain.lock",
  36. "objects/pack/multi-pack-index.lock",
  37. "objects/pack/multi-pack-index.d/multi-pack-index-chain.lock",
  38. )
  39. # Temp-file prefixes git writes into .git/objects/pack during a transfer and renames away on
  40. # success; anything left with these names after a fetch died is garbage by definition.
  41. # ``.tmp-<pid>-pack*`` is the same thing from ``pack-objects`` (repack/gc) killed mid-write.
  42. _TMP_PACK_PREFIXES = ("tmp_pack_", "tmp_idx_", "tmp_rev_", "tmp_mtimes_", ".tmp-")
  43. # A split commit-graph write killed before its rename strands this in objects/info/commit-graphs.
  44. _TMP_GRAPH_PREFIX = "tmp_graph_"
  45. # pgrep's name pattern for every form git runs as: ``git``, the dashed executables it ships and
  46. # launches (git-core's ``git-commit-graph``, ``git-repack``; comm is cut to 15 chars), ``git.exe``.
  47. _PGREP_GIT = r"^git(-.*|\.exe)?$"
  48. def _git_proc_running() -> bool:
  49. """Whether a git process may be running: the check that stops us yanking a lock a live git holds.
  50. Fails closed: a probe that cannot tell (pgrep missing or failing, tasklist failing, a process
  51. whose name psutil may not read) answers True and the sweep removes nothing. The age floor is
  52. no proof on its own: a stopped or slow writer's lock ages like an abandoned one.
  53. """
  54. if os.name != "nt":
  55. try:
  56. import psutil
  57. except ImportError: # the dependency-free bootstrap interpreter
  58. pass
  59. else:
  60. return _psutil_git_running(psutil)
  61. from hermes_cli._early_recovery import _git_program
  62. try:
  63. # One shared spawn for both probes: the argv is the platform fork, the
  64. # hide-flags wiring is common, so a console-less parent (pythonw backend)
  65. # never flashes a console for either (#117781).
  66. argv = ["tasklist", "/FO", "CSV", "/NH"] if os.name == "nt" else ["pgrep", _PGREP_GIT]
  67. proc = subprocess.run(argv, capture_output=True, text=True, encoding="utf-8", errors="replace",
  68. timeout=10, creationflags=windows_hide_flags(), check=False)
  69. except Exception: # health: allow BLE001 -- whatever stops the probe, the locks stay
  70. logger.debug("git process probe failed; assuming git may be running", exc_info=True)
  71. return True
  72. if os.name == "nt":
  73. return proc.returncode != 0 or any(
  74. _git_program(line.split(",", 1)[0].strip('"')) for line in proc.stdout.splitlines())
  75. return proc.returncode != 1 # 1 is pgrep's "no match"; 2 and 3 mean it could not tell
  76. def _psutil_git_running(psutil) -> bool:
  77. from hermes_cli._early_recovery import _git_program
  78. try:
  79. for proc in psutil.process_iter():
  80. try:
  81. if _git_program(proc.name()) is not None:
  82. return True
  83. except psutil.NoSuchProcess: # exited between the listing and the read
  84. continue
  85. except Exception: # health: allow BLE001 -- AccessDenied or a failed scan keeps the locks
  86. logger.debug("git process scan failed; assuming git may be running", exc_info=True)
  87. return True
  88. return False
  89. def _sweep_stale(directory: Path, candidates: Callable[[], Iterable[Path]], *, min_age_seconds: Optional[int],
  90. default_age: int, skip_msg: str, log_removed: Callable[[Path, int], None]) -> list[str]:
  91. """Shared guard + age-floor sweep. Never raises; skips anything it cannot stat/unlink."""
  92. if not directory.is_dir():
  93. return []
  94. if _git_proc_running():
  95. logger.debug(skip_msg)
  96. return []
  97. cutoff = time.time() - (min_age_seconds if min_age_seconds is not None else default_age)
  98. removed: list[str] = []
  99. for entry in candidates():
  100. try:
  101. if entry.is_file() and (st := entry.stat()).st_mtime < cutoff:
  102. if os.name == "nt":
  103. # git renames its transfer temps into place read-only; Windows refuses to
  104. # unlink a read-only file (EACCES/13), so without clearing the write bit
  105. # this sweep silently removes nothing on real debris (#116384).
  106. try:
  107. os.chmod(entry, 0o666)
  108. except OSError:
  109. pass
  110. entry.unlink()
  111. removed.append(str(entry))
  112. log_removed(entry, st.st_size)
  113. except OSError as exc:
  114. # A cleaner that fails silently is worse than none: debug-level skips hid the
  115. # Windows read-only unlink failure for months while debris grew to gigabytes.
  116. logger.warning("Could not clear %s (skipping): %s", entry, exc)
  117. return removed
  118. def _sweep_locks(git_dir: Path, names: Iterable[str], min_age_seconds: Optional[int]) -> list[str]:
  119. return _sweep_stale(
  120. git_dir, lambda: [git_dir / name for name in names],
  121. min_age_seconds=min_age_seconds, default_age=STALE_LOCK_MIN_AGE_SECONDS,
  122. skip_msg="git process running; skipping stale-lock sweep",
  123. log_removed=lambda p, _size: logger.info("Removed stale git lock %s", p),
  124. )
  125. def clear_stale_git_locks(repo_root: Path, *, min_age_seconds: Optional[int] = None) -> list[str]:
  126. """Remove abandoned ``.git`` lock files under ``repo_root``; returns the removed paths.
  127. Removes only when older than the age floor AND no git process is running. Never raises: a lock we cannot
  128. stat/unlink is skipped (it may have been re-created between the age check and the unlink; skipping is safe).
  129. """
  130. git_dir = Path(repo_root) / ".git"
  131. return _sweep_stale(
  132. git_dir, lambda: [*(git_dir / name for name in (*LOCK_NAMES, *OBJECT_STORE_LOCK_NAMES)),
  133. *ref_lock_files(git_dir)],
  134. min_age_seconds=min_age_seconds, default_age=STALE_LOCK_MIN_AGE_SECONDS,
  135. skip_msg="git process running; skipping stale-lock sweep",
  136. log_removed=lambda p, _size: logger.info("Removed stale git lock %s", p),
  137. )
  138. def clear_stale_object_store_locks(git_dir: Path) -> list[str]:
  139. """``OBJECT_STORE_LOCK_NAMES`` only, under ``git_dir`` itself (a linked worktree's or submodule's
  140. common dir), for repos Hermes does not own: index.lock / HEAD.lock there are the user's git's."""
  141. return _sweep_locks(Path(git_dir), OBJECT_STORE_LOCK_NAMES, None)
  142. def release_dead_index_lock(repo_root: Path) -> bool:
  143. """Drop ``.git/index.lock`` when the git that took it is proven gone, whatever its age.
  144. A git killed while it held the index lock (an update's tree killed by the user, a probe killed
  145. by its own timeout) leaves it behind, and every later merge/stash/reset refuses with "File
  146. exists". The age floor above keeps such a lock for 10 minutes, so the next ``hermes update``
  147. died at its fast-forward (#132089; the Windows crash cell ``mid_fetch``).
  148. Two conditions, both required. A killed update run must be behind the lock
  149. (:func:`_killed_update_owns`): necessary, never sufficient, since a user's ``git commit`` can take
  150. the lock after that run died. And no git of ANY form may be working in this checkout: a
  151. ``git commit`` waiting in the editor has closed its lock fd, so an absent fd proves nothing, and
  152. it runs as ``git``, a dashed ``git-commit``, an alias or ``git.exe``. Linux reads every process's
  153. cwd, path arguments and ``GIT_DIR`` through /proc (``_early_recovery._held_open`` with
  154. ``any_git``); macOS keeps the lock while any git runs (``ps`` cannot say where); Windows scans
  155. with psutil (:func:`_windows_git_in_checkout`). Whatever cannot be read keeps the lock. An
  156. interrupted tree move owns its own lock judgement, so its marker defers to that repair.
  157. """
  158. return bool(_release_killed_update_locks(Path(repo_root), lambda git_dir: [git_dir / "index.lock"]))
  159. def ref_lock_files(git_dir: Path) -> list[Path]:
  160. """The lock files of git's ref transactions: ``packed-refs.lock`` and every ``refs/**/*.lock``."""
  161. refs = git_dir / "refs"
  162. return [git_dir / "packed-refs.lock", *(sorted(refs.rglob("*.lock")) if refs.is_dir() else ())]
  163. def release_dead_fetch_locks(repo_root: Path) -> list[str]:
  164. """Drop the locks a killed update's fetch left, whatever their age: ``shallow.lock`` and the ref
  165. locks (:func:`ref_lock_files`).
  166. The update's ``git fetch`` dies with its tree (``taskkill /T /F``, the Linux parent-death signal),
  167. so a kill inside its ref transaction strands ``refs/remotes/origin/<branch>.lock``. Git (2.51+)
  168. then rejects that ref on every later fetch as "fetching ref refs/remotes/origin/main failed:
  169. reference already exists" (its batched fetch files a lock it cannot create under that reason),
  170. so no update could fetch again until a user found and deleted the file (the Windows crash cell
  171. ``mid_fetch``). A shallow fetch (installer checkouts are ``--depth 1``) killed in its pack
  172. transfer strands ``shallow.lock`` the same way ("File exists"); a live :class:`_ShallowLock`
  173. holder keeps it open, so the proofs see it. The same two proofs as
  174. :func:`release_dead_index_lock`; returns the removed paths.
  175. """
  176. return _release_killed_update_locks(Path(repo_root), lambda git_dir: [git_dir / "shallow.lock",
  177. *ref_lock_files(git_dir)])
  178. def _release_killed_update_locks(root: Path, candidates: Callable[[Path], Iterable[Path]]) -> list[str]:
  179. from hermes_cli._early_recovery import _git_dir, _release_dead_lock, interrupted_pull_marker
  180. git_dir = _git_dir(root)
  181. if interrupted_pull_marker(root).exists():
  182. return []
  183. locks = [lock for lock in candidates(git_dir) if lock.exists() and _killed_update_owns(lock)]
  184. if not locks or (os.name == "nt" and _windows_git_in_checkout(root) is not False):
  185. return []
  186. released: list[str] = []
  187. for lock in locks:
  188. if not _release_dead_lock(lock, root, any_git=True):
  189. break # a git works in the checkout: every lock stays
  190. released.append(str(lock))
  191. return released
  192. def _receipt_time(value: object) -> Optional[float]:
  193. from datetime import datetime
  194. try:
  195. return datetime.fromisoformat(str(value)).timestamp()
  196. except (TypeError, ValueError):
  197. return None
  198. def _killed_update_owns(lock: Path) -> bool:
  199. """The last update run that started before ``lock`` was written died unfinished, its processes gone.
  200. Read from the durable per-run records (read-only), not only ``latest.json``: a retry that kept
  201. the then-live lock and failed replaced ``latest.json``, and the killed run's evidence must
  202. still stand once the lock's git is gone. A run that started after the lock was written cannot
  203. have left it, so a later run never discharges an earlier killed one.
  204. """
  205. import json
  206. from hermes_cli import update_receipt
  207. try:
  208. written = lock.stat().st_mtime
  209. except OSError:
  210. return False
  211. records: list[dict] = []
  212. try:
  213. for directory in update_receipt._receipt_dirs():
  214. for path in directory.glob("update_*.json"):
  215. try:
  216. record = json.loads(path.read_text(encoding="utf-8-sig"))
  217. except (OSError, ValueError):
  218. continue
  219. if isinstance(record, dict):
  220. records.append(record)
  221. except OSError:
  222. pass
  223. latest = update_receipt.read_latest_receipt()
  224. if isinstance(latest, dict):
  225. records.append(latest) # a store from before per-run records
  226. runs: dict[str, tuple[float, dict]] = {}
  227. for record in records:
  228. started = _receipt_time(record.get("started_at"))
  229. if started is None or started > written:
  230. continue
  231. key = str(record.get("update_id") or id(record))
  232. held = runs.get(key)
  233. # One run, several copies (its archive, latest.json, a profile mirror): a terminal copy wins.
  234. if held is None or (held[1].get("outcome") in ("running", "interrupted")
  235. and record.get("outcome") not in ("running", "interrupted")):
  236. runs[key] = (started, record)
  237. if not runs:
  238. return False
  239. _started, last = max(runs.values(), key=lambda run: run[0])
  240. return last.get("outcome") in ("running", "interrupted") and not update_receipt._owner_alive(last)
  241. def _windows_git_in_checkout(root: Path) -> bool | None:
  242. """Whether any git (``git.exe``, a dashed ``git-<sub>.exe``) works in ``root``; None when unknowable.
  243. Works in: its cwd, a path argument or a ``GIT_DIR``-style variable is inside the checkout or
  244. its git dir, compared by path components (``hermes-backup`` is not inside ``hermes``). Scoped to
  245. the checkout: a system-wide check would keep the lock whenever any editor or terminal elsewhere
  246. runs git, which on Windows is most of the time. A git whose cwd cannot be read keeps the lock.
  247. """
  248. from hermes_cli._early_recovery import _NEVER_LOCKS, _checkout_places, _git_dir, _git_program, \
  249. _git_subcommand_of, git_works_in
  250. try:
  251. import psutil
  252. except ImportError:
  253. return None
  254. places = _checkout_places(root, _git_dir(Path(root)))
  255. try:
  256. for proc in psutil.process_iter(["pid", "name", "cwd", "cmdline", "environ"]):
  257. info = proc.info
  258. if info.get("pid") == os.getpid():
  259. continue
  260. args = [str(a) for a in info.get("cmdline") or () if a]
  261. if _git_program(info.get("name") or "") is None and not (args and _git_program(args[0])):
  262. continue
  263. if _git_subcommand_of(args or [info.get("name") or ""]) in _NEVER_LOCKS:
  264. continue
  265. cwd = info.get("cwd")
  266. if git_works_in(args or [info.get("name") or "git"], cwd, info.get("environ"), places):
  267. return True
  268. if not cwd and proc.is_running():
  269. return None # a git we cannot inspect may be working here
  270. except Exception: # health: allow BLE001 -- a failed scan keeps the lock, never drops it
  271. return None
  272. return False
  273. def _pack_dir(repo_root: Path) -> Path:
  274. git_dir = Path(repo_root) / ".git"
  275. return (git_dir if git_dir.is_dir() else Path(repo_root)) / "objects" / "pack"
  276. def mark_unmarked_packs_promisor(repo_root: Path) -> int:
  277. """Give every pack without a ``.promisor`` marker one; returns how many were marked.
  278. git 2.53+ ``index-pack --promisor`` hands objects that sit outside promisor packs to
  279. pack-objects, which BUG()s on the missing objects they lead to (#124272). Unmarked packs in a
  280. partial clone come from a filtered fetch that turned a full or depth-limited clone partial (the
  281. old packs keep no marker) and from checkouts that lost their markers. The marker only tells git
  282. that objects those packs link to may be fetched on demand; nothing is rewritten or deleted.
  283. """
  284. marked = 0
  285. for pack in _pack_dir(repo_root).glob("pack-*.pack"):
  286. marker = pack.with_suffix(".promisor")
  287. try:
  288. with open(marker, "xb"):
  289. marked += 1
  290. except FileExistsError:
  291. pass
  292. except OSError as exc:
  293. # A read-only store must not turn a fetch failure into a traceback.
  294. logger.warning("Could not mark %s as a partial-clone pack: %s", pack, exc)
  295. if marked:
  296. logger.info("Marked %d pack(s) in %s as partial-clone packs", marked, repo_root)
  297. return marked
  298. # In a partial clone whose commit-graph already carries changed-path (Bloom) data, any commit-graph
  299. # write over commits it has not seen yet needs their trees: one lazy fetch (and one pack) per commit,
  300. # and each lazy fetch spawns ``git maintenance`` again, which is the unbounded loop of #127711. gc,
  301. # ``maintenance run --task=commit-graph`` and ``fetch.writeCommitGraph`` all write one (git
  302. # 2.50.1, 2.53.0 and 2.55.0 alike). So a promisor checkout never writes the graph. Automatic
  303. # maintenance itself stays on: on git <= 2.53 its post-fetch ``gc --auto`` is what keeps the
  304. # lazy-fetch packs folded between updates. ``gc.auto`` stays at its default for the same reason
  305. # (``gc.auto=0`` turns that fold into a no-op).
  306. _TREE0_MAINTENANCE_OFF = (
  307. ("maintenance.commit-graph.enabled", "false"),
  308. ("gc.writeCommitGraph", "false"),
  309. ("fetch.writeCommitGraph", "false"),
  310. )
  311. def disable_tree0_auto_maintenance(repo_root: Path) -> None:
  312. """Keep git from writing a commit-graph in a partial clone, from gc, fetch or maintenance.
  313. Idempotent (a value already in place is not rewritten, so concurrent git never meets a config
  314. lock from this) and never raises: a read-only config must not turn fetch recovery into a
  315. traceback, matching mark_unmarked_packs_promisor above.
  316. """
  317. _migrate_earlier_maintenance_keys(repo_root)
  318. for key, value in _TREE0_MAINTENANCE_OFF:
  319. try:
  320. current = run_git(
  321. ["git"], ["config", "--local", "--get", key],
  322. cwd=str(repo_root), capture_output=True, text=True,
  323. encoding="utf-8", errors="replace", timeout=30,
  324. creationflags=windows_hide_flags(),
  325. ).stdout.strip()
  326. if current == value:
  327. continue
  328. run_git(
  329. ["git"], ["config", "--local", key, value],
  330. cwd=str(repo_root), check=True,
  331. capture_output=True, text=True,
  332. encoding="utf-8", errors="replace", timeout=30,
  333. creationflags=windows_hide_flags(),
  334. )
  335. except Exception:
  336. logger.warning("Could not set %s=%s in %s", key, value, repo_root)
  337. def _migrate_earlier_maintenance_keys(repo_root: Path) -> None:
  338. """Undo what earlier cuts of _TREE0_MAINTENANCE_OFF persisted, once.
  339. ``maintenance.auto=false`` switched off git's own post-fetch fold (git <= 2.53) and the first
  340. cut's ``gc.auto=0`` turns ``gc --auto``, the update's own fold, into a no-op. Nothing records
  341. who wrote them, so they are removed only under the earlier cuts' fingerprint: both wrote
  342. ``maintenance.auto=false`` together with ``fetch.writeCommitGraph=false``, and neither wrote
  343. ``maintenance.commit-graph.enabled``. The first pass of disable_tree0_auto_maintenance writes
  344. that key, so this runs once; an operator's own lone setting never matches.
  345. """
  346. try:
  347. local = dict(line.split(None, 1) for line in _git_stdout_lines(repo_root, [
  348. "config", "--local", "--get-regexp",
  349. r"^(maintenance\.auto|maintenance\.commit-graph\.enabled|fetch\.writecommitgraph|gc\.auto)$"]))
  350. if (local.get("maintenance.auto") != "false" or local.get("fetch.writecommitgraph") != "false"
  351. or "maintenance.commit-graph.enabled" in local):
  352. return
  353. for key in ("maintenance.auto", "gc.auto") if local.get("gc.auto") == "0" else ("maintenance.auto",):
  354. run_git(
  355. ["git"], ["config", "--local", "--unset", key],
  356. cwd=str(repo_root), check=True, capture_output=True, timeout=30,
  357. creationflags=windows_hide_flags(),
  358. )
  359. except Exception:
  360. logger.warning("Could not migrate earlier maintenance keys in %s", repo_root)
  361. def clear_stale_tmp_packs(repo_root: Path, *, min_age_seconds: Optional[int] = None) -> list[str]:
  362. """Remove aborted-transfer temp pack files and killed commit-graph writes' ``tmp_graph_*``;
  363. same contract as clear_stale_git_locks.
  364. Resolves ``.git/objects/pack`` for a checkout and ``objects/pack`` for a bare repo such as
  365. the checkpoint store — a ``git gc`` killed by a timeout strands the same debris there."""
  366. pack_dir = _pack_dir(repo_root)
  367. graph_dir = pack_dir.parent / "info" / "commit-graphs"
  368. def _candidates():
  369. found = []
  370. for directory, prefixes in ((pack_dir, _TMP_PACK_PREFIXES), (graph_dir, _TMP_GRAPH_PREFIX)):
  371. try:
  372. found += [e for e in directory.iterdir() if e.name.startswith(prefixes)]
  373. except OSError:
  374. pass
  375. return found
  376. return _sweep_stale(
  377. pack_dir, _candidates,
  378. min_age_seconds=min_age_seconds, default_age=STALE_TMP_PACK_MIN_AGE_SECONDS,
  379. skip_msg="git process running; skipping tmp-pack sweep",
  380. log_removed=lambda p, size: logger.info("Removed aborted-write git debris %s (%d bytes)", p, size),
  381. )
  382. def _git_stdout_lines(repo_root: Path, args: list[str]) -> list[str]:
  383. """Run a read-only git query in ``repo_root``; [] on any failure."""
  384. try:
  385. result = run_git(
  386. ["git"], [*args], cwd=str(repo_root),
  387. capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10,
  388. creationflags=windows_hide_flags(),
  389. )
  390. if result.returncode != 0:
  391. return []
  392. return [line.strip() for line in result.stdout.splitlines() if line.strip()]
  393. except Exception:
  394. logger.debug("git query failed: %s", args, exc_info=True)
  395. return []
  396. def _batch_missing_parents(repo_root: Path, candidates: list[str]) -> set[str]:
  397. """Return local commit objects whose parent objects are missing.
  398. Parents are read from the commit *header* only (lines before the first blank
  399. line) — a ``parent <sha>`` line inside a commit message body is prose, not
  400. an edge.
  401. """
  402. if not candidates:
  403. return set()
  404. try:
  405. parents_by_commit = {}
  406. parents = set()
  407. request = "\n".join(candidates) + "\n"
  408. result = run_git(
  409. ["git"], ["cat-file", "--batch"],
  410. cwd=str(repo_root),
  411. input=request.encode(),
  412. capture_output=True,
  413. timeout=30,
  414. creationflags=windows_hide_flags(),
  415. )
  416. if result.returncode != 0:
  417. return set()
  418. data = result.stdout
  419. cursor = 0
  420. for candidate in candidates:
  421. header_end = data.find(b"\n", cursor)
  422. if header_end < 0:
  423. return set()
  424. header = data[cursor:header_end].split()
  425. cursor = header_end + 1
  426. if len(header) >= 3 and header[1] == b"commit":
  427. size = int(header[2])
  428. body = data[cursor:cursor + size]
  429. cursor += size
  430. if data[cursor:cursor + 1] != b"\n":
  431. return set()
  432. cursor += 1
  433. commit_parents = set()
  434. for line in body.split(b"\n"):
  435. if not line: # blank line ends the commit header block
  436. break
  437. fields = line.split()
  438. if len(fields) >= 2 and fields[0] == b"parent":
  439. commit_parents.add(fields[1].decode())
  440. parents_by_commit[candidate] = commit_parents
  441. parents.update(commit_parents)
  442. elif len(header) < 2 or header[1] != b"missing":
  443. return set()
  444. if not parents:
  445. return set()
  446. check = run_git(
  447. ["git"], ["cat-file", "--batch-check"],
  448. cwd=str(repo_root),
  449. input=("\n".join(sorted(parents)) + "\n").encode(),
  450. capture_output=True,
  451. timeout=10,
  452. creationflags=windows_hide_flags(),
  453. )
  454. if check.returncode != 0:
  455. return set()
  456. missing = {
  457. line.split()[0]
  458. for line in check.stdout.decode(errors="replace").splitlines()
  459. if line.endswith(" missing")
  460. }
  461. return {commit for commit, commit_parents in parents_by_commit.items() if commit_parents & missing}
  462. except Exception:
  463. logger.debug("parent-object probe failed for %s", repo_root, exc_info=True)
  464. return set()
  465. def _shallow_file_path(repo_root: Path) -> Optional[Path]:
  466. """Resolve ``.git/shallow`` via git, or None when the repo has none."""
  467. shallow_rel = _git_stdout_lines(repo_root, ["rev-parse", "--git-path", "shallow"])
  468. if not shallow_rel:
  469. return None
  470. shallow_path = Path(shallow_rel[0])
  471. if not shallow_path.is_absolute():
  472. shallow_path = Path(repo_root) / shallow_rel[0]
  473. return shallow_path if shallow_path.is_file() else None
  474. class _ShallowLock:
  475. """Git's own ``shallow.lock`` protocol, so a concurrent ``git fetch`` that
  476. writes ``.git/shallow`` between our read and our write is never clobbered:
  477. the fetch fails fast on the lock and we re-read before writing. The lock stays
  478. open while held: this holder runs no git between its git calls, so the open fd
  479. (an unlink refusal on Windows) is how :func:`release_dead_fetch_locks` sees it."""
  480. def __init__(self, shallow_path: Path):
  481. self._path = shallow_path
  482. self._lock_path = shallow_path.with_name(shallow_path.name + ".lock")
  483. def __enter__(self) -> _ShallowLock:
  484. try:
  485. fd = os.open(self._lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
  486. except FileExistsError:
  487. raise RuntimeError(f"shallow lock held: {self._lock_path}")
  488. except OSError as exc:
  489. raise RuntimeError(f"cannot create shallow lock: {exc}") from exc
  490. try:
  491. os.write(fd, b"hermes shallow maintenance\n")
  492. except OSError as exc:
  493. # A leaked fd would read as a live holder (and pin the file on Windows).
  494. os.close(fd)
  495. self._lock_path.unlink(missing_ok=True)
  496. raise RuntimeError(f"cannot write shallow lock: {exc}") from exc
  497. self._fd = fd
  498. return self
  499. def __exit__(self, *exc_info) -> None:
  500. os.close(self._fd)
  501. try:
  502. self._lock_path.unlink()
  503. except FileNotFoundError:
  504. pass
  505. def _write_shallow(shallow_path: Path, content: str, *, suffix: str) -> None:
  506. """Atomically replace ``.git/shallow`` (temp file + os.replace)."""
  507. tmp_path = shallow_path.with_name(shallow_path.name + suffix)
  508. tmp_path.write_text(content, encoding="utf-8")
  509. os.replace(tmp_path, shallow_path)
  510. def repair_broken_shallow_boundaries(repo_root: Path) -> int:
  511. """Re-append shallow boundaries for reflog-reachable commits whose parents
  512. were never fetched (#108286).
  513. A reflog-only commit whose graft was pruned leaves the repo unwalkable
  514. (``gc``/``fsck``/``fetch`` fail) and unable to self-heal: reflog expiry
  515. happens during ``git gc``, which is exactly what the corruption breaks.
  516. Only reflog-reachable commits are considered, so unrelated object loss is
  517. never re-labelled as shallow history. Returns the number of boundaries
  518. appended; never raises.
  519. """
  520. try:
  521. shallow_path = _shallow_file_path(repo_root)
  522. if shallow_path is None:
  523. return 0
  524. # Cheap gate: repair only when the walk the corruption breaks already fails.
  525. probe = run_git(
  526. ["git"], ["rev-list", "--count", "--all", "--reflog"],
  527. cwd=str(repo_root), capture_output=True, timeout=10,
  528. creationflags=windows_hide_flags(),
  529. )
  530. if probe.returncode == 0:
  531. return 0
  532. with _ShallowLock(shallow_path):
  533. # Keep the rollback image byte-exact, including BOM and line endings.
  534. original = shallow_path.read_bytes()
  535. existing = {line for line in original.decode("utf-8-sig").splitlines() if line}
  536. if not existing:
  537. return 0
  538. # Boundary candidates: commits recorded as *fetch tips* in remote-tracking
  539. # refs' reflogs (NOT --batch-all-objects, and not HEAD's reflog): the bug
  540. # class is fetched tips whose graft the prune dropped, and restricting to
  541. # fetch-recorded tips is what keeps unrelated object loss (a deleted parent
  542. # of a locally-created commit) from being re-labelled as shallow history.
  543. # On an already-corrupted repo a rev-list --reflog walk is exactly what
  544. # fails, so read the reflog hash list directly. Scope: fetch-by-SHA
  545. # installs (scripts/install.sh) record their tip only in HEAD's reflog
  546. # and are NOT candidates — new corruption of that shape is prevented by
  547. # the prune's reflog fail-safe instead.
  548. reflog = _git_stdout_lines(
  549. repo_root, ["reflog", "show", "--all", "--format=%H%x00%gD"])
  550. candidates = sorted({
  551. sha
  552. for entry in reflog
  553. for sha, selector in [entry.split("\x00", 1)]
  554. if selector.startswith("refs/remotes/")
  555. })
  556. broken = _batch_missing_parents(repo_root, candidates)
  557. repaired = broken - existing
  558. if not repaired:
  559. return 0
  560. _write_shallow(shallow_path, "\n".join(sorted(existing | repaired)) + "\n",
  561. suffix=".hermes-repair")
  562. # Self-check under the same lock hold (rev-list never takes
  563. # shallow.lock): the rollback cannot be defeated by lock contention.
  564. if not _git_stdout_lines(repo_root, ["rev-list", "--count", "--all", "--reflog"]):
  565. shallow_path.write_bytes(original)
  566. logger.debug("shallow boundary repair self-check failed; file restored")
  567. return 0
  568. logger.info("Restored %d broken shallow boundary(ies) in %s", len(repaired), repo_root)
  569. return len(repaired)
  570. except Exception:
  571. logger.debug("shallow boundary repair failed for %s", repo_root, exc_info=True)
  572. return 0
  573. def prune_stale_shallow_grafts(repo_root: Path) -> int:
  574. """Drop ``.git/shallow`` graft lines no live ref or reflog still points at (#105951).
  575. Every ``git fetch --depth 1`` appends the fetched tip to ``.git/shallow`` as a new
  576. graft and never removes the previous one, so a long-lived shallow installer checkout
  577. accumulates one graft per update check (57 observed in the wild). The stale grafts
  578. break ``merge-base`` and push ``hermes update`` into the orphan-divergence reset path
  579. on every run. Keep only the boundaries that still protect referenced tips (HEAD,
  580. FETCH_HEAD, and every ref tip): the dropped commits are already unreachable and their
  581. objects are left for ``git gc``. Fetch reflogs naming a dropped graft are expired
  582. first — otherwise they pin it and the prune rolls back forever (#124645). Returns
  583. the number of graft lines removed; never raises, and restores the original file if
  584. the trimmed set breaks history walking (including the ``--reflog`` walk, so a graft
  585. a reflog-only commit still needs is never dropped, #108286).
  586. """
  587. try:
  588. shallow_path = _shallow_file_path(repo_root)
  589. if shallow_path is None:
  590. return 0
  591. with _ShallowLock(shallow_path):
  592. # Decode for pruning, but retain the same capture for a lossless rollback.
  593. original = shallow_path.read_bytes()
  594. lines = [line for line in original.decode("utf-8-sig").splitlines() if line]
  595. if not lines:
  596. return 0
  597. keep = set(lines) & {
  598. *(_git_stdout_lines(repo_root, ["rev-parse", "HEAD"]) or []),
  599. *(_git_stdout_lines(repo_root, ["rev-parse", "--verify", "--quiet", "FETCH_HEAD"]) or []),
  600. *_git_stdout_lines(repo_root, ["for-each-ref", "--format=%(objectname)"]),
  601. }
  602. if len(keep) == len(lines):
  603. return 0
  604. # A dropped graft's parent was never fetched (depth-1), so a fetch reflog
  605. # still naming it makes the fail-safe walk below fail and rolls the prune
  606. # back on every run — grafts keep accumulating and the next update falls
  607. # into orphan divergence (#124645). Fetch-history reflogs are the only safe
  608. # ones to expire; reflogs users read (HEAD, local branches) keep their
  609. # entries, and the fail-safe still rolls the prune back for those.
  610. dropped = set(lines) - keep
  611. for ref in _git_stdout_lines(
  612. repo_root, ["for-each-ref", "--format=%(refname)", "refs/remotes/"]
  613. ):
  614. entries = _git_stdout_lines(repo_root, ["reflog", "show", "--format=%H", ref])
  615. if set(entries) & dropped:
  616. run_git(
  617. ["git"], ["reflog", "expire", "--expire=now", ref],
  618. cwd=str(repo_root), capture_output=True, timeout=10,
  619. creationflags=windows_hide_flags(),
  620. )
  621. _write_shallow(shallow_path, "\n".join(sorted(keep)) + "\n", suffix=".hermes-prune")
  622. # Fail-safe: if any reachable walk now crosses a boundary we wrongly
  623. # removed, put the grafts back — a growing file beats a broken repo.
  624. # Runs under the same lock hold (rev-list never takes shallow.lock) so
  625. # the rollback cannot be defeated by lock contention.
  626. still_walks = _git_stdout_lines(repo_root, ["rev-list", "--count", "HEAD"]) and \
  627. _git_stdout_lines(repo_root, ["rev-list", "--count", "--all"]) and \
  628. _git_stdout_lines(repo_root, ["rev-list", "--count", "--all", "--reflog"])
  629. if not still_walks:
  630. shallow_path.write_bytes(original)
  631. logger.debug("shallow prune self-check failed; grafts restored")
  632. return 0
  633. logger.info("Pruned %d stale shallow graft(s) in %s", len(lines) - len(keep), repo_root)
  634. return len(lines) - len(keep)
  635. except Exception:
  636. logger.debug("shallow graft prune failed for %s", repo_root, exc_info=True)
  637. return 0
  638. def _partial_clone_filter(repo_root: Path, **run_kwargs) -> str | None:
  639. """The checkout's own ``remote.origin.partialclonefilter``, or None for a non-partial clone."""
  640. result = run_git(
  641. ["git"], ["config", "--get", "remote.origin.promisor"],
  642. cwd=str(repo_root), capture_output=True, text=True,
  643. encoding="utf-8", errors="replace", timeout=30, **run_kwargs,
  644. )
  645. if result.returncode != 0 or result.stdout.strip().lower() != "true":
  646. return None
  647. configured = run_git(
  648. ["git"], ["config", "--get", "remote.origin.partialclonefilter"],
  649. cwd=str(repo_root), capture_output=True, text=True,
  650. encoding="utf-8", errors="replace", timeout=30, **run_kwargs,
  651. )
  652. return configured.stdout.strip() or None
  653. def heal_shallow_history(repo_root: Path, branch: str, **run_kwargs) -> bool:
  654. """Unshallow a stale installer checkout before the updater's bounded fetch (#123254).
  655. From a depth-1 clone far behind ``branch``, a plain ``git fetch origin <branch>`` makes the
  656. server send the full ancestry of every side branch merged past the shallow boundary, which
  657. cannot finish inside the 300s network cap; the post-update unshallow never got to run. Fetch
  658. the commit graph first (900s budget), pulling ``branch`` along so the bounded fetch that
  659. follows is small. Returns whether the checkout was shallow; raises on fetch failure.
  660. """
  661. return _shallow_file_path(repo_root) is not None and fetch_full_commit_graph(repo_root, branch, **run_kwargs)
  662. # A tags-only refresh is a ref listing plus a few small tag objects; when GitHub is slow it can
  663. # stall for minutes, and a miss only leaves the version label stale until the next update. Filling
  664. # shallow history really transfers the commit graph, so it keeps the long budget.
  665. _TAG_REFRESH_TIMEOUT_SECONDS = 30
  666. _HISTORY_FETCH_TIMEOUT_SECONDS = 900
  667. def fetch_full_commit_graph(repo_root: Path, *extra_refspecs: str, **run_kwargs) -> bool:
  668. """Refresh release tags and fill shallow history before publishing identity.
  669. A full commit graph does not imply current tags, especially after a --no-tags
  670. clone. Fetch version tags explicitly without fetching every remote branch or
  671. replacing existing tags. The fetch never changes the clone's mode: ``--filter`` makes git
  672. write ``remote.origin.promisor``/``partialclonefilter``, so a full clone fetches unfiltered
  673. (#122353) and a partial clone repeats its own filter. The one conversion is deliberate: a
  674. depth-limited full clone whose history is really missing unshallows as a partial clone, because
  675. an unfiltered ``--unshallow`` downloads every file version ever committed; a full clone grafted by a
  676. ``--depth`` fetch already has its history and stays full. The converted clone's existing packs
  677. are marked as partial-clone packs, or every later fetch crashes on git 2.53+ (#124272).
  678. It converts to ``blob:none``, the layout installers make (see ``convert_treeless_checkout``).
  679. Returns whether the checkout was unshallowed; fetch failures raise subprocess errors.
  680. """
  681. shallow_path = _shallow_file_path(repo_root)
  682. shallow = shallow_path is not None
  683. # Callers already inject creationflags via no_prompt_git_kwargs(); OR the
  684. # hide flag into the shared kwargs instead of passing the keyword twice
  685. # (TypeError: got multiple values) — the config probes below inherit it.
  686. run_kwargs["creationflags"] = run_kwargs.get("creationflags", 0) | windows_hide_flags()
  687. fetch_filter = _partial_clone_filter(repo_root, **run_kwargs)
  688. converts = fetch_filter is None and shallow and bool(_batch_missing_parents(
  689. repo_root, shallow_path.read_text(encoding="utf-8-sig").split()))
  690. if converts:
  691. fetch_filter = "blob:none"
  692. try:
  693. run_git(
  694. ["git"], ["fetch", "--quiet", *(["--unshallow"] if shallow else []),
  695. *([f"--filter={fetch_filter}"] if fetch_filter else []),
  696. "--no-tags", "origin", "refs/tags/v*:refs/tags/v*", *extra_refspecs],
  697. cwd=str(repo_root), check=True, capture_output=True, text=True,
  698. encoding="utf-8", errors="replace",
  699. timeout=_HISTORY_FETCH_TIMEOUT_SECONDS if shallow else _TAG_REFRESH_TIMEOUT_SECONDS, **run_kwargs,
  700. )
  701. finally:
  702. # git writes the partial-clone config before it fetches, so a failed fetch converts too.
  703. if converts:
  704. mark_unmarked_packs_promisor(repo_root)
  705. disable_tree0_auto_maintenance(repo_root)
  706. return shallow
  707. # git 2.53+ promisor fetches run index-pack --promisor, whose repack_local_links() BUG()s in
  708. # pack-objects (should_include_obj) when an object outside the promisor packs leads to a
  709. # promisor-missing one (#124272). The unmarked packs stay, so fetches keep dying until they are
  710. # marked. The assertion is the fingerprint; a terminator line confirms the helper aborted. Those
  711. # lines vary by build (POSIX "died of signal 6", Windows "could not finish pack-objects to repack
  712. # local links", git 2.55 "fetch-pack: invalid index-pack output", #125138), so a new git wording
  713. # needs a new terminator here.
  714. _PACK_OBJECTS_CRASH_FINGERPRINT = "should_include_obj should only be called on existing objects"
  715. _PACK_OBJECTS_CRASH_TERMINATORS = (
  716. "pack-objects died of signal 6",
  717. "could not finish pack-objects to repack local links",
  718. "fetch-pack: invalid index-pack output",
  719. )
  720. def is_partial_clone_pack_objects_crash(stderr: str) -> bool:
  721. """True when a fetch failure is the git 2.53+ partial-clone pack-objects BUG (#124272)."""
  722. text = stderr or ""
  723. if _PACK_OBJECTS_CRASH_FINGERPRINT not in text:
  724. return False
  725. return any(terminator in text for terminator in _PACK_OBJECTS_CRASH_TERMINATORS)
  726. def fetch_with_partial_clone_recovery(runner: Callable[..., subprocess.CompletedProcess],
  727. git_cmd: list[str], fetch_args: list[str],
  728. repo_root: Path) -> subprocess.CompletedProcess:
  729. """Run a fetch; on the pack-objects BUG, mark the unmarked packs and retry it once.
  730. ``runner(git_cmd, args) -> CompletedProcess`` and ``git_cmd + fetch_args`` is the plain
  731. fetch argv. On the crash, ``repo_root``'s unmarked packs get a ``.promisor`` file and the
  732. identical fetch runs once more. The retry's result is returned whatever its exit code, so the caller keeps its
  733. normal failure handling. (A ``-c remote.origin.promisor=`` override does not help: git
  734. registers promisor remotes additively, so the repo's own ``true`` still wins.)
  735. """
  736. result = runner(git_cmd, fetch_args)
  737. if result.returncode == 0 or not is_partial_clone_pack_objects_crash(getattr(result, "stderr", "") or ""):
  738. return result
  739. mark_unmarked_packs_promisor(repo_root)
  740. logger.info("pack-objects crash on a partial clone; retrying the fetch")
  741. return runner(git_cmd, fetch_args)
  742. def convert_treeless_checkout(repo_root: Path, **run_kwargs) -> bool:
  743. """Turn a treeless (``tree:0``) checkout into the blobless layout installers now make (#129712).
  744. A treeless checkout holds no trees, and git asks for a missing tree without telling the server
  745. which ones it already has, so every checkout and path-filtered history walk downloads complete
  746. directory snapshots again: hundreds of GB on some installs. One ``--refetch`` of the clone's
  747. refspec and its tags brings every commit and tree (about 120 MB for this repo); file contents
  748. stay on demand. The checked-out commit can sit outside both (a branch fetched by hand, a
  749. release ref the refspec does not name), so its history is checked offline and refetched by
  750. commit when trees are still missing. The new filter is recorded only once that history is
  751. whole: a failed or partial conversion leaves the checkout treeless, so the next update
  752. retries. That check, not the fetch's exit status, is the verdict: a refused tag update (a
  753. local tag that would be clobbered) fails the fetch after its objects have landed. Git before
  754. 2.36 has no ``--refetch`` and is left as it is. Returns whether it converted; fetch failures
  755. raise subprocess errors.
  756. """
  757. run_kwargs["creationflags"] = run_kwargs.get("creationflags", 0) | windows_hide_flags()
  758. if _partial_clone_filter(repo_root, **run_kwargs) != "tree:0" or _git_version(**run_kwargs) < (2, 36):
  759. return False
  760. # A fetch spawns a detached gc/maintenance that would repack the whole refetch outside the
  761. # update's time limit; the per-command keys leave the user's own settings alone.
  762. refetch = ["git", "-c", "gc.auto=0", "-c", "maintenance.auto=false", "fetch", "--quiet", "--refetch",
  763. "--filter=blob:none", "origin"]
  764. fetch_kwargs = dict(cwd=str(repo_root), capture_output=True, text=True,
  765. encoding="utf-8", errors="replace", timeout=900, **run_kwargs)
  766. subprocess.run([*refetch, "--tags"], **fetch_kwargs) # noqa: PLW1510 -- forwarding wrapper: callers pass subprocess kwargs via **run_kwargs; an explicit check=False would raise TypeError
  767. if not _history_trees_complete(repo_root, **run_kwargs):
  768. head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(repo_root), check=True,
  769. capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=30,
  770. **run_kwargs).stdout.strip()
  771. subprocess.run([*refetch, head], check=True, **fetch_kwargs)
  772. if not _history_trees_complete(repo_root, **run_kwargs):
  773. raise subprocess.CalledProcessError(1, refetch, stderr="the checked-out history is still missing trees")
  774. # A refetch into an existing partial clone leaves its configured filter alone; record the
  775. # new one only now, so a failed or interrupted refetch is retried by the next update.
  776. subprocess.run(
  777. ["git", "config", "remote.origin.partialclonefilter", "blob:none"],
  778. cwd=str(repo_root), check=True, capture_output=True, timeout=30, **run_kwargs,
  779. )
  780. return True
  781. def convert_treeless_checkout_first(repo_root: Path) -> None:
  782. """Run the one-time conversion as the update's first new-code step. Never raises.
  783. Both update hand-offs (``update_completion._prepare`` and an older updater's
  784. ``_update_takeover.prepare``) call this before minutes of dependency work: a Desktop built before
  785. the fix keeps walking history during that window, and on a treeless checkout every walk
  786. downloads trees again (#129514: 434 GB, disk full mid-update). Runs in the dependency-free
  787. bootstrap interpreter.
  788. """
  789. from hermes_cli._subprocess_compat import no_prompt_git_kwargs
  790. try:
  791. if convert_treeless_checkout(repo_root, **no_prompt_git_kwargs()):
  792. print(" ✓ Fetched this checkout's directory history once; updates stop re-downloading it",
  793. flush=True)
  794. except (OSError, subprocess.SubprocessError) as exc:
  795. detail = (getattr(exc, "stderr", None) or str(exc)).strip().splitlines()[-1:] or [type(exc).__name__]
  796. print(f" ⚠ Could not fetch this checkout's directory history ({detail[0]}); retrying next update",
  797. flush=True)
  798. def _git_version(**run_kwargs) -> tuple:
  799. out = subprocess.run(["git", "--version"], capture_output=True, text=True, encoding="utf-8", # noqa: PLW1510 -- forwarding wrapper: **run_kwargs may carry check
  800. errors="replace", timeout=30, **run_kwargs).stdout
  801. match = re.search(r"(\d+)\.(\d+)", out)
  802. return (int(match[1]), int(match[2])) if match else (0, 0)
  803. def _history_trees_complete(repo_root: Path, **run_kwargs) -> bool:
  804. """Whether every tree in HEAD's history is local, checked without fetching (~3 s for this repo)."""
  805. env = dict(run_kwargs.pop("env", None) or noninteractive_git_env(), GIT_NO_LAZY_FETCH="1")
  806. walk = subprocess.run( # noqa: PLW1510 -- forwarding wrapper: **run_kwargs may carry check
  807. ["git", "rev-list", "--objects", "--filter=blob:none", "--missing=print", "HEAD"],
  808. cwd=str(repo_root), capture_output=True, text=True, encoding="utf-8", errors="replace",
  809. timeout=300, env=env, **run_kwargs,
  810. )
  811. return walk.returncode == 0 and not any(line.startswith("?") for line in walk.stdout.splitlines())
  812. def settle_partial_clone_maintenance(repo_root: Path) -> None:
  813. """Persist the commit-graph-off keys on a partial clone; a full clone keeps stock maintenance.
  814. Runs on every update so checkouts that predate the installer change converge. Never raises.
  815. """
  816. try:
  817. if _partial_clone_filter(repo_root, creationflags=windows_hide_flags()) is not None:
  818. disable_tree0_auto_maintenance(repo_root)
  819. except Exception:
  820. logger.debug("partial-clone maintenance settings failed for %s", repo_root, exc_info=True)