Skip to content

Commit 12179d7

Browse files
committed
Merge pull request #2 from cyphar/kill-remaining-goroutines-after-finished
Use a done channel to kill off remaining goroutines
2 parents e8909ca + 1337000 commit 12179d7

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

gitbrute.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ func main() {
6767

6868
possibilities := make(chan try, 512)
6969
go explore(possibilities)
70+
7071
winner := make(chan solution)
72+
done := make(chan struct{})
73+
7174
for i := 0; i < *cpu; i++ {
72-
go bruteForce(obj, winner, possibilities)
75+
go bruteForce(obj, winner, possibilities, done)
7376
}
7477

7578
w := <-winner
79+
close(done)
80+
7681
cmd := exec.Command("git", "commit", "--amend", "--date="+w.author.String(), "--file=-")
7782
cmd.Env = append([]string{"GIT_COMMITTER_DATE=" + w.committer.String()}, os.Environ()...)
7883
cmd.Stdout = os.Stdout
@@ -91,7 +96,7 @@ var (
9196
commiterDateRx = regexp.MustCompile(`(?m)^committer.+> (.+)`)
9297
)
9398

94-
func bruteForce(obj []byte, winner chan<- solution, possibilities <-chan try) {
99+
func bruteForce(obj []byte, winner chan<- solution, possibilities <-chan try, done <-chan struct{}) {
95100
// blob is the blob to mutate in-place repatedly while testing
96101
// whether we have a match.
97102
blob := []byte(fmt.Sprintf("commit %d\x00%s", len(obj), obj))
@@ -103,17 +108,23 @@ func bruteForce(obj []byte, winner chan<- solution, possibilities <-chan try) {
103108
hexBuf := make([]byte, 0, sha1.Size*2)
104109

105110
for t := range possibilities {
106-
ad := date{startUnix - int64(t.authorBehind), authorDate.tz}
107-
cd := date{startUnix - int64(t.commitBehind), commitDate.tz}
108-
strconv.AppendInt(blob[:adatei], ad.n, 10)
109-
strconv.AppendInt(blob[:cdatei], cd.n, 10)
110-
s1.Reset()
111-
s1.Write(blob)
112-
if !bytes.HasPrefix(hexInPlace(s1.Sum(hexBuf[:0])), wantHexPrefix) {
113-
continue
111+
select {
112+
case <-done:
113+
return
114+
default:
115+
ad := date{startUnix - int64(t.authorBehind), authorDate.tz}
116+
cd := date{startUnix - int64(t.commitBehind), commitDate.tz}
117+
strconv.AppendInt(blob[:adatei], ad.n, 10)
118+
strconv.AppendInt(blob[:cdatei], cd.n, 10)
119+
s1.Reset()
120+
s1.Write(blob)
121+
if !bytes.HasPrefix(hexInPlace(s1.Sum(hexBuf[:0])), wantHexPrefix) {
122+
continue
123+
}
124+
125+
winner <- solution{ad, cd}
126+
return
114127
}
115-
winner <- solution{ad, cd}
116-
return // at least yield one goroutine's CPU for git commit to run.
117128
}
118129
}
119130

0 commit comments

Comments
 (0)