gitward (binary ward) manages encrypted, git-tracked secrets for a monorepo.
A single encrypted store ($GIT_ROOT/.gitward.json) is fanned out to leaf
.env / .dev.vars files; local edits are captured back on commit via git
hooks. Module: github.com/taigrr/gitward.
go build ./... # build
go test ./... # all tests
go test -race ./... # race detector (CI-equivalent; must stay green)
go vet ./... # vet
gofmt -l . # must print nothing
go build -o /tmp/ward ./cmd/ward # build the CLI binary
There is no Makefile or CI config; the commands above are the full toolchain.
Go version is pinned to 1.27.1 in go.mod.
Data flows through layered internal/ packages (leaf depends on store; engine
depends on all):
internal/store— on-disk JSON shape. Store maps basepath dir → env →{buildtime, runtime}→ key → encrypted value. Key names are cleartext (SOPS style, meaningful diffs); values are individually encrypted.internal/crypto— envelope encryption: one random DEK (AES-256-GCM per value) wrapped 3 ways (age/ssh, gpg, scrypt passphrase); any one unwraps.RecoverDEKtries passphrase → ssh → gpg in that order. Recipients are stored grouped by name inKeys.Recipients(map[name][]keys); each key is classified at wrap time byisAgeKey(ssh-ed25519/ssh-rsa/age1 prefix → age wrap, otherwise → gpg fingerprint). One person can hold both an ssh key and a gpg fingerprint under one name.internal/merge— the 3-way merge state machine (store S / leaf L / base B). Pure, no I/O, heavily unit-tested. This is the correctness core.internal/leaf— dotenv parse/serialize + filename convention.internal/gitutil— repo discovery by walking for.git(no config parse, soextensions.worktreeConfigrepos and linked worktrees work); ignore matching via go-git's matcher off the raw filesystem; staging shells out togit add(only ever runs inside the pre-commit hook, where git is present).internal/engine— ties everything together (Open,Plan,Apply,Init,Register, hooks, doctor). The CLI is a thin shell over this.internal/cli+cmd/ward— cobra commands wrapped by charmbraceletfang. CLI compatibility is a hard constraint: existing commands, positional forms, and human-readable output must keep working. New capability is added via flags (--json,--dry-run,--exit-code,--take/--key/--value), new verbs (get/set/unset/recipients), or alternative arg forms (a cell may be a leaf file path or the<path> <env> <tier>triple; seeparseCellRef/cellArgCountincli/helpers.go). Shared JSON shapes and exit-code plumbing live incli/output.go:--jsonis a global persistent flag,ExitError{Code, Silent}carries exit codes through fang'sWithErrorHandler, andExitConflict(2) is reserved for conflicts.
- Deterministic ciphertext is intentional, not a bug.
EncryptValueStablederives the GCM nonce from(DEK, key, plaintext)so unchanged values re-encrypt to byte-identical output, keeping git diffs minimal. Do NOT switch to random nonces — a test enforces this. - The variable name is bound as AEAD additional data. Ciphertext cannot be moved between keys; decrypting under the wrong name fails by design.
- The base snapshot (
.git/gitward/base.json) is the merge-base linchpin. It holds plaintext (same secrets as the.envfiles), lives inside.gitso it is never committed and needs no gitignore entry. Without it the 3-way merge degrades to lossy store-wins/leaf-wins. Advance it only for non-conflicting cells. - A missing leaf file is NOT a deletion. In
engine.Plan/Applyan absent leaf is treated as "no local change" (leaf == base) so fresh clones regenerate and pulls never clobber uncommitted edits. A key removed from an existing file does propagate as a deletion. - age cannot use ssh-agent — only signs, can't derive a decryption key. The
ssh path (
crypto/identity.go) requires the private key file on disk (~/.ssh/id_ed25519, thenid_rsa). Agent-backed decryption is only via the gpg path. - gpg is deliberately shelled out (
crypto/gpg.go) to preserve gpg-agent / pinentry / smartcard support; a pure-Go OpenPGP lib would lose those. This is the only external binary dependency. - Hooks honor
core.hooksPath.InstallHooksandDoctorresolve the hooks dir viagitutil.HooksDir(git rev-parse --git-path hooks), so husky / customcore.hooksPathsetups install and verify in the right place — not a hardcoded.git/hooks. Install is idempotent (skips ifhookMarkerpresent) and appends to a pre-existing hook body rather than clobbering it. - Hooks must never block git except pre-commit on a true conflict.
runHook(cli/doctor.go) swallows most errors with a warning and exits 0;post-checkout/post-merge= store→leaf,pre-commit= leaf→store + stage the store.Applyreturns a conflict count and refuses to mutate the store whendir != StoreToLeafand conflicts exist. _is the sentinel env (store.DefaultEnv) for suffix-less files (.env,.dev.vars). Filenames are convention-derived inleaf.FileName: buildtime →.env[.<env>], runtime →.dev.vars[.<env>].- Only
*.exampledotfiles are ever committed; everything else is gitignored. - Ignored variables are invisible to the merge but preserved on disk.
A ward-ignored variable is stored as an
EncValue{Ignore: true}marker colocated with where its value would live — in the cell'sTierMap, with noenc— so ignoring is per-cell(path, env, tier). The engine strips ignored keys from all three merge inputs (store, leaf, base) inPlan/Apply/Register, so they are never captured, written, or reported as drift. On store→leaf regenerationwriteLeafre-emits any ignored keys (from the on-disk leaf, else a seed value) in a trailing block afterleaf.IgnoredHeader— emitted only when at least one ignored variable is present. Markers carry no plaintext, soDecryptStoreskips them andwriteStoreFromPTre-injects them viareinjectIgnores(otherwise the rebuild-from-plaintext would drop them).engine.Ignoreevicts an already-stored key from store+base and preserves its last-known value into the leaf even when the leaf file is absent.
- Integration tests (
engine/engine_test.go) build real repos in-process with go-git (git.PlainInit/PlainClone), not by shelling togit. - Test helpers
chdirinto a temp repo and setGITWARD_PASSPHRASE(crypto.PassphraseEnv) viat.Setenv, then unlock the store with the passphrase recipient — this is also how CI decrypts. merge_test.gocovers the full state-machine table; keep it exhaustive when changing merge rules.- Prefer adding cases to the existing table-driven tests over new ad-hoc ones.
wrangler secret put push hook, secret-rotation helpers, and a bubbletea TUI
(everything is CLI today).