1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
package gitdiff
import (
"fmt"
"io"
"strconv"
"strings"
)
// ParseTextFragments parses text fragments until the next file header or the
// end of the stream and attaches them to the given file. It returns the number
// of fragments that were added.
func (p *parser) ParseTextFragments(f *File) (n int, err error) {
for {
frag, err := p.ParseTextFragmentHeader()
if err != nil {
return n, err
}
if frag == nil {
return n, nil
}
if f.IsNew && frag.OldLines > 0 {
return n, p.Errorf(-1, "new file depends on old contents")
}
if f.IsDelete && frag.NewLines > 0 {
return n, p.Errorf(-1, "deleted file still has contents")
}
if err := p.ParseTextChunk(frag); err != nil {
return n, err
}
f.TextFragments = append(f.TextFragments, frag)
n++
}
}
func (p *parser) ParseTextFragmentHeader() (*TextFragment, error) {
const (
startMark = "@@ -"
endMark = " @@"
)
if !strings.HasPrefix(p.Line(0), startMark) {
return nil, nil
}
parts := strings.SplitAfterN(p.Line(0), endMark, 2)
if len(parts) < 2 {
return nil, p.Errorf(0, "invalid fragment header")
}
f := &TextFragment{}
f.Comment = strings.TrimSpace(parts[1])
header := parts[0][len(startMark) : len(parts[0])-len(endMark)]
ranges := strings.Split(header, " +")
if len(ranges) != 2 {
return nil, p.Errorf(0, "invalid fragment header")
}
var err error
if f.OldPosition, f.OldLines, err = parseRange(ranges[0]); err != nil {
return nil, p.Errorf(0, "invalid fragment header: %v", err)
}
if f.NewPosition, f.NewLines, err = parseRange(ranges[1]); err != nil {
return nil, p.Errorf(0, "invalid fragment header: %v", err)
}
if err := p.Next(); err != nil && err != io.EOF {
return nil, err
}
return f, nil
}
func (p *parser) ParseTextChunk(frag *TextFragment) error {
if p.Line(0) == "" {
return p.Errorf(0, "no content following fragment header")
}
oldLines, newLines := frag.OldLines, frag.NewLines
for oldLines > 0 || newLines > 0 {
line := p.Line(0)
op, data := line[0], line[1:]
switch op {
case '\n':
data = "\n"
fallthrough // newer GNU diff versions create empty context lines
case ' ':
oldLines--
newLines--
if frag.LinesAdded == 0 && frag.LinesDeleted == 0 {
frag.LeadingContext++
} else {
frag.TrailingContext++
}
frag.Lines = append(frag.Lines, Line{OpContext, data})
case '-':
oldLines--
frag.LinesDeleted++
frag.TrailingContext = 0
frag.Lines = append(frag.Lines, Line{OpDelete, data})
case '+':
newLines--
frag.LinesAdded++
frag.TrailingContext = 0
frag.Lines = append(frag.Lines, Line{OpAdd, data})
case '\\':
// this may appear in middle of fragment if it's for a deleted line
if isNoNewlineMarker(line) {
removeLastNewline(frag)
break
}
fallthrough
default:
// TODO(bkeyes): if this is because we hit the next header, it
// would be helpful to return the miscounts line error. We could
// either test for the common headers ("@@ -", "diff --git") or
// assume any invalid op ends the fragment; git returns the same
// generic error in all cases so either is compatible
return p.Errorf(0, "invalid line operation: %q", op)
}
if err := p.Next(); err != nil {
if err == io.EOF {
break
}
return err
}
}
if oldLines != 0 || newLines != 0 {
hdr := max(frag.OldLines-oldLines, frag.NewLines-newLines) + 1
return p.Errorf(-hdr, "fragment header miscounts lines: %+d old, %+d new", -oldLines, -newLines)
}
if frag.LinesAdded == 0 && frag.LinesDeleted == 0 {
return p.Errorf(0, "fragment contains no changes")
}
// check for a final "no newline" marker since it is not included in the
// counters used to stop the loop above
if isNoNewlineMarker(p.Line(0)) {
removeLastNewline(frag)
if err := p.Next(); err != nil && err != io.EOF {
return err
}
}
return nil
}
func isNoNewlineMarker(s string) bool {
// test for "\ No newline at end of file" by prefix because the text
// changes by locale (git claims all versions are at least 12 chars)
return len(s) >= 12 && s[:2] == "\\ "
}
func removeLastNewline(frag *TextFragment) {
if len(frag.Lines) > 0 {
last := &frag.Lines[len(frag.Lines)-1]
last.Line = strings.TrimSuffix(last.Line, "\n")
}
}
func parseRange(s string) (start int64, end int64, err error) {
parts := strings.SplitN(s, ",", 2)
if start, err = strconv.ParseInt(parts[0], 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return 0, 0, fmt.Errorf("bad start of range: %s: %v", parts[0], nerr.Err)
}
if len(parts) > 1 {
if end, err = strconv.ParseInt(parts[1], 10, 64); err != nil {
nerr := err.(*strconv.NumError)
return 0, 0, fmt.Errorf("bad end of range: %s: %v", parts[1], nerr.Err)
}
} else {
end = 1
}
return
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
|