Skip to content

Commit 1ee22f3

Browse files
committed
raw line out and channel response from parse
1 parent b68e1cf commit 1ee22f3

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

gitdiff/gitdiff.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"strings"
78
)
89

910
// File describes changes to a single file. It can be either a text file or a
@@ -24,7 +25,7 @@ type File struct {
2425
NewOIDPrefix string
2526
Score int
2627

27-
PatchHeader *PatchHeader
28+
PatchHeader *PatchHeader
2829

2930
// TextFragments contains the fragments describing changes to a text file. It
3031
// may be empty if the file is empty or if only the mode changes.
@@ -59,6 +60,16 @@ type TextFragment struct {
5960
Lines []Line
6061
}
6162

63+
func (f *TextFragment) Raw(op LineOp) string {
64+
sb := strings.Builder{}
65+
for _, l := range f.Lines {
66+
if l.Op == op {
67+
sb.WriteString(l.Line)
68+
}
69+
}
70+
return sb.String()
71+
}
72+
6273
// Header returns the canonical header of this fragment.
6374
func (f *TextFragment) Header() string {
6475
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)

gitdiff/parser.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,54 @@ const commitPrefix = "commit"
1515
// Parse parses a patch with changes to one or more files. Any content before
1616
// the first file is returned as the second value. If an error occurs while
1717
// parsing, it returns all files parsed before the error.
18-
func Parse(r io.Reader) ([]*File, string, error) {
18+
func Parse(r io.Reader) (<-chan *File, error) {
1919
p := newParser(r)
20+
out := make(chan *File)
2021

2122
if err := p.Next(); err != nil {
2223
if err == io.EOF {
23-
return nil, "", nil
24+
return out, nil
2425
}
25-
return nil, "", err
26+
return out, err
2627
}
2728

28-
var preamble string
29-
var files []*File
30-
var ph *PatchHeader
31-
for {
32-
file, pre, err := p.ParseNextFileHeader()
33-
if err != nil {
34-
return files, preamble, err
35-
}
36-
37-
if strings.Contains(pre, commitPrefix) {
38-
ph, _ = ParsePatchHeader(pre)
39-
}
40-
41-
if file == nil {
42-
break
43-
}
44-
45-
for _, fn := range []func(*File) (int, error){
46-
p.ParseTextFragments,
47-
p.ParseBinaryFragments,
48-
} {
49-
n, err := fn(file)
29+
go func() {
30+
ph := &PatchHeader{}
31+
for {
32+
file, pre, err := p.ParseNextFileHeader()
5033
if err != nil {
51-
return files, preamble, err
34+
out <- file
35+
return
5236
}
53-
if n > 0 {
37+
38+
if strings.Contains(pre, commitPrefix) {
39+
ph, _ = ParsePatchHeader(pre)
40+
}
41+
42+
if file == nil {
5443
break
5544
}
56-
}
5745

58-
file.PatchHeader = ph
59-
if len(files) == 0 {
60-
preamble = pre
46+
for _, fn := range []func(*File) (int, error){
47+
p.ParseTextFragments,
48+
p.ParseBinaryFragments,
49+
} {
50+
n, err := fn(file)
51+
if err != nil {
52+
return
53+
}
54+
if n > 0 {
55+
break
56+
}
57+
}
58+
59+
file.PatchHeader = ph
60+
out <- file
6161
}
62-
files = append(files, file)
63-
}
62+
close(out)
63+
}()
6464

65-
return files, preamble, nil
65+
return out, nil
6666
}
6767

6868
// TODO(bkeyes): consider exporting the parser type with configuration

0 commit comments

Comments
 (0)