mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
2c9be20fca
Track leading and trailing context lines because it's easy and Git includes this information in the fragment type. Add validation for when the fragment does not agree with the header or with the new/deleted state of the file.
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package gitdiff
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// File describes changes to a single file. It can be either a text file or a
|
|
// binary file.
|
|
type File struct {
|
|
OldName string
|
|
NewName string
|
|
|
|
IsNew bool
|
|
IsDelete bool
|
|
IsCopy bool
|
|
IsRename bool
|
|
|
|
OldMode os.FileMode
|
|
NewMode os.FileMode
|
|
|
|
OldOIDPrefix string
|
|
NewOIDPrefix string
|
|
Score int
|
|
|
|
Fragments []*Fragment
|
|
}
|
|
|
|
// Fragment describes changed lines starting at a specific line in a text file.
|
|
type Fragment struct {
|
|
Comment string
|
|
|
|
OldPosition int64
|
|
OldLines int64
|
|
|
|
NewPosition int64
|
|
NewLines int64
|
|
|
|
LinesAdded int64
|
|
LinesDeleted int64
|
|
|
|
LeadingContext int64
|
|
TrailingContext int64
|
|
|
|
Lines []FragmentLine
|
|
}
|
|
|
|
// FragmentLine is a line in a fragment.
|
|
type FragmentLine struct {
|
|
Op LineOp
|
|
Line string
|
|
}
|
|
|
|
// LineOp describes the type of a fragment line: context, added, or removed.
|
|
type LineOp int
|
|
|
|
const (
|
|
// OpContext indicates a context line
|
|
OpContext LineOp = iota
|
|
// OpDelete indicates a deleted line
|
|
OpDelete
|
|
// OpAdd indicates an added line
|
|
OpAdd
|
|
)
|
|
|
|
// Header returns the cannonical header of this fragment.
|
|
func (f *Fragment) Header() string {
|
|
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
|
|
}
|