Documentation ¶
Overview ¶
Package diff is a generated protocol buffer package.
It is generated from these files:
diff.proto
It has these top-level messages:
FileDiff Hunk Stat
Package diff provides a parser for unified diffs.
Index ¶
- Variables
- func PrintFileDiff(d *FileDiff) ([]byte, error)
- func PrintHunks(hunks []*Hunk) ([]byte, error)
- func PrintMultiFileDiff(ds []*FileDiff) ([]byte, error)
- type ErrBadHunkHeader
- type ErrBadHunkLine
- type FileDiff
- type FileDiffReader
- func (r *FileDiffReader) HunksReader() *HunksReader
- func (r *FileDiffReader) Read() (*FileDiff, error)
- func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error)
- func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error)
- func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, err error)
- type Hunk
- type HunksReader
- type MultiFileDiffReader
- type ParseError
- type Stat
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoFileHeader is when a file unified diff has no file header // (i.e., the lines that begin with "---" and "+++"). ErrNoFileHeader = errors.New("expected file header, got EOF") // ErrBadFileHeader is when a file unified diff has a malformed // file header (i.e., the lines that begin with "---" and "+++"). ErrBadFileHeader = errors.New("bad file header") // ErrExtendedHeadersEOF is when an EOF was encountered while reading extended file headers, which means that there were no ---/+++ headers encountered before hunks (if any) began. ErrExtendedHeadersEOF = errors.New("expected file header while reading extended headers, got EOF") )
var ErrNoHunkHeader = errors.New("no hunk header")
ErrNoHunkHeader indicates that a unified diff hunk header was expected but not found during parsing.
Functions ¶
func PrintFileDiff ¶
PrintFileDiff prints a FileDiff in unified diff format.
TODO(sqs): handle escaping whitespace/etc. chars in filenames
func PrintHunks ¶
PrintHunks prints diff hunks in unified diff format.
func PrintMultiFileDiff ¶
PrintMultiFileDiff prints a multi-file diff in unified diff format.
Types ¶
type ErrBadHunkHeader ¶
type ErrBadHunkHeader struct {
// contains filtered or unexported fields
}
ErrBadHunkHeader indicates that a malformed unified diff hunk header was encountered during parsing.
func (*ErrBadHunkHeader) Error ¶
func (e *ErrBadHunkHeader) Error() string
type ErrBadHunkLine ¶
type ErrBadHunkLine struct {
Line []byte
}
ErrBadHunkLine is when a line not beginning with ' ', '-', '+', or '\' is encountered while reading a hunk. In the context of reading a single hunk or file, it is an unexpected error. In a multi-file diff, however, it indicates that the current file's diff is complete (and remaining diff data will describe another file unified diff).
func (*ErrBadHunkLine) Error ¶
func (e *ErrBadHunkLine) Error() string
type FileDiff ¶
type FileDiff struct { // the original name of the file OrigName string `protobuf:"bytes,1,opt,name=orig_name,proto3" json:"orig_name,omitempty"` // the original timestamp (nil if not present) OrigTime *pbtypes.Timestamp `protobuf:"bytes,2,opt,name=orig_time" json:"orig_time,omitempty"` // the new name of the file (often same as OrigName) NewName string `protobuf:"bytes,3,opt,name=new_name,proto3" json:"new_name,omitempty"` // the new timestamp (nil if not present) NewTime *pbtypes.Timestamp `protobuf:"bytes,4,opt,name=new_time" json:"new_time,omitempty"` // extended header lines (e.g., git's "new mode <mode>", "rename from <path>", etc.) Extended []string `protobuf:"bytes,5,rep,name=extended" json:"extended,omitempty"` // hunks that were changed from orig to new Hunks []*Hunk `protobuf:"bytes,6,rep,name=hunks" json:"hunks,omitempty"` }
A FileDiff represents a unified diff for a single file.
A file unified diff has a header that resembles the following:
--- oldname 2009-10-11 15:12:20.000000000 -0700 +++ newname 2009-10-11 15:12:30.000000000 -0700
func ParseFileDiff ¶
ParseFileDiff parses a file unified diff.
func ParseMultiFileDiff ¶
ParseMultiFileDiff parses a multi-file unified diff. It returns an error if parsing failed as a whole, but does its best to parse as many files in the case of per-file errors. In the case of non-fatal per-file errors, the error return value is null and the Errs field in the returned MultiFileDiff is set.
func (*FileDiff) GetNewTime ¶
func (*FileDiff) GetOrigTime ¶
func (*FileDiff) ProtoMessage ¶
func (*FileDiff) ProtoMessage()
type FileDiffReader ¶
type FileDiffReader struct {
// contains filtered or unexported fields
}
FileDiffReader reads a unified file diff.
func NewFileDiffReader ¶
func NewFileDiffReader(r io.Reader) *FileDiffReader
NewFileDiffReader returns a new FileDiffReader that reads a file unified diff.
func (*FileDiffReader) HunksReader ¶
func (r *FileDiffReader) HunksReader() *HunksReader
HunksReader returns a new HunksReader that reads hunks from r. The HunksReader's line and offset (used in error messages) is set to start where the file diff header ended (which means errors have the correct position information).
func (*FileDiffReader) Read ¶
func (r *FileDiffReader) Read() (*FileDiff, error)
Read reads a file unified diff, including headers and hunks, from r.
func (*FileDiffReader) ReadAllHeaders ¶
func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error)
ReadAllHeaders reads the file headers and extended headers (if any) from a file unified diff. It does not read hunks, and the returned FileDiff's Hunks field is nil. To read the hunks, call the (*FileDiffReader).HunksReader() method to get a HunksReader and read hunks from that.
func (*FileDiffReader) ReadExtendedHeaders ¶
func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error)
ReadExtendedHeaders reads the extended header lines, if any, from a unified diff file (e.g., git's "diff --git a/foo.go b/foo.go", "new mode <mode>", "rename from <path>", etc.).
func (*FileDiffReader) ReadFileHeaders ¶
func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, err error)
ReadFileHeaders reads the unified file diff header (the lines that start with "---" and "+++" with the orig/new file names and timestamps).
type Hunk ¶
type Hunk struct { // starting line number in original file OrigStartLine int `protobuf:"varint,1,opt,name=orig_start_line,proto3,customtype=int" json:"orig_start_line,omitempty"` // number of lines the hunk applies to in the original file OrigLines int `protobuf:"varint,2,opt,name=orig_lines,proto3,customtype=int" json:"orig_lines,omitempty"` // if > 0, then the original file had a 'No newline at end of file' mark at this offset OrigNoNewlineAt int `protobuf:"varint,3,opt,name=orig_no_newline_at,proto3,customtype=int" json:"orig_no_newline_at,omitempty"` // starting line number in new file NewStartLine int `protobuf:"varint,4,opt,name=new_start_line,proto3,customtype=int" json:"new_start_line,omitempty"` // number of lines the hunk applies to in the new file NewLines int `protobuf:"varint,5,opt,name=new_lines,proto3,customtype=int" json:"new_lines,omitempty"` // optional section heading Section string `protobuf:"bytes,6,opt,name=section,proto3" json:"section,omitempty"` // 0-indexed line offset in unified file diff (including section headers); this is // only set when Hunks are read from entire file diff (i.e., when ReadAllHunks is // called) This accounts for hunk headers, too, so the StartPosition of the first // hunk will be 1. StartPosition int `protobuf:"varint,7,opt,name=start_position,proto3,customtype=int" json:"start_position,omitempty"` // hunk body (lines prefixed with '-', '+', or ' ') Body []byte `protobuf:"bytes,8,opt,name=body,proto3" json:"body,omitempty"` }
A Hunk represents a series of changes (additions or deletions) in a file's unified diff.
func ParseHunks ¶
ParseHunks parses hunks from a unified diff. The diff must consist only of hunks and not include a file header; if it has a file header, use ParseFileDiff.
func (*Hunk) ProtoMessage ¶
func (*Hunk) ProtoMessage()
type HunksReader ¶
type HunksReader struct {
// contains filtered or unexported fields
}
A HunksReader reads hunks from a unified diff.
func NewHunksReader ¶
func NewHunksReader(r io.Reader) *HunksReader
NewHunksReader returns a new HunksReader that reads unified diff hunks from r.
func (*HunksReader) ReadAllHunks ¶
func (r *HunksReader) ReadAllHunks() ([]*Hunk, error)
ReadAllHunks reads all remaining hunks from r. A successful call returns err == nil, not err == EOF. Because ReadAllHunks is defined to read until EOF, it does not treat end of file as an error to be reported.
func (*HunksReader) ReadHunk ¶
func (r *HunksReader) ReadHunk() (*Hunk, error)
ReadHunk reads one hunk from r. If there are no more hunks, it returns error io.EOF.
type MultiFileDiffReader ¶
type MultiFileDiffReader struct {
// contains filtered or unexported fields
}
MultiFileDiffReader reads a multi-file unified diff.
func NewMultiFileDiffReader ¶
func NewMultiFileDiffReader(r io.Reader) *MultiFileDiffReader
NewMultiFileDiffReader returns a new MultiFileDiffReader that reads a multi-file unified diff from r.
func (*MultiFileDiffReader) ReadAllFiles ¶
func (r *MultiFileDiffReader) ReadAllFiles() ([]*FileDiff, error)
ReadAllFiles reads all file unified diffs (including headers and all hunks) remaining in r.
func (*MultiFileDiffReader) ReadFile ¶
func (r *MultiFileDiffReader) ReadFile() (*FileDiff, error)
ReadFile reads the next file unified diff (including headers and all hunks) from r. If there are no more files in the diff, it returns error io.EOF.
type ParseError ¶
type ParseError struct { Line int // Line where the error occurred Offset int64 // Offset where the error occurred Err error // The actual error }
A ParseError is a description of a unified diff syntax error.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Stat ¶
type Stat struct { // number of lines added Added int `protobuf:"varint,1,opt,name=added,proto3,customtype=int" json:"added,omitempty"` // number of lines changed Changed int `protobuf:"varint,2,opt,name=changed,proto3,customtype=int" json:"changed,omitempty"` // number of lines deleted Deleted int `protobuf:"varint,3,opt,name=deleted,proto3,customtype=int" json:"deleted,omitempty"` }
A Stat is a diff stat that represents the number of lines added/changed/deleted.
func (*Stat) ProtoMessage ¶
func (*Stat) ProtoMessage()