/
filter-branch.go
326 lines (271 loc) · 9.72 KB
/
filter-branch.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2020 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"context"
"fmt"
"io"
"runtime"
"strings"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/analyzer"
"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/doltcore/rebase"
dsqle "github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/store/hash"
)
const (
filterDbName = "filterDB"
branchesFlag = "branches"
)
var filterBranchDocs = cli.CommandDocumentationContent{
ShortDesc: "Edits the commit history using the provided query",
LongDesc: `Traverses the commit history to the initial commit starting at the current HEAD commit, or a commit you name. Replays all commits, rewriting the history using the provided SQL queries. Separate multiple queries with semicolons. Use the DELIMITER syntax to define stored procedures, triggers, etc.
If a {{.LessThan}}commit-spec{{.GreaterThan}} is provided, the traversal will stop when the commit is reached and rewriting will begin at that commit, or will error if the commit is not found.
If the {{.EmphasisLeft}}--branches{{.EmphasisRight}} flag is supplied, filter-branch traverses and rewrites commits for all branches.
If the {{.EmphasisLeft}}--all{{.EmphasisRight}} flag is supplied, filter-branch traverses and rewrites commits for all branches and tags.
`,
Synopsis: []string{
"[--all] -q {{.LessThan}}queries{{.GreaterThan}} [{{.LessThan}}commit{{.GreaterThan}}]",
},
}
type FilterBranchCmd struct{}
// Name is returns the name of the Dolt cli command. This is what is used on the command line to invoke the command
func (cmd FilterBranchCmd) Name() string {
return "filter-branch"
}
// Description returns a description of the command
func (cmd FilterBranchCmd) Description() string {
return fmt.Sprintf("%s.", filterBranchDocs.ShortDesc)
}
func (cmd FilterBranchCmd) Docs() *cli.CommandDocumentation {
ap := cmd.ArgParser()
return cli.NewCommandDocumentation(filterBranchDocs, ap)
}
func (cmd FilterBranchCmd) ArgParser() *argparser.ArgParser {
ap := argparser.NewArgParserWithVariableArgs(cmd.Name())
ap.SupportsFlag(cli.VerboseFlag, "v", "logs more information")
ap.SupportsFlag(branchesFlag, "b", "filter all branches")
ap.SupportsFlag(cli.AllFlag, "a", "filter all branches and tags")
ap.SupportsFlag(continueFlag, "c", "log a warning and continue if any errors occur executing statements")
ap.SupportsString(QueryFlag, "q", "queries", "Queries to run, separated by semicolons. If not provided, queries are read from STDIN.")
return ap
}
// EventType returns the type of the event to log
func (cmd FilterBranchCmd) EventType() eventsapi.ClientEventType {
return eventsapi.ClientEventType_FILTER_BRANCH
}
// Exec executes the command
func (cmd FilterBranchCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int {
ap := cmd.ArgParser()
help, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, filterBranchDocs, ap))
apr := cli.ParseArgsOrDie(ap, args, help)
if apr.NArg() > 1 {
args := strings.Join(apr.Args, ", ")
verr := errhand.BuildDError("%s takes 0 or 1 args, %d provided: %s", cmd.Name(), apr.NArg(), args).Build()
return HandleVErrAndExitCode(verr, usage)
}
queryString := apr.GetValueOrDefault(QueryFlag, "")
verbose := apr.Contains(cli.VerboseFlag)
continueOnErr := apr.Contains(continueFlag)
// If we didn't get a query string, read one from STDIN
if len(queryString) == 0 {
queryStringBytes, err := io.ReadAll(cli.InStream)
if err != nil {
return HandleVErrAndExitCode(errhand.BuildDError("error reading from stdin").AddCause(err).Build(), usage)
}
queryString = string(queryStringBytes)
}
replay := func(ctx context.Context, commit, _, _ *doltdb.Commit) (*doltdb.RootValue, error) {
var cmHash, before hash.Hash
var root *doltdb.RootValue
if verbose {
var err error
cmHash, err = commit.HashOf()
if err != nil {
return nil, err
}
cli.Printf("processing commit %s\n", cmHash.String())
root, err = commit.GetRootValue(ctx)
if err != nil {
return nil, err
}
before, err = root.HashOf()
if err != nil {
return nil, err
}
}
updatedRoot, err := processFilterQuery(ctx, dEnv, commit, queryString, verbose, continueOnErr)
if err != nil {
return nil, err
}
if verbose {
after, err := updatedRoot.HashOf()
if err != nil {
return nil, err
}
if before != after {
cli.Printf("updated commit %s (root: %s -> %s)\n",
cmHash.String(), before.String(), after.String())
} else {
cli.Printf("no changes to commit %s", cmHash.String())
}
}
return updatedRoot, nil
}
nerf, err := getNerf(ctx, dEnv, apr)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
switch {
case apr.Contains(branchesFlag):
err = rebase.AllBranches(ctx, dEnv, replay, nerf)
case apr.Contains(cli.AllFlag):
err = rebase.AllBranchesAndTags(ctx, dEnv, replay, nerf)
default:
err = rebase.CurrentBranch(ctx, dEnv, replay, nerf)
}
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
return 0
}
func getNerf(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseResults) (rebase.NeedsRebaseFn, error) {
if apr.NArg() == 0 {
return rebase.EntireHistory(), nil
}
cs, err := doltdb.NewCommitSpec(apr.Arg(0))
if err != nil {
return nil, err
}
headRef, err := dEnv.RepoStateReader().CWBHeadRef()
if err != nil {
return nil, err
}
optCmt, err := dEnv.DoltDB.Resolve(ctx, cs, headRef)
if err != nil {
return nil, err
}
cm, ok := optCmt.ToCommit()
if !ok {
return nil, doltdb.ErrGhostCommitEncountered
}
return rebase.StopAtCommit(cm), nil
}
func processFilterQuery(ctx context.Context, dEnv *env.DoltEnv, cm *doltdb.Commit, query string, verbose bool, continueOnErr bool) (*doltdb.RootValue, error) {
sqlCtx, eng, err := rebaseSqlEngine(ctx, dEnv, cm)
if err != nil {
return nil, err
}
scanner := NewSqlStatementScanner(strings.NewReader(query))
if err != nil {
return nil, err
}
for scanner.Scan() {
q := scanner.Text()
if verbose {
cli.Printf("executing query: %s\n", q)
}
err = func() error {
_, itr, err := eng.Query(sqlCtx, q)
if err != nil {
return err
}
for {
_, err = itr.Next(sqlCtx)
if err == io.EOF {
break
} else if err != nil {
return err
}
}
return itr.Close(sqlCtx)
}()
if err != nil {
if continueOnErr {
if verbose {
cmHash, cmErr := cm.HashOf()
if cmErr != nil {
return nil, err
}
cli.PrintErrf("error encountered processing commit %s (continuing): %s\n", cmHash.String(), err.Error())
}
} else {
return nil, err
}
}
}
sess := dsess.DSessFromSess(sqlCtx.Session)
ws, err := sess.WorkingSet(sqlCtx, filterDbName)
if err != nil {
return nil, err
}
return ws.WorkingRoot(), nil
}
// rebaseSqlEngine packages up the context necessary to run sql queries against single root
// The SQL engine returned has transactions disabled. This is to prevent transactions starts from overwriting the root
// we set manually with the one at the working set of the HEAD being rebased.
// Some functionality will not work on this kind of engine, e.g. many DOLT_ functions.
func rebaseSqlEngine(ctx context.Context, dEnv *env.DoltEnv, cm *doltdb.Commit) (*sql.Context, *engine.SqlEngine, error) {
tmpDir, err := dEnv.TempTableFilesDir()
if err != nil {
return nil, nil, err
}
opts := editor.Options{Deaf: dEnv.DbEaFactory(), Tempdir: tmpDir}
db, err := dsqle.NewDatabase(ctx, filterDbName, dEnv.DbData(), opts)
if err != nil {
return nil, nil, err
}
mrEnv, err := env.MultiEnvForDirectory(ctx, dEnv.Config.WriteableConfig(), dEnv.FS, dEnv.Version, dEnv)
if err != nil {
return nil, nil, err
}
b := env.GetDefaultInitBranch(dEnv.Config)
pro, err := dsqle.NewDoltDatabaseProviderWithDatabase(b, mrEnv.FileSystem(), db, dEnv.FS)
if err != nil {
return nil, nil, err
}
sess := dsess.DefaultSession(pro)
sqlCtx := sql.NewContext(ctx, sql.WithSession(sess))
err = sqlCtx.SetSessionVariable(sqlCtx, sql.AutoCommitSessionVar, false)
if err != nil {
return nil, nil, err
}
err = sqlCtx.SetSessionVariable(sqlCtx, dsess.TransactionsDisabledSysVar, true)
if err != nil {
return nil, nil, err
}
parallelism := runtime.GOMAXPROCS(0)
azr := analyzer.NewBuilder(pro).WithParallelism(parallelism).Build()
root, err := cm.GetRootValue(ctx)
if err != nil {
return nil, nil, err
}
err = db.SetRoot(sqlCtx, root)
if err != nil {
return nil, nil, err
}
sqlCtx.SetCurrentDatabase(filterDbName)
se := engine.NewRebasedSqlEngine(sqle.New(azr, &sqle.Config{IsReadOnly: false}), map[string]dsess.SqlDatabase{filterDbName: db})
return sqlCtx, se, nil
}