Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion core/src/okfy/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down