-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchangeset_reader.go
449 lines (384 loc) · 11 KB
/
changeset_reader.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package godiff
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"
)
const (
stateStartOfDiff = "stateStartOfDiff"
stateDiffHeader = "stateDiffHeader"
stateHunkHeader = "stateHunkHeader"
stateHunkBody = "stateHunkBody"
stateComment = "stateComment"
stateCommentDelim = "stateCommentDelim"
stateCommentHeader = "stateCommentHeader"
stateDiffComment = "stateDiffComment"
stateDiffCommentDelim = "stateDiffCommentDelim"
stateDiffCommentHeader = "stateDiffCommentHeader"
)
var (
reDiffHeader = regexp.MustCompile(
`^--- |^\+\+\+ `)
reGitDiffHeader = regexp.MustCompile(
`^diff |^index `)
reFromFile = regexp.MustCompile(
`^--- (\S+)(\s+(.*))`)
reToFile = regexp.MustCompile(
`^\+\+\+ (\S+)(\s+(.*))`)
reHunk = regexp.MustCompile(
`^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@`)
reSegmentContext = regexp.MustCompile(
`^ `)
reSegmentAdded = regexp.MustCompile(
`^\+`)
reSegmentRemoved = regexp.MustCompile(
`^-`)
reCommentDelim = regexp.MustCompile(
`^#\s+---`)
reCommentHeader = regexp.MustCompile(
`^#\s+\[(\d+)@(\d+)\]\s+\|([^|]+)\|(.*)`)
reCommentText = regexp.MustCompile(
`^#(\s*)(.*)\s*`)
reIndent = regexp.MustCompile(
`^#(\s+)`)
reEmptyLine = regexp.MustCompile(
`^\n$`)
reIgnoredLine = regexp.MustCompile(
`^` + ignorePrefix)
)
type parser struct {
state string
changeset Changeset
diff *Diff
hunk *Hunk
segment *Segment
comment *Comment
line *Line
lineNumber int
segmentType string
commentsList []*Comment
}
type Error struct {
LineNumber int
Message string
}
func (err Error) Error() string {
return fmt.Sprintf("line %d: %s", err.LineNumber, err.Message)
}
func ReadChangeset(r io.Reader) (Changeset, error) {
buffer := bufio.NewReader(r)
current := parser{}
current.state = stateStartOfDiff
for {
current.lineNumber++
line, err := buffer.ReadString('\n')
if err != nil {
break
}
if reIgnoredLine.MatchString(line) {
continue
}
err = current.switchState(line)
if err != nil {
return current.changeset, err
}
err = current.createNodes(line)
if err != nil {
return current.changeset, err
}
err = current.locateNodes(line)
if err != nil {
return current.changeset, err
}
err = current.parseLine(line)
if err != nil {
return current.changeset, err
}
}
for _, comment := range current.commentsList {
comment.Text = strings.TrimSpace(comment.Text)
}
return current.changeset, nil
}
func (current *parser) switchState(line string) error {
inComment := false
switch current.state {
case stateStartOfDiff:
switch {
case reDiffHeader.MatchString(line), reGitDiffHeader.MatchString(line):
current.state = stateDiffHeader
case reCommentText.MatchString(line):
inComment = true
case reEmptyLine.MatchString(line):
// body intentionally left empty
default:
return Error{
current.lineNumber,
"expected diff header, but none found",
}
}
case stateDiffHeader:
switch {
case reHunk.MatchString(line):
current.state = stateHunkHeader
}
case stateDiffComment, stateDiffCommentDelim, stateDiffCommentHeader:
switch {
case reDiffHeader.MatchString(line):
current.state = stateDiffHeader
case reCommentText.MatchString(line):
inComment = true
case reEmptyLine.MatchString(line):
current.state = stateStartOfDiff
}
case stateHunkHeader:
current.state = stateHunkBody
fallthrough
case stateHunkBody, stateComment, stateCommentDelim, stateCommentHeader:
switch {
case reSegmentContext.MatchString(line):
current.state = stateHunkBody
current.segmentType = SegmentTypeContext
case reSegmentRemoved.MatchString(line):
current.state = stateHunkBody
current.segmentType = SegmentTypeRemoved
case reSegmentAdded.MatchString(line):
current.state = stateHunkBody
current.segmentType = SegmentTypeAdded
case reHunk.MatchString(line):
current.state = stateHunkHeader
case reCommentText.MatchString(line):
inComment = true
case reEmptyLine.MatchString(line):
current.state = stateStartOfDiff
current.diff = nil
current.hunk = nil
current.segment = nil
current.line = nil
}
}
if !inComment {
current.comment = nil
} else {
switch current.state {
case stateStartOfDiff:
fallthrough
case stateDiffComment, stateDiffCommentDelim, stateDiffCommentHeader:
switch {
case reCommentDelim.MatchString(line):
current.state = stateDiffCommentDelim
case reCommentHeader.MatchString(line):
current.state = stateDiffCommentHeader
case reCommentText.MatchString(line):
current.state = stateDiffComment
}
case stateHunkBody:
fallthrough
case stateComment, stateCommentDelim, stateCommentHeader:
switch {
case reCommentDelim.MatchString(line):
current.state = stateCommentDelim
case reCommentHeader.MatchString(line):
current.state = stateCommentHeader
case reCommentText.MatchString(line):
current.state = stateComment
}
}
}
// Uncomment for debug state switching
//fmt.Printf("%20s : %#v\n", current.state, line)
return nil
}
func (current *parser) createNodes(line string) error {
switch current.state {
case stateDiffComment:
if current.comment != nil {
break
}
fallthrough
case stateDiffCommentDelim, stateDiffCommentHeader:
current.comment = &Comment{}
fallthrough
case stateDiffHeader:
if current.diff == nil {
current.diff = &Diff{}
current.changeset.Diffs = append(current.changeset.Diffs,
current.diff)
}
case stateHunkHeader:
current.hunk = &Hunk{}
current.segment = &Segment{}
case stateCommentDelim, stateCommentHeader:
current.comment = &Comment{}
case stateComment:
if current.comment == nil {
current.comment = &Comment{}
}
case stateHunkBody:
if current.segment.Type != current.segmentType {
current.segment = &Segment{Type: current.segmentType}
current.hunk.Segments = append(current.hunk.Segments,
current.segment)
}
current.line = &Line{}
current.segment.Lines = append(current.segment.Lines, current.line)
}
return nil
}
func (current *parser) locateNodes(line string) error {
switch current.state {
case stateComment, stateDiffComment:
current.locateComment(line)
case stateHunkBody:
current.locateLine(line)
}
return nil
}
func (current *parser) locateComment(line string) error {
if current.comment.Parented || strings.TrimSpace(line) == "#" {
return nil
}
current.commentsList = append(current.commentsList, current.comment)
current.comment.Parented = true
if current.hunk != nil {
current.comment.Anchor.LineType = current.segment.Type
current.comment.Anchor.Line = current.segment.GetLineNum(current.line)
current.comment.Anchor.Path = current.diff.Destination.ToString
current.comment.Anchor.SrcPath = current.diff.Source.ToString
}
current.comment.Indent = getIndentSize(line)
parent := current.findParentComment(current.comment)
if parent != nil {
parent.Comments = append(parent.Comments, current.comment)
} else {
if current.line != nil {
current.diff.LineComments = append(current.diff.LineComments,
current.comment)
current.line.Comments = append(current.line.Comments,
current.comment)
} else {
current.diff.FileComments = append(current.diff.FileComments,
current.comment)
}
}
return nil
}
func (current *parser) locateLine(line string) error {
sourceOffset := current.hunk.SourceLine - 1
destinationOffset := current.hunk.DestinationLine - 1
if len(current.hunk.Segments) > 1 {
prevSegment := current.hunk.Segments[len(current.hunk.Segments)-2]
lastLine := prevSegment.Lines[len(prevSegment.Lines)-1]
sourceOffset = lastLine.Source
destinationOffset = lastLine.Destination
}
hunkLength := int64(len(current.segment.Lines))
switch current.segment.Type {
case SegmentTypeContext:
current.line.Source = sourceOffset + hunkLength
current.line.Destination = destinationOffset + hunkLength
case SegmentTypeAdded:
current.line.Source = sourceOffset
current.line.Destination = destinationOffset + hunkLength
case SegmentTypeRemoved:
current.line.Source = sourceOffset + hunkLength
current.line.Destination = destinationOffset
}
return nil
}
func (current *parser) parseLine(line string) error {
switch current.state {
case stateDiffHeader:
current.parseDiffHeader(line)
case stateHunkHeader:
current.parseHunkHeader(line)
case stateHunkBody:
current.parseHunkBody(line)
case stateComment, stateDiffComment:
current.parseComment(line)
case stateCommentHeader, stateDiffCommentHeader:
current.parseCommentHeader(line)
}
return nil
}
func (current *parser) parseDiffHeader(line string) error {
switch {
case reFromFile.MatchString(line):
matches := reFromFile.FindStringSubmatch(line)
current.changeset.Path = matches[1]
current.diff.Source.ToString = matches[1]
current.changeset.FromHash = matches[3]
current.diff.Attributes.FromHash = []string{matches[3]}
case reToFile.MatchString(line):
matches := reToFile.FindStringSubmatch(line)
current.diff.Destination.ToString = matches[1]
current.changeset.ToHash = matches[3]
current.diff.Attributes.ToHash = []string{matches[3]}
default:
return Error{
current.lineNumber,
"expected diff header, but not found",
}
}
return nil
}
func (current *parser) parseHunkHeader(line string) error {
matches := reHunk.FindStringSubmatch(line)
current.hunk.SourceLine, _ = strconv.ParseInt(matches[1], 10, 64)
current.hunk.SourceSpan, _ = strconv.ParseInt(matches[3], 10, 64)
current.hunk.DestinationLine, _ = strconv.ParseInt(matches[4], 10, 64)
current.hunk.DestinationSpan, _ = strconv.ParseInt(matches[6], 10, 64)
current.diff.Hunks = append(current.diff.Hunks, current.hunk)
return nil
}
func (current *parser) parseHunkBody(line string) error {
current.line.Line = line[1 : len(line)-1]
return nil
}
func (current *parser) parseCommentHeader(line string) error {
matches := reCommentHeader.FindStringSubmatch(line)
current.comment.Author.DisplayName = strings.TrimSpace(matches[3])
current.comment.Id, _ = strconv.ParseInt(matches[1], 10, 64)
updatedDate, _ := time.ParseInLocation(time.ANSIC,
strings.TrimSpace(matches[4]),
time.Local)
current.comment.UpdatedDate = UnixTimestamp(updatedDate.Unix() * 1000)
version, _ := strconv.ParseInt(matches[2], 10, 64)
current.comment.Version = int(version)
return nil
}
func (current *parser) parseComment(line string) error {
matches := reCommentText.FindStringSubmatch(line)
if len(matches[1]) < current.comment.Indent {
return Error{
LineNumber: current.lineNumber,
Message: fmt.Sprintf(
"unexpected indent, should be at least: %d",
current.comment.Indent,
),
}
}
indentedLine := matches[1][current.comment.Indent:] + matches[2]
current.comment.Text += "\n" + indentedLine
return nil
}
func (current *parser) findParentComment(comment *Comment) *Comment {
for i := len(current.commentsList) - 1; i >= 0; i-- {
c := current.commentsList[i]
if comment.Indent > c.Indent {
return c
}
}
return nil
}
func getIndentSize(line string) int {
matches := reIndent.FindStringSubmatch(line)
if len(matches) == 0 {
return 0
}
return len(matches[1])
}