This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
223 lines (187 loc) · 5.26 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
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
"sync"
"github.com/dxw/git-test-branch/screenwriter"
"github.com/gammazero/workerpool"
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
)
var mutex sync.Mutex
var debug bool
func main() {
flags := flag.NewFlagSet("git test-branch", flag.ContinueOnError)
flags.Usage = func() {
fmt.Printf("Usage: git test-branch main..@ 'command-to-run'\n\n")
fmt.Printf("Options:\n")
flags.PrintDefaults()
}
flags.BoolVar(&debug, "debug", false, "Run in debug mode (showing a lot of output).")
err := flags.Parse(os.Args[1:])
if err == flag.ErrHelp {
// Do nothing
} else if err != nil {
log.Fatal(err)
}
if len(flags.Args()) < 2 {
flags.Usage()
os.Exit(1)
}
commitSpecification := flags.Args()[0]
command := flags.Args()[1]
rootDir := getRootDir()
commits := getCommits(commitSpecification)
// This only needs a small buffer because showResults will be reading it constantly
results := make(chan testResult, 10)
poolSize := 5
if debug {
log.Println("main: Setting poolSize to 1 for debug mode")
poolSize = 1
}
pool := workerpool.New(poolSize)
// Process failures
failures := make(chan bool, 10)
aCommandFailed := false
go func() {
for <-failures {
aCommandFailed = true
}
}()
// Show the results before we do anything, and update them regularly
go showResults(commits, results)
for _, commit := range commits {
// This line is necessary because otherwise `commit`'s contents change
commit := commit
pool.Submit(func() {
err := runTest(rootDir, command, commit, results, failures)
if err != nil {
log.Fatal(errors.Wrap(err, "failure in workerpool task"))
}
})
}
pool.StopWait()
close(results)
close(failures)
if aCommandFailed {
os.Exit(1)
}
}
func runCommandSilent(path string, arg ...string) error {
cmd := exec.Command(path, arg...)
if debug {
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
log.Printf("runCommandSilent: Running command: %s %#v\n", cmd.Path, cmd.Args)
}
err := cmd.Run()
if err != nil {
return errors.Wrap(err, "runCommandSilent")
}
return err
}
func runTest(root string, command string, commit commit, results chan<- testResult, failures chan<- bool) error {
err := os.MkdirAll(root, 0755)
if err != nil {
return errors.Wrap(err, "runTest: failed to create build directory root")
}
results <- testResult{commit: commit, status: testStatusRunning}
commitDir := path.Join(root, commit.hash)
err = os.RemoveAll(commitDir)
if err != nil {
return errors.Wrap(err, "runTest: failed removing commitDir")
}
err = runExclusively(func() error {
err := runCommandSilent("git", "worktree", "add", "--force", "--detach", commitDir, commit.hash)
if err != nil {
return errors.Wrap(err, "runTest: failed running worktree add command")
}
return nil
})
if err != nil {
return errors.Wrap(err, "runTest: failed running exclusively")
}
err = runCommandSilent("sh", "-c", fmt.Sprintf("cd %s && %s", commitDir, command))
if err == nil {
results <- testResult{commit: commit, status: testStatusPass}
} else {
results <- testResult{commit: commit, status: testStatusFail}
failures <- true
}
err = runExclusively(func() error {
err := runCommandSilent("git", "worktree", "remove", "--force", commitDir)
if err != nil {
return errors.Wrap(err, "runTest: failed running worktree remove command")
}
return nil
})
if err != nil {
return errors.Wrap(err, "runTest: failed running exclusively")
}
return nil
}
func runExclusively(f func() error) error {
defer mutex.Unlock()
mutex.Lock()
err := f()
if err != nil {
return errors.Wrap(err, "runExclusively: f() returned non-nil error")
}
return nil
}
func showResults(commits []commit, results <-chan testResult) {
finalResults := map[string]testResult{}
// Populate finalResults with some placeholder values
for _, commit := range commits {
finalResults[commit.hash] = testResult{commit: commit, status: testStatusWaiting}
}
// Update finalResults with the correct values as they come in from the channel
// These get updated continuously
update := make(chan bool, 1)
go func() {
for thisResult := range results {
finalResults[thisResult.commit.hash] = thisResult
update <- true
}
// When results is closed, close update as well
close(update)
}()
w := screenwriter.New(os.Stdout)
// Display finalResults immediately, then whenever they're updated
showResultsOnce(w, commits, finalResults)
for range update {
showResultsOnce(w, commits, finalResults)
}
}
func showResultsOnce(w *screenwriter.ScreenWriter, commits []commit, finalResults map[string]testResult) {
text := ""
for _, commit := range commits {
result := finalResults[commit.hash]
text += fmt.Sprintf("%s [%s] %s\n", result.commit.shortHash, result.status, result.commit.subject)
}
if debug {
fmt.Print(text)
log.Println("showResultsOnce: Done")
} else {
err := w.Display(text)
if err != nil {
log.Fatal(err)
}
}
}
func gitGetOutput(command ...string) string {
cmd := exec.Command("git", command...)
output, err := cmd.Output()
if err != nil {
log.Fatal(errors.Wrap(err, "gitGetOutput"))
}
return strings.TrimSpace(string(output))
}
func getRootDir() string {
dir := gitGetOutput("rev-parse", "--absolute-git-dir")
return path.Join(dir, "git-test-branch")
}