-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
141 lines (132 loc) · 3.16 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
)
var (
baseRef = flag.String("base", "HEAD", "git reference to compare against")
verbose = flag.Bool("v", false, "enable verbose mode")
privRecv = flag.Bool("private", false, "include exported methods with private receivers")
)
type checker struct {
fset *token.FileSet
fsetold *token.FileSet
}
func init() {
flag.CommandLine.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Example usage:")
fmt.Fprintln(flag.CommandLine.Output(), " breakcheck # compares working tree against git head")
fmt.Fprintln(flag.CommandLine.Output(), " breakcheck --base=v1.0.0 # compares against tag v1.0.0")
fmt.Fprintln(flag.CommandLine.Output(), "\nFlags:")
flag.PrintDefaults()
}
flag.Parse()
}
func main() {
stats, err := gitStats(*baseRef)
if err != nil {
log.Fatal(err)
}
pkgs := make(map[string]struct{})
for _, stat := range stats {
if stat.mode == modeAdded {
// skip; file not in base
continue
}
path := stat.oldPath
if filepath.Ext(path) != ".go" {
continue
}
if strings.HasSuffix(path, "_test.go") {
continue
}
dir := removePrivatePathSegments(filepath.Dir(path))
if len(dir) == 0 {
// this was a root internal or vendor folder
continue
}
if dir == "." {
dir = "./"
}
pkgs[dir] = struct{}{}
}
fsetHead := token.NewFileSet()
fsetBase := token.NewFileSet()
for dir := range pkgs {
fd, err := os.Open(dir)
if err != nil {
// package was removed
fmt.Printf("%s: package removed\n\n", dir)
continue
}
list, err := fd.Readdir(-1)
if err != nil {
log.Fatal("fd.Readdir: ", err)
}
fd.Close()
if *verbose {
fmt.Println(dir)
}
// create working path summary
summary := newPackageSummary(fsetHead, dir)
for _, d := range list {
if d.IsDir() || !strings.HasSuffix(d.Name(), ".go") || strings.HasSuffix(d.Name(), "_test.go") {
continue
}
fullpath := filepath.Join(dir, d.Name())
src, err := parser.ParseFile(fsetHead, fullpath, nil, parser.DeclarationErrors)
if err != nil {
log.Fatal("parser.ParseFile: ", err)
}
if !ast.FileExports(src) {
continue
}
ast.Walk(summary, src)
}
// scan base, everything found there should exist in working path
comparer := newDeclComparer(fsetBase, dir, summary)
blobs, err := gitLsTreeGoBlobs(*baseRef, dir)
if err != nil {
log.Fatal(err)
}
for _, file := range blobs {
r, err := gitBlob(*baseRef, file)
if err != nil {
log.Fatal(err)
}
src, err := parser.ParseFile(fsetBase, file, r, parser.DeclarationErrors)
if err != nil {
log.Fatalf("parser.ParseFile: %s", err)
}
if !ast.FileExports(src) {
continue
}
ast.Walk(comparer, src)
}
if comparer.report.Len() > 0 {
fmt.Println(comparer.report.String())
}
}
}
func removePrivatePathSegments(dir string) string {
if i := strings.Index(dir, "internal/"); i > -1 {
dir = dir[:i]
}
if i := strings.Index(dir, "/internal"); i > -1 {
dir = dir[:i]
}
if i := strings.Index(dir, "vendor/"); i > -1 {
dir = dir[:i]
}
if i := strings.Index(dir, "/vendor"); i > -1 {
dir = dir[:i]
}
return dir
}