Documentation
¶
Overview ¶
Package gitdiff resolves the set of files changed in a working tree and exposes a path Allowlist that the whitebox scanners consult to scope a scan to only those files — the engine half of "diff-aware scanning" (`fendix scan --diff[=ref] --staged`), the single most important developer-workflow feature in the 90-day cut.
Why os/exec git instead of go-git: the codebase deliberately avoids the go-git dependency (it is large and has surprising defaults — see internal/pluginscmd). Resolving a changed-file list is a one-shot `git diff --name-only` shell-out; the heavyweight library buys us nothing here. The only git feature we need is name-only diff output, which every git since 1.x prints identically.
The Allowlist is intentionally a separate, scanner-agnostic type so the secrets / textscan / semgrep packages can filter against it without importing each other or the orchestrator. A nil *Allowlist means "no diff scoping" and every scanner behaves exactly as it does in a full scan — diff mode can only ever *narrow* the file set, never change detection logic, so it cannot move the heavy-eval F1 score.
Index ¶
- func ChangedFiles(ctx context.Context, opts Options) ([]string, error)
- func TraversesSymlink(root, abs string) bool
- type Allowlist
- func (a *Allowlist) AbsPaths() []string
- func (a *Allowlist) Allows(path string) bool
- func (a *Allowlist) CheckScope(root string) error
- func (a *Allowlist) ContainsBase(names ...string) bool
- func (a *Allowlist) Empty() bool
- func (a *Allowlist) Len() int
- func (a *Allowlist) RelPaths(root string) ([]string, error)
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChangedFiles ¶
ChangedFiles returns the repo-relative paths of files changed under the given options, in sorted order, with deletions excluded (a deleted file has nothing left to scan). The returned slice is empty (not nil) when the tree is clean, so callers can distinguish "nothing changed" from an error.
Renames are reported as their new path (--diff-filter excludes the old side via D). Paths git would quote (those containing unusual bytes) are returned verbatim from `-z` NUL-delimited output, so no shell unquoting is needed.
func TraversesSymlink ¶
TraversesSymlink reports whether reaching abs from root crosses a symlinked directory component (or escapes root). filepath.WalkDir never descends into symlinked directories, so the diff-aware fast scanners MUST skip any allowlist entry whose parent path traverses one — otherwise they read files the full walk would never reach, breaking walk parity and (worse) letting a symlinked parent point outside the repo. A bare os.Lstat(abs) does not catch this: it follows intermediate symlinks and only reveals a final-component symlink. We Lstat each directory component instead. Returns true (refuse) on any error or if abs is not contained in root. root may be relative; it is resolved first, like abs.
Types ¶
type Allowlist ¶
type Allowlist struct {
// contains filtered or unexported fields
}
Allowlist is a set of absolute file paths a diff-aware scan is restricted to. A nil *Allowlist means "no scoping" — see the package doc. Methods are nil-safe so callers can hold a *Allowlist field that is nil in full-scan mode and pass it straight to scanners.
func NewAllowlist ¶
NewAllowlist builds an Allowlist from repo-relative paths resolved against root. Paths are cleaned and made absolute so a scanner walking an absolute tree (secrets/textscan call filepath.Abs on their root) can match entries directly. An empty paths slice yields a non-nil, empty Allowlist that matches nothing — the correct behaviour for "diff requested, zero files changed" (scan nothing, not everything).
func (*Allowlist) AbsPaths ¶
AbsPaths returns the allowlist entries as cleaned absolute paths, sorted. The diff-aware scanners use it to scan only the changed files directly — O(changed files) — instead of walking the whole tree to filter down to them. A nil/empty allowlist yields nil.
func (*Allowlist) Allows ¶
Allows reports whether the given path is in the allowlist. A nil Allowlist allows everything (full-scan semantics). The path is cleaned and absolutised before lookup so callers may pass either absolute or root-relative paths.
func (*Allowlist) CheckScope ¶
CheckScope reports whether the allowlist can safely scope a scan of root: root resolves, and every entry lies under it and exists. ChangedFiles drops deletions, so an entry that does not exist means git's repository-relative path did not map onto the tree under root, as happens when root is a subdirectory of the repository. A non-nil error means the diff scope cannot be trusted, and the caller must widen to a full scan rather than narrow to files no scanner can find. A nil or empty allowlist is always in scope.
func (*Allowlist) ContainsBase ¶
ContainsBase reports whether any allowlisted file has the given base name (e.g. "go.mod", "package-lock.json", "poetry.lock"). The SCA gate uses this to decide whether a dependency manifest changed and the ecosystem's dep-CVE scan must run. Matching is on the final path element only, so a nested service's manifest (services/api/requirements.txt) still triggers.
func (*Allowlist) Empty ¶
Empty reports whether the allowlist scopes the scan to zero files. Always false for a nil Allowlist (full scan). Lets the orchestrator short-circuit the expensive whitebox walk when a diff matched nothing scannable.
func (*Allowlist) RelPaths ¶
RelPaths returns the allowlist entries relative to root, sorted, as the semgrep `--include` set. root may be relative; it is resolved first. An entry that is not under root is an error: it has no root-relative form, and handing a scanner its absolute path as a filter would match nothing while the run still looked clean. A nil or empty allowlist yields no paths.
type Options ¶
type Options struct {
// RepoRoot is the directory the git command runs in. Empty means the
// process working directory. The resolved Allowlist paths are made
// absolute against this root.
RepoRoot string
// Ref, when non-empty, diffs the working tree (or the index, if
// Staged) against this git ref — a branch, tag, or commit-ish such as
// "origin/main" or "HEAD~1". Empty Ref diffs against HEAD.
Ref string
// Staged restricts the diff to changes staged in the index
// (`git diff --cached`) — the set a pre-commit hook must scan. When
// false, unstaged working-tree changes are included too.
Staged bool
}
Options selects which changed-file set git should report.