-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
220 lines (192 loc) · 5.04 KB
/
diff.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/dgravesa/gover/pkg/modface"
"github.com/dgravesa/minicli"
)
type diffCmd struct {
modpath *string // injected by main command
pchanges *optset
errcond *optset
compare string
}
func newDiffCmd(modpath *string) minicli.CmdImpl {
return &diffCmd{modpath: modpath}
}
func (d *diffCmd) SetFlags(flags *flag.FlagSet) {
d.pchanges = makeOptsetFlag(flags, "changes", "changes to print", "any", "breaking")
d.errcond = makeOptsetFlag(flags, "error", "condition to exit with error status code",
"none", "breaking", "any")
flags.StringVar(&d.compare, "compare", "HEAD", "specify commit or tag to compare against")
}
func (d *diffCmd) Exec(args []string) error {
// validate changes and error arguments
pchanges, err := d.pchanges.Value()
if err != nil {
return err
}
errcond, err := d.errcond.Value()
if err != nil {
return err
}
moduleDifference, err := diff(*d.modpath, d.compare)
if err != nil {
return err
}
// print differences to stdout as specified by change level
printDiff(moduleDifference, pchanges)
var resultStatus error
switch errcond {
case "breaking":
if moduleDifference.Breaking() {
resultStatus = fmt.Errorf("breaking changes detected")
}
case "any":
if moduleDifference.Any() {
resultStatus = fmt.Errorf("changes detected")
}
default:
resultStatus = nil
}
return resultStatus
}
func diff(modpath string, compareID string) (*modface.ModuleDifference, error) {
var currentModule *modface.Module
var compareModule *modface.Module
currentDone := make(chan error)
recentDone := make(chan error)
// parse current module interface
go func() {
var err error
currentModule, err = modface.ParseModule(modpath)
currentDone <- err
}()
// copy module and checkout compare version
go func() {
// create temporary directory
tmpdir, err := ioutil.TempDir(os.TempDir(), "gover-*")
if err != nil {
recentDone <- err
return
}
// copy module into temporary directory
cpycmd := exec.Command("cp", "-r", fmt.Sprintf("%s/", modpath), tmpdir)
_, cperr := cpycmd.Output()
// NOTE: doing this manually because defer does not seem to like it
rmdir := func() {
rmcmd := exec.Command("rm", "-rf", tmpdir)
_, rmerr := rmcmd.Output()
// verify rm succeeded
switch v := rmerr.(type) {
case nil:
break
case *exec.ExitError:
fmt.Fprintln(os.Stderr, rmcmd.String(), string(v.Stderr))
default:
fmt.Fprintln(os.Stderr, "error:", v)
}
}
// verify copy succeeded
switch v := cperr.(type) {
case nil:
break
case *exec.ExitError:
rmdir()
recentDone <- fmt.Errorf("%s: %s", cpycmd, string(v.Stderr))
return
default:
rmdir()
recentDone <- fmt.Errorf("%s: %v", cpycmd, cperr)
return
}
// checkout recent version
checkoutcmd := exec.Command("git", "-C", tmpdir, "checkout", "-f", compareID)
_, checkouterr := checkoutcmd.Output()
switch v := checkouterr.(type) {
case nil:
break
case *exec.ExitError:
rmdir()
recentDone <- fmt.Errorf("%s: %s", checkoutcmd, string(v.Stderr))
return
default:
rmdir()
recentDone <- fmt.Errorf("%s: %v", checkoutcmd, err)
return
}
// parse version of module
compareModule, err = modface.ParseModule(tmpdir)
rmdir()
recentDone <- err
}()
// wait for results
currentErr := <-currentDone
recentErr := <-recentDone
if currentErr != nil {
return nil, currentErr
} else if recentErr != nil {
return nil, recentErr
}
// compute difference between module interfaces
moduleDifference := modface.Diff(compareModule, currentModule)
return moduleDifference, nil
}
func printDiff(moduleDifference *modface.ModuleDifference, level string) {
type difference interface {
Any() bool
Breaking() bool
}
meetsLevel := func(d difference) bool {
if level == "any" && d.Any() {
return true
} else if level == "breaking" && d.Breaking() {
return true
}
return false
}
if !meetsLevel(moduleDifference) {
return
}
// print module name
if !moduleDifference.ModPathsMatch {
fmt.Println("< module", moduleDifference.OldModPath)
fmt.Println("> module", moduleDifference.ModPath)
// do not attempt to print further since everything would be a difference
return
}
fmt.Println("module", moduleDifference.ModPath)
// print removals
for pkgname := range moduleDifference.PackageRemovals {
fmt.Println("< package", pkgname)
}
// print additions
for pkgname := range moduleDifference.PackageAdditions {
fmt.Println("> package", pkgname)
}
// print changes per package
for pkgname, pkgchanges := range moduleDifference.PackageChanges {
if !meetsLevel(pkgchanges) {
continue
}
fmt.Println("---", "package", pkgname)
// print package removals
for _, face := range pkgchanges.Removals {
fmt.Println("< ", face)
}
if level == "any" {
// print package additions
for _, face := range pkgchanges.Additions {
fmt.Println("> ", face)
}
}
// print package changes
for _, facediff := range pkgchanges.Changes {
fmt.Println("< ", facediff.Old)
fmt.Println("> ", facediff.New)
}
}
}