git

package
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2026 License: GPL-3.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultMaxCommits bounds how many matching commits are folded into one
	// review. A broad filter (`--author me` on a year-old repo) would
	// otherwise assemble a diff far past any model's context window.
	DefaultMaxCommits = 200
)

Variables

View Source
var (
	ErrUnsupported = errors.New("git: operation not supported on this backend")
	ErrNotARepo    = errors.New("git: not inside a git repository")
	ErrNoGitCLI    = errors.New("git: `git` binary not on PATH and go-git fallback unavailable")
)

Functions

func Commit added in v1.5.0

func Commit(ctx context.Context, repoRoot, message string) (string, error)

Commit creates a commit from the currently staged changes with the given message, returning git's summary line(s) (e.g. "[main abc1234] feat: …").

This is the one place the tool writes to git (ADR-0019, superseding ADR-0015 §6's read-only stance for the authoring path). It is reached only after the `commit` command has the user's explicit confirmation (or --yes).

The message is fed via stdin (`git commit -F -`) rather than `-m` so that multi-line bodies and arbitrary content commit verbatim without any shell-quoting or newline pitfalls, and so the behaviour is identical across platforms. Commit hooks run as usual (no --no-verify) — a failing pre-commit hook surfaces as an error here.

func DefaultBranch added in v1.15.0

func DefaultBranch(ctx context.Context, repoRoot string) string

DefaultBranch resolves the repo's base branch: the target of refs/remotes/origin/HEAD when it exists, else the first of main/master/trunk that does, else the current HEAD. It never fails — a topology view with an imperfect base is far more useful than an error.

func FilteredDiff added in v1.15.0

func FilteredDiff(ctx context.Context, repoRoot string, f CommitFilter) (Diff, Selection, error)

FilteredDiff selects the commits matching f and returns their concatenated patches together with the selection metadata. It is read-only: `git log`, `git for-each-ref`, `git rev-list`, `git show`.

func FindRepo

func FindRepo(start string) (string, error)

func PatchesFor added in v1.15.0

func PatchesFor(ctx context.Context, repoRoot string, commits []CommitMeta) (string, error)

PatchesFor is phase B: the concatenated patches of the given commits, in the order given.

`git show` is used rather than `git log -p` because we need per-commit framing we control. A raw `git log -p` interleaves the commit message indented by four spaces, and internal/diff's parser reads a leading-space line as hunk CONTEXT — a message body would be silently absorbed into the previous hunk. Emitting a bare record separator as the format and slicing each block from its first `diff --git` removes that whole class of problem.

`-m --first-parent` is what makes a merge commit produce a patch at all (git shows nothing for a merge otherwise); it is a no-op on ordinary commits.

func RefsByCommit added in v1.15.0

func RefsByCommit(ctx context.Context, repoRoot string) (map[string][]string, error)

RefsByCommit maps a commit hash to the short names of every branch and tag pointing at it, for the graph's `(main, v1.2.0)` labels.

Best-effort: a repo with no refs, or a git that refuses the query, yields an empty map rather than an error — labels are decoration, and a graph without them is still a graph.

Types

type Branch added in v1.15.0

type Branch struct {
	Name    string    // short ref name, e.g. "main" or "origin/feature/x"
	Hash    string    // commit the ref points at
	Remote  bool      // true for refs/remotes/*
	Subject string    // the tip commit's subject
	Author  string    // the tip commit's author name
	Date    time.Time // the tip commit's author date
	Ahead   int       // commits on this ref that the base lacks
	Behind  int       // commits on the base that this ref lacks
	IsBase  bool      // this ref IS the base; Ahead/Behind are 0 by definition
}

Branch is one ref plus its position relative to the base branch.

func BranchTopology added in v1.15.0

func BranchTopology(ctx context.Context, repoRoot, base string) ([]Branch, error)

BranchTopology returns every local and remote-tracking branch, each measured against base. A base of "" is resolved with DefaultBranch.

Ahead/behind are computed per ref with `git rev-list --left-right --count base...ref`, which is symmetric-difference counting — exactly what "3 ahead, 12 behind" means to a human. Refs are ordered base-first, then local, then remote, then by name, so the output is deterministic.

type CLIRepo

type CLIRepo struct {
	// contains filtered or unexported fields
}

func NewCLIRepo

func NewCLIRepo(root string) (*CLIRepo, error)

func (*CLIRepo) BranchDiff

func (r *CLIRepo) BranchDiff(target string) (Diff, error)

func (*CLIRepo) CommitDiff

func (r *CLIRepo) CommitDiff(hash string) (Diff, error)

func (*CLIRepo) Diff added in v0.9.0

func (r *CLIRepo) Diff(args []string) (Diff, error)

Diff is the generic `git diff <args>` passthrough used by the `commitbrief diff` subcommand. We always inject `--no-color` and `--no-ext-diff` so the parser/renderer pipeline sees the same stable unified-diff shape the other Diff*() helpers produce. The caller is responsible for validating args (e.g. requiring at least one positional ref) — empty args yields `git diff` (i.e. unstaged), which is identical to UnstagedDiff() and discouraged.

func (*CLIRepo) FileDiff

func (r *CLIRepo) FileDiff(path string) (Diff, error)

func (*CLIRepo) RangeDiff

func (r *CLIRepo) RangeDiff(target, feature string) (Diff, error)

func (*CLIRepo) Root

func (r *CLIRepo) Root() string

func (*CLIRepo) StagedDiff

func (r *CLIRepo) StagedDiff() (Diff, error)

func (*CLIRepo) UnstagedDiff

func (r *CLIRepo) UnstagedDiff() (Diff, error)

type CommitFilter added in v1.15.0

type CommitFilter struct {
	// Authors / Committers match case-insensitively against BOTH the name and
	// the email, so `--author ayse` and `--author ayse@example.com` both work.
	// Multiple values are OR'd; the two kinds are AND'd with each other.
	Authors    []string
	Committers []string

	// Since / Until bound the author date. Zero means unbounded. Until is
	// expected to already carry the end-of-day time (the CLI expands
	// --end-date so the named day is inclusive).
	Since time.Time
	Until time.Time

	// Text matches the commit subject+body, and additionally the NAME of a
	// branch/remote ref — a ref whose short name contains Text contributes
	// the commits unique to it (phase A′).
	Text string

	// Merges includes merge commits in the selection. Off by default: a
	// merge's changes are already carried by the commits it merges, and its
	// patch is noise.
	Merges bool

	// MaxCommits caps the selection; 0 means DefaultMaxCommits.
	MaxCommits int

	// Rev is the revision range to walk, e.g. ["main..HEAD"]. Empty means
	// ["HEAD"] — the implicit history walk.
	Rev []string
}

CommitFilter is the commit-level narrowing requested on the command line. The zero value selects nothing and reports Active() == false, which is how the pipeline decides to keep using the ordinary `git diff` path.

func (CommitFilter) Active added in v1.15.0

func (f CommitFilter) Active() bool

Active reports whether any *selecting* filter is set. Merges and MaxCommits are deliberately excluded: they only modify a selection, they never create one, so `--merges` alone must not silently turn a staged review into a history walk. The CLI rejects a modifier used on its own.

type CommitMeta added in v1.6.0

type CommitMeta struct {
	Hash           string    // full 40-hex hash; empty for RangeCommits records
	Parents        []string  // full parent hashes (%P); empty for RangeCommits records
	Short          string    // abbreviated hash, e.g. "a1b2c3d"
	Author         string    // author name (%an)
	AuthorEmail    string    // author email (%ae)
	Committer      string    // committer name (%cn)
	CommitterEmail string    // committer email (%ce)
	Date           time.Time // author date (%aI), parsed from strict ISO 8601
	Subject        string    // first line of the commit message
	Body           string    // remainder of the commit message (may be empty)
	Files          []string  // post-change paths touched by this commit
}

CommitMeta is one commit's human-relevant metadata. It carries no diff body — patches are fetched separately (via the Diff() passthrough for a range, or PatchesFor for a selected commit set) — only the identity, the author's subject/body (the "commit message"), and the paths the commit touched (so the model can map a logical area back to the commit responsible for it).

Two producers fill it, and they populate different subsets:

  • RangeCommits (the `commitbrief summary` manifest) sets Short, Subject, Body, Files. The identity/date fields stay zero — the manifest never needed them.
  • SelectCommits (the commit-level filters) sets every field, because author/committer/date matching happens on this struct and the commit graph (ADR-0037) needs Parents to lay out its lanes.

func RangeCommits added in v1.6.0

func RangeCommits(ctx context.Context, repoRoot string, logArgs []string) ([]CommitMeta, error)

RangeCommits returns the commit manifest for a log range (e.g. {"main..develop"} or {"HEAD~3..HEAD"}). It is read-only — `git log` only — and the caller is expected to have already derived a clean two-endpoint range from the user's diff arguments (see deriveLogRange in the cli package); arbitrary single refs would make `git log` walk all of history.

Merge commits are excluded (--no-merges): their diffs are already covered by the cumulative range diff and their subjects ("Merge branch …") add noise rather than intent. The result is capped at maxRangeCommits.

Best-effort by contract: a non-zero git exit (bad range, shallow clone, etc.) returns an error the caller may treat as "no manifest" and degrade to a diff-only summary rather than failing the whole command.

type Diff

type Diff struct {
	Content string
	Origin  Origin
	Args    map[string]string
}

func (Diff) Empty

func (d Diff) Empty() bool

type DispatchOptions

type DispatchOptions struct {
	Logger *slog.Logger
}

type DispatchRepo

type DispatchRepo struct {
	// contains filtered or unexported fields
}

func Open

func Open(root string, opts DispatchOptions) (*DispatchRepo, error)

func (*DispatchRepo) BranchDiff

func (d *DispatchRepo) BranchDiff(target string) (Diff, error)

func (*DispatchRepo) CommitDiff

func (d *DispatchRepo) CommitDiff(hash string) (Diff, error)

func (*DispatchRepo) Diff added in v0.9.0

func (d *DispatchRepo) Diff(args []string) (Diff, error)

func (*DispatchRepo) FileDiff

func (d *DispatchRepo) FileDiff(path string) (Diff, error)

func (*DispatchRepo) RangeDiff

func (d *DispatchRepo) RangeDiff(target, feature string) (Diff, error)

func (*DispatchRepo) Root

func (d *DispatchRepo) Root() string

func (*DispatchRepo) StagedDiff

func (d *DispatchRepo) StagedDiff() (Diff, error)

func (*DispatchRepo) UnstagedDiff

func (d *DispatchRepo) UnstagedDiff() (Diff, error)

type GoGitRepo

type GoGitRepo struct {
	// contains filtered or unexported fields
}

func NewGoGitRepo

func NewGoGitRepo(root string) (*GoGitRepo, error)

func (*GoGitRepo) BranchDiff

func (g *GoGitRepo) BranchDiff(target string) (Diff, error)

func (*GoGitRepo) CommitDiff

func (g *GoGitRepo) CommitDiff(hash string) (Diff, error)

func (*GoGitRepo) Diff added in v0.9.0

func (g *GoGitRepo) Diff([]string) (Diff, error)

Diff (arbitrary `git diff <args>` passthrough) is intentionally unsupported on the go-git backend — re-implementing every git diff CLI shorthand (HEAD, A..B, A...B, --merge-base, etc.) by hand would balloon the surface area and drift. DispatchRepo falls back to the CLI here.

func (*GoGitRepo) FileDiff

func (g *GoGitRepo) FileDiff(string) (Diff, error)

func (*GoGitRepo) RangeDiff

func (g *GoGitRepo) RangeDiff(target, feature string) (Diff, error)

func (*GoGitRepo) Root

func (g *GoGitRepo) Root() string

func (*GoGitRepo) StagedDiff

func (g *GoGitRepo) StagedDiff() (Diff, error)

func (*GoGitRepo) UnstagedDiff

func (g *GoGitRepo) UnstagedDiff() (Diff, error)

type Origin

type Origin string
const (
	OriginStaged   Origin = "staged"
	OriginUnstaged Origin = "unstaged"
	OriginFile     Origin = "file"
	OriginCommit   Origin = "commit"
	OriginRange    Origin = "range"
	OriginBranch   Origin = "branch"
	OriginDiff     Origin = "diff" // `commitbrief diff <args...>` passthrough
	// OriginFiltered marks a diff assembled from an explicitly selected set
	// of commits (the --author / --start-date / --end-date / --text filters).
	// Unlike every other origin its content is a CONCATENATION of per-commit
	// patches, so the same path may appear as several file entries.
	OriginFiltered Origin = "filtered"
)

type Repo

type Repo interface {
	StagedDiff() (Diff, error)
	UnstagedDiff() (Diff, error)
	FileDiff(path string) (Diff, error)
	CommitDiff(hash string) (Diff, error)
	RangeDiff(target, feature string) (Diff, error)
	BranchDiff(target string) (Diff, error)
	// Diff is the generic `git diff <args>` passthrough used by the
	// `commitbrief diff` subcommand. args are forwarded verbatim
	// after `--no-color --no-ext-diff` so the renderer/parser see
	// stable unified-diff output. Backends that can't faithfully
	// implement arbitrary git arg combinations may return
	// ErrUnsupported; the DispatchRepo then falls through to the
	// CLI path.
	Diff(args []string) (Diff, error)
	Root() string
}

type Selection added in v1.15.0

type Selection struct {
	Commits []CommitMeta // newest first

	// Walked is how many commits phase A inspected.
	Walked int
	// Truncated is set when more commits matched than MaxCommits allowed.
	Truncated bool
	// WalkTruncated is set when phase A itself hit walkLimit, meaning older
	// history was never inspected at all.
	WalkTruncated bool
}

Selection is the outcome of a commit walk: the matching commits plus enough accounting for the caller to report honestly what was and wasn't covered.

func SelectCommits added in v1.15.0

func SelectCommits(ctx context.Context, repoRoot string, f CommitFilter) (Selection, error)

SelectCommits runs phases A and A′ and returns the matching commits, newest first, capped at f.MaxCommits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL