Documentation
¶
Overview ¶
Package diff turns unified diff text into a structure the UI can render, and turns a selection of hunks back into a patch git can apply.
Index ¶
Constants ¶
const DefaultContext = 3
DefaultContext is how many unchanged lines surround a hunk by default — git's -U3.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate diffs two files or two directories and returns unified diff text.
ponytail: generate-then-reparse. Producing text and handing it straight back to Parse keeps one interpretation of a diff in the codebase instead of two. Swap in a direct []File builder only if profiling says this matters.
func NewFilePatch ¶
NewFilePatch renders an untracked file as an add-everything patch, so a file git has never seen can be reviewed and staged like any other change.
Types ¶
type File ¶
type File struct {
OldPath string
NewPath string
IsNew bool
IsDelete bool
IsRename bool
IsBinary bool
Hunks []Hunk
Added int
Removed int
}
File is one file's worth of changes.
func ParseString ¶
ParseString reads unified diff text. Every input to hunk becomes unified diff text first, so this is the only place a diff is interpreted.
func (File) Patch ¶
Patch renders a patch containing only the named hunks of a file, ready for "git apply --cached".
The hunks are emitted exactly as they were parsed, never recomputed: git is asked to recount the line numbers, so no arithmetic of ours can silently corrupt a patch and stage the wrong thing.
type Hunk ¶
type Hunk struct {
Header string // the full "@@ -1,4 +1,6 @@ func foo()" line
OldStart int
NewStart int
Lines []Line
// contains filtered or unexported fields
}
Hunk is one @@ block.
func (Hunk) Key ¶
Key is a content-stable identity for a hunk: a hash of what each line does and says, but not of its line numbers. Two hunks with the same body match even after edits elsewhere shift their positions, and a hunk stops matching the moment its own content changes — which is how live-follow tells an untouched mark from one that needs re-approving.
type Line ¶
Line is one line of a hunk. OldNum/NewNum are 1-based line numbers in the old and new file; they are 0 when the line does not exist on that side.
type Range ¶
type Range struct {
Start, End int
}
Range is a half-open byte range [Start, End) inside a line.
func Intraline ¶
Intraline finds the parts of two paired lines that actually differ, so a one-word change reads as a one-word change instead of a whole red line next to a whole green one.
Ranges are byte ranges into before and after respectively. It returns nothing when the lines are too different to be worth comparing.