Skip to content

Commit 21eb011

Browse files
committed
use protobufs
1 parent 07d9929 commit 21eb011

File tree

5 files changed

+221
-49
lines changed

5 files changed

+221
-49
lines changed

diff/diff.go

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
package diff
22

3-
import (
4-
"bytes"
5-
"time"
6-
)
7-
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-
OrigName string // the original name of the file
16-
OrigTime *time.Time // the original timestamp (nil if not present)
17-
18-
NewName string // the new name of the file (often same as OrigName)
19-
NewTime *time.Time // the new timestamp (nil if not present)
3+
import "bytes"
204

21-
Extended []string // extended header lines (e.g., git's "new mode <mode>", "rename from <path>", etc.)
5+
// NOTE: types are code-generated in diff.pb.go.
226

23-
Hunks []*Hunk // hunks that were changed from orig to new
24-
}
7+
//go:generate protoc -I../../../../github.com/gogo/protobuf/protobuf -I../../../../github.com/gogo/protobuf -I../../../../sourcegraph.com/sqs/pbtypes -I. --gogo_out=. diff.proto
8+
//go:generate sed -i "s#timestamp\\.pb#sourcegraph.com/sqs/pbtypes#g" diff.pb.go
9+
//go:generate sed -i "s#vcs\\.pb#sourcegraph.com/sourcegraph/go-vcs/vcs#g" diff.pb.go
2510

2611
// Stat computes the number of lines added/changed/deleted in all
2712
// hunks in this file's diff.
@@ -33,26 +18,6 @@ func (d *FileDiff) Stat() Stat {
3318
return total
3419
}
3520

36-
// A Hunk represents a series of changes (additions or deletions) in a
37-
// file's unified diff.
38-
type Hunk struct {
39-
OrigStartLine int // starting line number in original file
40-
OrigLines int // number of lines the hunk applies to in the original file
41-
OrigNoNewlineAt int // if > 0, then the original file had a 'No newline at end of file' mark at this offset
42-
43-
NewStartLine int // starting line number in new file
44-
NewLines int // number of lines the hunk applies to in the new file
45-
46-
Section string // optional section heading
47-
48-
// 0-indexed line offset in unified file diff (including section headers);
49-
// this is only set when Hunks are read from entire file diff (i.e., when ReadAllHunks is called)
50-
// This accounts for hunk headers, too, so the StartPosition of the first hunk will be 1.
51-
StartPosition int
52-
53-
Body []byte // hunk body (lines prefixed with '-', '+', or ' ')
54-
}
55-
5621
// Stat computes the number of lines added/changed/deleted in this
5722
// hunk.
5823
func (h *Hunk) Stat() Stat {
@@ -101,12 +66,6 @@ const hunkHeader = "@@ -%d,%d +%d,%d @@"
10166
// http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html.
10267
const diffTimeFormat = "2006-01-02 15:04:05.000000000 -0700"
10368

104-
// A Stat is a diff stat that represents the number of lines
105-
// added/changed/deleted.
106-
type Stat struct {
107-
Added, Changed, Deleted int // numbers of lines
108-
}
109-
11069
func (s *Stat) add(o Stat) {
11170
s.Added += o.Added
11271
s.Changed += o.Changed

diff/diff.pb.go

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

diff/diff.proto

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
syntax = "proto3";
2+
package diff;
3+
4+
import "gogoproto/gogo.proto";
5+
import "timestamp.proto";
6+
7+
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+
message FileDiff {
15+
// the original name of the file
16+
string orig_name = 1;
17+
18+
// the original timestamp (nil if not present)
19+
pbtypes.Timestamp orig_time = 2;
20+
21+
// the new name of the file (often same as OrigName)
22+
string new_name = 3;
23+
24+
// the new timestamp (nil if not present)
25+
pbtypes.Timestamp new_time = 4;
26+
27+
// extended header lines (e.g., git's "new mode <mode>", "rename from <path>", etc.)
28+
repeated string extended = 5;
29+
30+
// hunks that were changed from orig to new
31+
repeated Hunk hunks = 6;
32+
}
33+
34+
35+
// A Hunk represents a series of changes (additions or deletions) in a file's
36+
// unified diff.
37+
message Hunk {
38+
// starting line number in original file
39+
int32 orig_start_line = 1 [(gogoproto.customtype) = "int"];
40+
41+
// number of lines the hunk applies to in the original file
42+
int32 orig_lines = 2 [(gogoproto.customtype) = "int"];
43+
44+
// if > 0, then the original file had a 'No newline at end of file' mark at this offset
45+
int32 orig_no_newline_at = 3 [(gogoproto.customtype) = "int"];
46+
47+
// starting line number in new file
48+
int32 new_start_line = 4 [(gogoproto.customtype) = "int"];
49+
50+
// number of lines the hunk applies to in the new file
51+
int32 new_lines = 5 [(gogoproto.customtype) = "int"];
52+
53+
// optional section heading
54+
string section = 6;
55+
56+
// 0-indexed line offset in unified file diff (including section headers); this is
57+
// only set when Hunks are read from entire file diff (i.e., when ReadAllHunks is
58+
// called) This accounts for hunk headers, too, so the StartPosition of the first
59+
// hunk will be 1.
60+
int32 start_position = 7 [(gogoproto.customtype) = "int"];
61+
62+
// hunk body (lines prefixed with '-', '+', or ' ')
63+
bytes body = 8;
64+
}
65+
66+
// A Stat is a diff stat that represents the number of lines added/changed/deleted.
67+
message Stat {
68+
// number of lines added
69+
int32 added = 1 [(gogoproto.customtype) = "int"];
70+
71+
// number of lines changed
72+
int32 changed = 2 [(gogoproto.customtype) = "int"];
73+
74+
// number of lines deleted
75+
int32 deleted = 3 [(gogoproto.customtype) = "int"];
76+
}
77+

diff/parse.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
"strings"
1010
"time"
11+
12+
"sourcegraph.com/sqs/pbtypes"
1113
)
1214

1315
// ParseMultiFileDiff parses a multi-file unified diff. It returns an error if parsing failed as a whole, but does its
@@ -172,10 +174,19 @@ func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error) {
172174
return nil, err
173175
}
174176

