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
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
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 |