go.bigb.es/cacher/internal/hash

go.bigb.es/cacher @ v0.2.2 — 333189a2 11 days ago

import "go.bigb.es/cacher/internal/hash"

Overview

Package hash derives cache keys from file/directory contents.

For a single file path, Derive returns the same hex digest as `sha256sum <path> | cut -c1-<length>` (default length 16). This matches the convention used by the existing .builds/lib/ci-lib.sh shell helpers it replaces.

Constants

const DefaultLength = 16

DefaultLength is the hex-character count after truncation. 16 hex chars = 64 bits, which matches the existing `cut -c1-16` shell convention.

Functions

f func ApplyTemplate

src
func ApplyTemplate(keyTemplate, derived, goos, goarch string, archSuffix bool) string

ApplyTemplate substitutes {hash} (and {arch} when archSuffix is true) in keyTemplate with the derived values. If keyTemplate has no {hash} placeholder and paths is non-empty, the hash is appended before the final extension. When archSuffix is true, "-<goos>-<goarch>" is appended to the final path component, before its extension.

f func Derive

src
func Derive(paths []string, length int) (string, error)

Derive returns a deterministic hex digest of the contents of the given paths, truncated to length characters. The order of paths matters: concatenating in flag order is intentional (so callers can express dependencies like "Dockerfile + context dir" with stable ordering).

A single regular file produces the same digest as `sha256sum <file>` when length is 64 (or its prefix when length<64). A directory is hashed recursively by walking entries in sorted relative-path order.

Source files