-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (148 loc) · 4.29 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
const shortSHALength = 7
var (
cloneDirectory = flag.String("d", "clones", "clone directory")
workers = flag.Int("w", 3, "number of workers")
refreshInterval = flag.Int("r", 1000, "refresh every r attempts")
logInterval = flag.Int("l", 100, "log every l attempts")
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
start := time.Now()
flag.Parse()
// Path
workingDirectory, err := os.Getwd()
if err != nil {
fmt.Println("failed to get working directory", err)
os.Exit(1)
return
}
clonePath, err := filepath.Abs(*cloneDirectory)
if err != nil {
fmt.Println("failed to get absolute path of clone dir", err)
os.Exit(1)
return
}
clonePath = filepath.Join(clonePath, strconv.Itoa(int(start.Unix())))
// Async
doneChan := make(chan struct{})
errChan := make(chan error)
// Start workers
worker := 0
for ; worker < *workers; worker++ {
go findLuckySHA(worker, workingDirectory, clonePath, doneChan, errChan)
time.Sleep(100 * time.Millisecond)
}
go func() {
for err := range errChan {
fmt.Println("error", err)
go findLuckySHA(worker, workingDirectory, clonePath, doneChan, errChan)
worker++
time.Sleep(100 * time.Millisecond)
}
}()
// Wait and cancel
<-doneChan
}
func findLuckySHA(worker int, remotePath, clonePath string, doneChan chan struct{}, errChan chan error) {
repoPath := filepath.Join(clonePath, fmt.Sprintf("%d-quine-commit", worker))
for attempts := 0; ; attempts++ {
start := time.Now()
shouldRefresh := attempts%*refreshInterval == 0
shouldLog := attempts%*logInterval == 0
if shouldRefresh {
if err := os.RemoveAll(repoPath); err != nil {
errChan <- errors.Wrap(err, "failed to remove repo")
return
}
if err := gitCloneLocal(remotePath, repoPath); err != nil {
errChan <- errors.Wrapf(err, "failed to git clone %q", repoPath)
return
}
}
shortSha := randomShortSHA(shortSHALength)
if err := gitCommit(repoPath, shortSha); err != nil {
errChan <- err
return
}
outputSha, err := gitRevParse(repoPath, shortSHALength)
if err != nil {
errChan <- err
return
}
if shortSha == outputSha {
message := fmt.Sprintf("success! the lucky sha was %s from worker %d", shortSha, worker)
fmt.Println(message)
if err := os.WriteFile(filepath.Join(repoPath, "short.sha"), []byte(message), 0666); err != nil {
fmt.Println("failed to write file under repo", repoPath, err)
return
}
close(doneChan)
} else {
if err := gitReset(repoPath); err != nil {
errChan <- err
return
}
}
if shouldLog {
fmt.Println(shortSha, "!=", outputSha, "worker", worker, "attempt", attempts, "elapsed", time.Since(start))
}
}
}
func gitClone(repoPath string) error {
return gitCloneLocal("https://github.com/broothie/quine-commit.git", repoPath)
}
func gitCloneLocal(remotePath, repoPath string) error {
output, err := exec.Command("git", "clone", remotePath, repoPath).CombinedOutput()
fmt.Print(string(output))
if err != nil {
return errors.Wrapf(err, "failed to clone git repo at %q", repoPath)
}
return nil
}
func gitCommit(repoPath, message string) error {
output, err := exec.Command("git", "-C", repoPath, "commit", "--allow-empty", "-m", message).CombinedOutput()
if err != nil {
fmt.Print(string(output))
return errors.Wrapf(err, "failed to commit to repo at %q", repoPath)
}
return nil
}
func gitRevParse(repoPath string, length int) (string, error) {
output, err := exec.Command("git", "-C", repoPath, "rev-parse", fmt.Sprintf("--short=%d", length), "HEAD").CombinedOutput()
if err != nil {
fmt.Println(string(output))
return "", errors.Wrapf(err, "failed to parse revision at %q", repoPath)
}
return strings.TrimSpace(string(output)), nil
}
func gitReset(repoPath string) error {
output, err := exec.Command("git", "-C", repoPath, "reset", "--hard", "HEAD~").CombinedOutput()
if err != nil {
fmt.Print(string(output))
return errors.Wrapf(err, "failed to reset repo at %q", repoPath)
}
return nil
}
func randomShortSHA(length int) string {
const hexRunes = "0123456789abcdef"
runes := make([]rune, length)
for i := range runes {
runes[i] = rune(hexRunes[rand.Intn(len(hexRunes))])
}
return string(runes)
}