MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / file_hash

Function file_hash

graphify/cache.py:604–724  ·  view source on GitHub ↗

SHA256 of file contents + path relative to root. Uses a stat-based fastpath (size + mtime_ns) to skip full reads when the file hasn't changed. Falls through to full SHA256 on first encounter, when stat changes, and when the recorded signature is not yet provably stable (see :func:`_

(
    path: Path, root: Path = Path("."), cache_root: "Path | None" = None,
    *, include_frontmatter: bool = False,
)

Source from the content-addressed store, hash-verified

602
603
604def file_hash(
605 path: Path, root: Path = Path("."), cache_root: "Path | None" = None,
606 *, include_frontmatter: bool = False,
607) -> str:
608 """SHA256 of file contents + path relative to root.
609
610 Uses a stat-based fastpath (size + mtime_ns) to skip full reads when the
611 file hasn't changed. Falls through to full SHA256 on first encounter, when
612 stat changes, and when the recorded signature is not yet provably stable
613 (see :func:`_stat_sig_fresh`) — so two different contents can never share a
614 digest. Index is flushed atomically at process exit.
615
616 Using the walked path relative to root keeps distinct symlink aliases from
617 sharing an extraction entry while preserving portability across machines
618 and checkout directories. Falls back to the resolved path when the walked
619 path cannot be expressed relative to root.
620
621 Markdown hashes omit YAML frontmatter by default for semantic cache
622 compatibility. AST callers include it because it affects page facts.
623
624 The path handed to the filesystem comes from os_long_path(); the cache key
625 preserves case because case sensitivity belongs to the volume, not the
626 platform (#4925).
627 """
628 global _stat_index_dirty
629 raw_path = Path(path)
630 raw_root = Path(root)
631 src = Path(_os_long_path(raw_path))
632 root_src = Path(_os_long_path(raw_root))
633 # The build's earlier word-count read already computed this exact digest and
634 # parked it here keyed by the same (path, root): serving it skips the two
635 # Path.resolve() calls below, which dominate a cold load_cached() pass once
636 # the content read is gone. Same stat + racily-clean proof as the stat index.
637 # The memo holds the plain-slot digest (Markdown frontmatter stripped), so an
638 # AST caller asking for a Markdown hash with its frontmatter skips it.
639 memo = (
640 None
641 if include_frontmatter and raw_path.suffix.lower() == ".md"
642 else _RUN_HASH.get((str(raw_path), _root_key(raw_root)))
643 )
644 if memo is not None:
645 size, mtime_ns, indexed_at_ns, digest = memo
646 try:
647 st_memo: "os.stat_result | None" = os.stat(src)
648 except OSError:
649 st_memo = None
650 if (
651 st_memo is not None
652 and stat.S_ISREG(st_memo.st_mode)
653 and st_memo.st_size == size
654 and st_memo.st_mtime_ns == mtime_ns
655 and mtime_ns + _mtime_granularity_ns() <= indexed_at_ns
656 ):
657 return digest
658 if not src.is_file():
659 raise IsADirectoryError(f"file_hash requires a file, got: {src}")
660
661 # The stat index is a cache artifact, so it must follow the cache location

Calls 9

_root_keyFunction · 0.85
_mtime_granularity_nsFunction · 0.85
_ensure_stat_indexFunction · 0.85
_normalize_pathFunction · 0.85
_file_saltFunction · 0.85
_stat_sig_freshFunction · 0.85
_content_digestFunction · 0.85
_stat_entry_forFunction · 0.85
getMethod · 0.45