175-
fd.OrigName, fd.NewName, fd.OrigTime, fd.NewTime, err = r.ReadFileHeaders()
177+
var origTime, newTime *time.Time
178+
fd.OrigName, fd.NewName, origTime, newTime, err = r.ReadFileHeaders()
176179
if err != nil {
177180
return nil, err
178181
}
182+
if origTime != nil {
183+
ts := pbtypes.NewTimestamp(*origTime)
184+
fd.OrigTime = &ts
185+
}
186+
if newTime != nil {
187+
ts := pbtypes.NewTimestamp(*newTime)
188+
fd.NewTime = &ts
189+
}
179190

180191
return fd, nil
181192
}

diff/print.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"time"
8+
9+
"sourcegraph.com/sqs/pbtypes"
810
)
911

1012
// PrintMultiFileDiff prints a multi-file diff in unified diff format.
@@ -34,10 +36,10 @@ func PrintFileDiff(d *FileDiff) ([]byte, error) {
3436
}
3537
}
3638

37-
if err := printFileHeader(&buf, "--- ", d.OrigName, d.OrigTime); err != nil {
39+
if err := printFileHeader(&buf, "--- ", d.OrigName, timePtr(d.OrigTime)); err != nil {
3840
return nil, err
3941
}
40-
if err := printFileHeader(&buf, "+++ ", d.NewName, d.NewTime); err != nil {
42+
if err := printFileHeader(&buf, "+++ ", d.NewName, timePtr(d.NewTime)); err != nil {
4143
return nil, err
4244
}
4345

@@ -52,6 +54,14 @@ func PrintFileDiff(d *FileDiff) ([]byte, error) {
5254
return buf.Bytes(), nil
5355
}
5456

57+
func timePtr(ts *pbtypes.Timestamp) *time.Time {
58+
if ts == nil {
59+
return nil
60+
}
61+
t := ts.Time()
62+
return &t
63+
}
64+
5565
func printFileHeader(w io.Writer, prefix string, filename string, timestamp *time.Time) error {
5666
if _, err := fmt.Fprint(w, prefix, filename); err != nil {
5767
return err

0 commit comments

Comments
 (0)