Documentation
¶
Overview ¶
Package absorb decides which commit of a stack each staged change belongs to, the way git stack absorb needs it: it reads diffs as text, walks each changed run of lines backwards through the stack's commits until one that changed those lines stops it, and writes the patches that put the change into that commit and every commit above it. It is pure; pkg/app does the git work around it.
Index ¶
Constants ¶
const ( ReasonNewFile = "new file" ReasonDeletedFile = "deleted file" ReasonBinary = "binary file" ReasonSubmodule = "submodule" ReasonNoText = "no text changes" )
Reasons a staged file is skipped. They stay staged.
Variables ¶
This section is empty.
Functions ¶
func Patch ¶
Patch renders hunks for one file as a zero context patch for git apply, the path on both sides. The hunks are sorted by old line (an insertion after a line goes after an edit of that line) and their new side numbers are recomputed so each one accounts for the ones before it, which is what git apply uses to place an insertion.
func PatchFiles ¶
PatchFiles renders every file's hunks, files in path order, as one patch.
Types ¶
type Commit ¶
Commit is a candidate destination: a commit of the stack with its diff against its first parent.
type FileDiff ¶
type FileDiff struct {
// OldPath and NewPath are the paths on each side, "" where the file does
// not exist (a new or deleted file).
OldPath, NewPath string
Hunks []Hunk
// Binary is set when git printed no text hunks because the file is
// binary; Submodule when the entry is a submodule pointer.
Binary, Submodule bool
}
FileDiff is one file's part of a diff.
func Parse ¶
Parse reads a diff as git prints it with zero lines of context and rename detection (diff -U0 -M, a/ and b/ prefixes) into one FileDiff per file. Should a hunk carry context lines anyway, it is split into one Hunk per stretch of changed lines, numbered as git would number each on its own, so every Hunk is exactly one contiguous change.
type Hunk ¶
type Hunk struct {
OldStart, OldCount int
NewStart, NewCount int
// Lines is the body exactly as the diff had it: "-" and "+" lines, with
// a "\ No newline at end of file" marker kept after the line it follows.
Lines []string
}
Hunk is one contiguous run of changed lines in one file: OldCount lines removed from the old file starting at OldStart, NewCount lines added to the new file starting at NewStart. Git's zero context conventions hold: an insertion has OldCount 0 and OldStart is the old line it goes after (0 for the top of the file); a deletion has NewCount 0 and NewStart is the new line before the gap. The planner only moves OldStart around; NewStart is what the diff said and is recomputed when a patch is written.
type HunkPlan ¶
type HunkPlan struct {
// Path is the file's path in the index (the new path of a rename).
Path string
Hunk Hunk
// Target is nil when no candidate commit changed the lines.
Target *Target
}
HunkPlan is one staged hunk and where it goes.
type Plan ¶
type Plan struct {
Hunks []HunkPlan
Skipped []SkippedFile
// Patches holds, for every commit to rewrite, the hunks to apply to its
// tree by the file's path in that tree.
Patches map[string]map[string][]Hunk
// Rewrites lists the commits to rewrite, oldest first: the contiguous
// run from the oldest destination up to the newest candidate. Every
// one of them has a patch, since the hunk that stops at the oldest
// destination was carried through all of the newer ones.
Rewrites []string
}
Plan is where every staged hunk goes and what it takes to put it there.
func Compute ¶
Compute decides a destination for every staged hunk. commits run oldest first (bottom branch first, as the stack has them); staged is the index against the newest of them. Each hunk starts at the newest commit with its HEAD line numbers and is moved backwards past every commit that doesn't touch its lines; the first commit that does, or the one that created the file, takes it. A hunk that passes every commit is left without a Target.
func (*Plan) Unabsorbed ¶
Unabsorbed counts the hunks no commit took.
type SkippedFile ¶
type SkippedFile struct {
Path, Reason string
}
SkippedFile is a staged change absorb does not plan, with the reason.