Documentation
¶
Overview ¶
Package group turns a surveyed working tree into a set of planned commits.
Grouping is the one part of preen that is judgment rather than mechanics, so it sits behind an interface. The built-in grouper is deterministic and needs no model, which keeps preen useful on its own; a model-backed grouper can be substituted where better judgment is worth the cost.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoCommand reports a command grouper with no program to run. ErrNoCommand = errors.New("no grouper command configured") // ErrCommand reports a grouper program that failed to run. ErrCommand = errors.New("grouper command failed") // ErrRequest reports a request that could not be encoded. ErrRequest = errors.New("cannot build the grouper request") // ErrResponse reports an answer preen cannot trust: unparsable, empty, or // naming a path or hunk that is not in the tree. ErrResponse = errors.New("unusable grouper response") // ErrStale reports an answer written for a tree other than the one being // preened. ErrStale = errors.New("answer does not match this tree") // ErrAnswer reports an answer file that could not be read. ErrAnswer = errors.New("cannot read the grouping answer") )
Sentinel errors for grouping.
Functions ¶
This section is empty.
Types ¶
type Answer ¶
type Answer struct {
// Path is the file holding the response.
Path string
}
Answer groups changes as a response written ahead of time says. It exists for the agent that made the changes: that agent knows why each hunk exists rather than inferring it from the diff, so it answers the request from `preen request` itself and preen verifies the answer like any other.
Unlike Command, an Answer is never paired with a fallback. Whoever wrote it is waiting on the result, so a stale or malformed answer is an error to fix, not a reason to quietly plan something else.
type Command ¶
type Command struct {
// Name is the program to run.
Name string
// Args are the arguments passed to it.
Args []string
// Dir is the working directory for the program.
Dir string
}
Command groups changes by handing them to an external program.
The program receives the request as JSON on stdin and returns a Response as JSON on stdout. That contract is deliberately provider agnostic: any model CLI, script, or service wrapper can be a grouper without preen knowing anything about it, and none of them can touch the repository.
type Grouper ¶
type Grouper interface {
// Group clusters the input into commits, in the order they should be
// recorded.
Group(ctx context.Context, in Input) ([]plan.Commit, error)
}
Grouper clusters changes into ordered commits. It returns commits only; the caller validates them against the tree and builds the surrounding plan.
type GrouperFunc ¶
GrouperFunc adapts a function to the Grouper interface.
type Heuristic ¶
type Heuristic struct {
// RespectStaged treats already-staged paths as a boundary the user drew by
// hand and keeps them in their own commit.
RespectStaged bool
// MaxSubject is the subject length generated subjects aim to fit. Zero means
// the default the style layer applies.
MaxSubject int
}
Heuristic groups changes without a model, using path conventions alone.
It never splits a file across commits: a deterministic grouper cannot know whether two hunks in one file are one idea or two, and guessing wrong is worse than a slightly coarse commit. Splitting is left to a model-backed grouper or an explicit edit.
Within a directory it works in units rather than files. A unit is a source file plus the tests that exercise it, which is the one pairing a file name states outright. Units that only add code are separated from units that change code already there, and tests written for code this run did not touch stand on their own. Nothing here reads a diff, so the shape of the plan follows from the file list and is the same every time.
func NewHeuristic ¶
func NewHeuristic() Heuristic
NewHeuristic returns a Heuristic with the default behavior, which honors a hand-staged boundary.
type Input ¶
type Input struct {
// Changes is every changed path, including untracked files.
Changes []repo.Change
// Diffs are the per-file patches, which carry the hunks a grouper may
// split across commits.
Diffs []repo.FileDiff
// Tree is the content hash of the tree the changes were read from, which an
// answer written ahead of time must match.
Tree string
}
Input is everything a grouper sees about the working tree.
type Request ¶
type Request struct {
// Tree is the content hash of the tree the request describes. An answer
// given to --grouping must carry it back, which proves the hunk indexes it
// names were read from this tree and not an earlier one.
Tree string `json:"tree"`
// Files are the changed files and the hunks available to divide.
Files []RequestFile `json:"files"`
}
Request is what a command grouper is asked to solve, written as JSON on stdin. It carries the changes and their hunks, never the repository itself, so a grouper cannot act on anything: it only answers.
func NewRequest ¶
NewRequest turns the survey into the grouper's input.
type RequestFile ¶
type RequestFile struct {
// Path is the file, relative to the repository root.
Path string `json:"path"`
// Kind is what happened to it.
Kind string `json:"kind"`
// Staged reports that the user staged this path by hand.
Staged bool `json:"staged"`
// Splittable reports whether the hunks may be divided across commits.
Splittable bool `json:"splittable"`
// Hunks are the file's hunks, in order.
Hunks []RequestHunk `json:"hunks,omitempty"`
}
RequestFile is one file offered to the grouper.
type RequestHunk ¶
type RequestHunk struct {
// Index is the hunk's position in the file's diff.
Index int `json:"index"`
// Header is the @@ line, which names the enclosing function when git knows
// it.
Header string `json:"header"`
// Text is the hunk body, so the grouper can read what actually changed.
Text string `json:"text"`
}
RequestHunk is one hunk offered to the grouper.
type Response ¶
type Response struct {
// Tree echoes the request's tree. A command grouper may omit it, since it
// answers the request it was just handed; an answer file may not.
Tree string `json:"tree,omitempty"`
// Commits are the proposed commits, in the order they should be recorded.
Commits []ResponseCommit `json:"commits"`
}
Response is what a command grouper returns on stdout.
type ResponseCommit ¶
type ResponseCommit struct {
// Subject is the commit subject.
Subject string `json:"subject"`
// Body is the optional message body.
Body string `json:"body,omitempty"`
// Parts are the file contributions.
Parts []ResponsePart `json:"parts"`
}
ResponseCommit is one proposed commit.
type ResponsePart ¶
type ResponsePart struct {
// Path is the file.
Path string `json:"path"`
// Hunks are the hunk indexes this commit takes. Omitting them takes the
// whole file.
Hunks []int `json:"hunks,omitempty"`
}
ResponsePart is one file's contribution to a proposed commit.