absorb

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 6 Imported by: 1 module, 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

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

View Source
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

func Patch(path string, hunks []Hunk) string

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. git apply takes a hunk's new start as the line, in the file with the hunks above already applied, where its removed lines begin (or before which its added lines go), and reports an offset when they are anywhere else. That is git diff's numbering except for a deletion, where git diff gives the line before the gap; here a deletion names its first removed line instead, so a correct patch applies exactly where it says.

func PatchFiles

func PatchFiles(files map[string][]Hunk) string

PatchFiles renders every file's hunks, files in path order, as one patch.

Types

type Commit

type Commit struct {
	SHA, Subject, Branch string
	Files                []FileDiff
}

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

func Parse(diff string) ([]FileDiff, error)

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.

func (FileDiff) IsDeleted

func (f FileDiff) IsDeleted() bool

IsDeleted reports whether the file was deleted by this diff.

func (FileDiff) IsNew

func (f FileDiff) IsNew() bool

IsNew reports whether the file was created by this diff.

func (FileDiff) Path

func (f FileDiff) Path() string

Path is the file's path for reports: the new one, or the old one for a deleted file.

func (FileDiff) Renamed

func (f FileDiff) Renamed() bool

Renamed reports whether the file changed path.

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.

func (Hunk) Net

func (h Hunk) Net() int

Net is how many lines the hunk adds to the file (negative for a removal).

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

func Compute(commits []Commit, staged []FileDiff) Plan

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) Absorbed

func (p *Plan) Absorbed() int

Absorbed counts the hunks with a destination.

func (*Plan) Targets

func (p *Plan) Targets() int

Targets counts the distinct destination commits.

func (*Plan) Unabsorbed

func (p *Plan) Unabsorbed() int

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.

type Target

type Target struct {
	SHA, Subject, Branch string
}

Target is the commit a hunk goes into.

Jump to

Keyboard shortcuts

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