forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
58 lines (50 loc) · 1.11 KB
/
parse.go
File metadata and controls
58 lines (50 loc) · 1.11 KB
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
package diff
import (
"bufio"
"fmt"
"strings"
)
type DiffStats struct {
Added int
Removed int
Modified int
}
func ParseStats(diff string) (map[string]DiffStats, error) {
stats := make(map[string]DiffStats)
var currentFile string
scanner := bufio.NewScanner(strings.NewReader(diff))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "---") {
continue
} else if strings.HasPrefix(line, "+++") {
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 {
currentFile = strings.TrimPrefix(parts[1], "b/")
}
continue
}
if strings.HasPrefix(line, "@@") {
continue
}
if currentFile == "" {
continue
}
fileStats := stats[currentFile]
switch {
case strings.HasPrefix(line, "+"):
fileStats.Added++
case strings.HasPrefix(line, "-"):
fileStats.Removed++
}
stats[currentFile] = fileStats
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading diff string: %w", err)
}
for file, fileStats := range stats {
fileStats.Modified = fileStats.Added + fileStats.Removed
stats[file] = fileStats
}
return stats, nil
}