|
1 | 1 | package diff
|
2 | 2 |
|
3 |
| -import "bytes" |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "time" |
| 6 | +) |
4 | 7 |
|
5 |
| -// NOTE: types are code-generated in diff.pb.go. |
| 8 | +// A FileDiff represents a unified diff for a single file. |
| 9 | +// |
| 10 | +// A file unified diff has a header that resembles the following: |
| 11 | +// |
| 12 | +// --- oldname 2009-10-11 15:12:20.000000000 -0700 |
| 13 | +// +++ newname 2009-10-11 15:12:30.000000000 -0700 |
| 14 | +type FileDiff struct { |
| 15 | + // the original name of the file |
| 16 | + OrigName string |
| 17 | + // the original timestamp (nil if not present) |
| 18 | + OrigTime *time.Time |
| 19 | + // the new name of the file (often same as OrigName) |
| 20 | + NewName string |
| 21 | + // the new timestamp (nil if not present) |
| 22 | + NewTime *time.Time |
| 23 | + // extended header lines (e.g., git's "new mode <mode>", "rename from <path>", etc.) |
| 24 | + Extended []string |
| 25 | + // hunks that were changed from orig to new |
| 26 | + Hunks []*Hunk |
| 27 | +} |
6 | 28 |
|
7 |
| -//go:generate protoc -I../../../.. -I ../../../../github.com/gogo/protobuf/protobuf -I. --gogo_out=. diff.proto |
| 29 | +// A Hunk represents a series of changes (additions or deletions) in a file's |
| 30 | +// unified diff. |
| 31 | +type Hunk struct { |
| 32 | + // starting line number in original file |
| 33 | + OrigStartLine int32 |
| 34 | + // number of lines the hunk applies to in the original file |
| 35 | + OrigLines int32 |
| 36 | + // if > 0, then the original file had a 'No newline at end of file' mark at this offset |
| 37 | + OrigNoNewlineAt int32 |
| 38 | + // starting line number in new file |
| 39 | + NewStartLine int32 |
| 40 | + // number of lines the hunk applies to in the new file |
| 41 | + NewLines int32 |
| 42 | + // optional section heading |
| 43 | + Section string |
| 44 | + // 0-indexed line offset in unified file diff (including section headers); this is |
| 45 | + // only set when Hunks are read from entire file diff (i.e., when ReadAllHunks is |
| 46 | + // called) This accounts for hunk headers, too, so the StartPosition of the first |
| 47 | + // hunk will be 1. |
| 48 | + StartPosition int32 |
| 49 | + // hunk body (lines prefixed with '-', '+', or ' ') |
| 50 | + Body []byte |
| 51 | +} |
| 52 | + |
| 53 | +// A Stat is a diff stat that represents the number of lines added/changed/deleted. |
| 54 | +type Stat struct { |
| 55 | + // number of lines added |
| 56 | + Added int32 |
| 57 | + // number of lines changed |
| 58 | + Changed int32 |
| 59 | + // number of lines deleted |
| 60 | + Deleted int32 |
| 61 | +} |
8 | 62 |
|
9 | 63 | // Stat computes the number of lines added/changed/deleted in all
|
10 | 64 | // hunks in this file's diff.
|
|
0 commit comments