From 1075b53ecac5a44047e6f6aec78b50a86b55c04c Mon Sep 17 00:00:00 2001 From: vbanas Date: Sun, 18 Oct 2026 10:11:12 +0300 Subject: [PATCH] init: list a git corpus with git ls-files --exclude-standard so gitignored files never enter the manifest --- core/src/okfy/init.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/core/src/okfy/init.py b/core/src/okfy/init.py index 1d9eb93..c15ad18 100644 --- a/core/src/okfy/init.py +++ b/core/src/okfy/init.py @@ -4,7 +4,7 @@ import os from pathlib import Path -from okfy.bundle import is_bundle_dir +from okfy.bundle import drop_bundle_paths, is_bundle_dir from okfy.frontmatter import serialize from okfy.gitenv import run_git from okfy.guard import assert_safe_bundle_path @@ -45,6 +45,13 @@ def _manifest(corpus: Path, errors: list[str] | None = None, skipped_symlinks: list[str] | None = None) -> dict[str, str]: """path -> sha256, for every non-hidden file under `corpus`. + A corpus inside a git work tree is listed by `git ls-files --cached + --others --exclude-standard`, the listing `segment._walk` reads, so a + gitignored file (a private corpus, a build directory) never reaches the + committed manifest; hidden components and embedded bundles are dropped as + the walk below drops them. Outside git, or when git fails, the os.walk + below is used unchanged. + Walked with `os.walk(..., onerror=...)` rather than `Path.rglob` so an unreadable directory or file can be REPORTED instead of silently dropped from the listing: `rglob` swallows `OSError` from `scandir` the same way @@ -69,6 +76,21 @@ def onerror(exc: OSError) -> None: if errors is not None: errors.append(getattr(exc, "filename", None) or str(exc)) + listed = _git_listing(corpus) + if listed is not None: + for rel in listed: + p = corpus / rel + if not p.is_file(): + if skipped_symlinks is not None and p.is_symlink(): + skipped_symlinks.append(rel) + continue + try: + out[rel] = hashlib.sha256(p.read_bytes()).hexdigest() + except OSError: + if errors is not None: + errors.append(rel) + return out + for dirpath, dirnames, filenames in os.walk(corpus, onerror=onerror): # Prune hidden dirs from descent — matches the old rglob filter, # which excluded any path with a "."-prefixed component — and any @@ -93,6 +115,24 @@ def onerror(exc: OSError) -> None: return out +def _git_listing(corpus: Path) -> list[str] | None: + """Corpus-relative posix paths git knows about under `corpus`, gitignore + respected (`--exclude-standard`), minus hidden components and embedded + bundles — or None when `corpus` is not inside a git work tree or git + fails, so the caller falls back to walking the directory.""" + r = run_git(corpus, "rev-parse", "--is-inside-work-tree", + capture_output=True, text=True) + if r.returncode != 0 or r.stdout.strip() != "true": + return None + r = run_git(corpus, "ls-files", "--cached", "--others", "--exclude-standard", + "-z", capture_output=True, text=True) + if r.returncode != 0: + return None + rels = sorted({rel for rel in r.stdout.split("\0") + if rel and not any(part.startswith(".") for part in rel.split("/"))}) + return drop_bundle_paths(corpus, rels) + + def manifest_digest(manifest: dict[str, str]) -> str: """One sha256 over a `_manifest` — every path read and the sha256 of the bytes read from it. This is what a snapshot can prove it acted on; the