github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dprocedures/dolt_cherry_pick.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dprocedures 16 17 import ( 18 "errors" 19 "fmt" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 gmstypes "github.com/dolthub/go-mysql-server/sql/types" 23 24 "github.com/dolthub/dolt/go/cmd/dolt/cli" 25 "github.com/dolthub/dolt/go/libraries/doltcore/branch_control" 26 "github.com/dolthub/dolt/go/libraries/doltcore/cherry_pick" 27 ) 28 29 var ErrEmptyCherryPick = errors.New("cannot cherry-pick empty string") 30 31 var cherryPickSchema = []*sql.Column{ 32 { 33 Name: "hash", 34 Type: gmstypes.LongText, 35 Nullable: true, 36 }, 37 { 38 Name: "data_conflicts", 39 Type: gmstypes.Int64, 40 Nullable: false, 41 }, 42 { 43 Name: "schema_conflicts", 44 Type: gmstypes.Int64, 45 Nullable: false, 46 }, 47 { 48 Name: "constraint_violations", 49 Type: gmstypes.Int64, 50 Nullable: false, 51 }, 52 } 53 54 // doltCherryPick is the stored procedure version for the CLI command `dolt cherry-pick`. 55 func doltCherryPick(ctx *sql.Context, args ...string) (sql.RowIter, error) { 56 newCommitHash, dataConflicts, schemaConflicts, constraintViolations, err := doDoltCherryPick(ctx, args) 57 if err != nil { 58 return nil, err 59 } 60 return rowToIter(newCommitHash, dataConflicts, schemaConflicts, constraintViolations), nil 61 } 62 63 // doDoltCherryPick attempts to perform a cherry-pick merge based on the arguments specified in |args| and returns 64 // the new, created commit hash (if it was successful created), a count of the number of tables with data conflicts, 65 // a count of the number of tables with schema conflicts, and a count of the number of tables with constraint violations. 66 func doDoltCherryPick(ctx *sql.Context, args []string) (string, int, int, int, error) { 67 // Get the information for the sql context. 68 dbName := ctx.GetCurrentDatabase() 69 if len(dbName) == 0 { 70 return "", 0, 0, 0, fmt.Errorf("error: empty database name") 71 } 72 73 if err := branch_control.CheckAccess(ctx, branch_control.Permissions_Write); err != nil { 74 return "", 0, 0, 0, err 75 } 76 77 apr, err := cli.CreateCherryPickArgParser().Parse(args) 78 if err != nil { 79 return "", 0, 0, 0, err 80 } 81 82 if apr.Contains(cli.AbortParam) { 83 return "", 0, 0, 0, cherry_pick.AbortCherryPick(ctx, dbName) 84 } 85 86 // we only support cherry-picking a single commit for now. 87 if apr.NArg() == 0 { 88 return "", 0, 0, 0, ErrEmptyCherryPick 89 } else if apr.NArg() > 1 { 90 return "", 0, 0, 0, fmt.Errorf("cherry-picking multiple commits is not supported yet") 91 } 92 93 cherryStr := apr.Arg(0) 94 if len(cherryStr) == 0 { 95 return "", 0, 0, 0, ErrEmptyCherryPick 96 } 97 98 commit, mergeResult, err := cherry_pick.CherryPick(ctx, cherryStr, cherry_pick.CherryPickOptions{}) 99 if err != nil { 100 return "", 0, 0, 0, err 101 } 102 103 if mergeResult != nil { 104 return "", 105 mergeResult.CountOfTablesWithDataConflicts(), 106 mergeResult.CountOfTablesWithSchemaConflicts(), 107 mergeResult.CountOfTablesWithConstraintViolations(), 108 nil 109 } 110 111 return commit, 0, 0, 0, nil 112 }