-
Notifications
You must be signed in to change notification settings - Fork 332
/
bisect.go
196 lines (176 loc) · 5.02 KB
/
bisect.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
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"os"
"time"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
)
const bisectCommitTemplate = `{{ range $val := . }}
ID: {{ $val.Id|yellow }}{{if $val.Committer }}
Author: {{ $val.Committer }}{{end}}
Date: {{ $val.CreationDate|date }}
Meta Range ID: {{ $val.MetaRangeId }}
{{ if gt ($val.Parents|len) 1 -}}
Merge: {{ $val.Parents|join ", "|bold }}
{{ end }}
{{ $val.Message }}
{{ if $val.Metadata.AdditionalProperties }}
Metadata:
{{ range $key, $value := $val.Metadata.AdditionalProperties }}
{{ $key | printf "%-18s" }} = {{ $value }}
{{ end -}}
{{ end -}}
{{ end }}`
type BisectSelect int
const (
BisectSelectGood BisectSelect = iota
BisectSelectBad
)
var ErrCommitNotFound = errors.New("commit not found")
// bisectCmd represents the bisect command
var bisectCmd = &cobra.Command{
Use: "bisect",
Short: "Binary search to find the commit that introduced a bug",
Hidden: true,
}
type Bisect struct {
Created time.Time `json:"created"`
Repository string `json:"repository"`
BadCommit string `json:"badCommit,omitempty"`
GoodCommit string `json:"goodCommit,omitempty"`
Commits []*apigen.Commit `json:"commits,omitempty"`
}
const bisectStartCmdArgs = 2
func resolveCommitOrDie(ctx context.Context, client apigen.ClientWithResponsesInterface, repository, ref string) string {
response, err := client.GetCommitWithResponse(ctx, repository, ref)
DieOnErrorOrUnexpectedStatusCode(response, err, http.StatusOK)
if response.JSON200 == nil {
Die("Bad response from server", 1)
}
return response.JSON200.Id
}
func (b *Bisect) PrintStatus() {
fmt.Println("Repository:", b.Repository)
commitCount := len(b.Commits)
switch {
case b.BadCommit == "" && b.GoodCommit == "":
fmt.Println("Missing both good and bad commits")
case b.GoodCommit == "":
fmt.Printf("Bad commit lakefs://%s/%s, waiting for good commit\n", b.Repository, b.BadCommit)
case b.BadCommit == "":
fmt.Printf("Good commit lakefs://%s/%s, waiting for bad commit\n", b.Repository, b.GoodCommit)
case commitCount == 0:
fmt.Println("No commits found")
case commitCount == 1:
commit := b.Commits[0]
fmt.Printf("Found commit lakefs://%s/%s %s\n", b.Repository, commit.Id, commit.Message)
default:
steps := math.Log2(float64(commitCount))
h := (commitCount >> 1) - 1
fmt.Printf("Bisecting: %d commits left to test after this (roughly %d steps)\n", h, int(steps))
commit := b.Commits[h]
fmt.Printf("Current commit lakefs://%s/%s %s\n", b.Repository, commit.Id, commit.Message)
}
}
func runBisectSelect(bisectSelect BisectSelect) {
var state Bisect
err := state.Load()
if os.IsNotExist(err) {
Die(`You need to start by "bisect start"`, 1)
}
if err := state.SaveSelect(bisectSelect); err != nil {
DieErr(err)
}
state.PrintStatus()
}
const bisectFilePath = "~/.lakectl.bisect.json"
func BisectRemove() error {
expand, err := homedir.Expand(bisectFilePath)
if err != nil {
return err
}
return os.Remove(expand)
}
func (b *Bisect) Load() error {
expand, err := homedir.Expand(bisectFilePath)
if err != nil {
return err
}
data, err := os.ReadFile(expand)
if err != nil {
return err
}
return json.Unmarshal(data, b)
}
func (b *Bisect) Save() error {
expand, err := homedir.Expand(bisectFilePath)
if err != nil {
return err
}
data, err := json.Marshal(b)
if err != nil {
return err
}
const fileMode = 0o644
return os.WriteFile(expand, data, fileMode)
}
func (b *Bisect) Update(ctx context.Context, client apigen.ClientWithResponsesInterface) error {
if b.GoodCommit == "" || b.BadCommit == "" {
b.Commits = nil
return nil
}
// scan commit log from bad to good (not included) and save them into state
b.Commits = nil
const amountPerCall = 500
params := &apigen.LogCommitsParams{
Amount: apiutil.Ptr(apigen.PaginationAmount(amountPerCall)),
}
var commits []*apigen.Commit
for {
logResponse, err := client.LogCommitsWithResponse(ctx, b.Repository, b.BadCommit, params)
DieOnErrorOrUnexpectedStatusCode(logResponse, err, http.StatusOK)
if logResponse.JSON200 == nil {
Die("Bad response from server", 1)
}
results := logResponse.JSON200.Results
for i := range results {
if results[i].Id == b.GoodCommit {
b.Commits = commits
return nil
}
commits = append(commits, &results[i])
}
if !logResponse.JSON200.Pagination.HasMore {
break
}
params.After = apiutil.Ptr(apigen.PaginationAfter(logResponse.JSON200.Pagination.NextOffset))
}
return fmt.Errorf("good %w", ErrCommitNotFound)
}
func (b *Bisect) SaveSelect(sel BisectSelect) error {
if len(b.Commits) <= 1 {
return nil
}
h := len(b.Commits) >> 1
switch sel {
case BisectSelectGood:
b.Commits = b.Commits[:h]
case BisectSelectBad:
b.Commits = b.Commits[h:]
default:
DieFmt("Unknown bisect select - %d", sel)
}
return b.Save()
}
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(bisectCmd)
}