Skip to content

cache

Cache handling for snippet hashes.

cache

Cache handling for snippet hashes.

This module provides functions for computing content hashes and managing the cache file that tracks which snippets have been applied to which files. The cache prevents unnecessary file rewrites when snippets haven't changed.

compute_hash(text)

Return a deterministic SHA-256 hex digest for a Unicode string.

Parameters:

Name Type Description Default
text str

The string to hash.

required

Returns:

Type Description
str

A 64-character lowercase hexadecimal SHA-256 digest.

Source code in pre_commit_snippet/cache.py
def compute_hash(text: str) -> str:
    """
    Return a deterministic SHA-256 hex digest for a Unicode string.

    Args:
        text: The string to hash.

    Returns:
        A 64-character lowercase hexadecimal SHA-256 digest.
    """
    return hashlib.sha256(text.encode("utf-8")).hexdigest()

load_cache(cache_file)

Load the JSON cache from disk.

Parameters:

Name Type Description Default
cache_file Path

Path to the cache file.

required

Returns:

Type Description
dict[str, str]

A dictionary mapping cache keys to hash values.

dict[str, str]

Returns an empty dict if the file does not exist or is corrupt.

Source code in pre_commit_snippet/cache.py
def load_cache(cache_file: Path) -> dict[str, str]:
    """
    Load the JSON cache from disk.

    Args:
        cache_file: Path to the cache file.

    Returns:
        A dictionary mapping cache keys to hash values.
        Returns an empty dict if the file does not exist or is corrupt.
    """
    if not cache_file.is_file():
        return {}
    try:
        data = json.loads(cache_file.read_text(encoding="utf-8"))
        if isinstance(data, dict):
            return dict(data)
        return {}
    except Exception:
        return {}

save_cache(cache_file, cache)

Write the cache to disk atomically.

Uses a temporary file and rename to avoid corruption on crash.

Parameters:

Name Type Description Default
cache_file Path

Path to the cache file.

required
cache dict[str, str]

A dictionary mapping cache keys to hash values.

required
Source code in pre_commit_snippet/cache.py
def save_cache(cache_file: Path, cache: dict[str, str]) -> None:
    """
    Write the cache to disk atomically.

    Uses a temporary file and rename to avoid corruption on crash.

    Args:
        cache_file: Path to the cache file.
        cache: A dictionary mapping cache keys to hash values.
    """
    tmp = cache_file.with_suffix(".tmp")
    tmp.write_text(json.dumps(cache, indent=2, sort_keys=True), encoding="utf-8")
    tmp.replace(cache_file)