github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dprocedures/dolt_clean.go (about) 1 // Copyright 2022 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 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 22 "github.com/dolthub/dolt/go/cmd/dolt/cli" 23 "github.com/dolthub/dolt/go/libraries/doltcore/branch_control" 24 "github.com/dolthub/dolt/go/libraries/doltcore/env/actions" 25 "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" 26 ) 27 28 // doltClean is the stored procedure version for the CLI command `dolt clean`. 29 func doltClean(ctx *sql.Context, args ...string) (sql.RowIter, error) { 30 res, err := doDoltClean(ctx, args) 31 if err != nil { 32 return nil, err 33 } 34 return rowToIter(int64(res)), nil 35 } 36 37 func doDoltClean(ctx *sql.Context, args []string) (int, error) { 38 dbName := ctx.GetCurrentDatabase() 39 40 if len(dbName) == 0 { 41 return 1, fmt.Errorf("Empty database name.") 42 } 43 if err := branch_control.CheckAccess(ctx, branch_control.Permissions_Write); err != nil { 44 return statusErr, err 45 } 46 47 dSess := dsess.DSessFromSess(ctx.Session) 48 49 apr, err := cli.CreateCleanArgParser().Parse(args) 50 if err != nil { 51 return 1, err 52 } 53 54 // Get all the needed roots. 55 roots, ok := dSess.GetRoots(ctx, dbName) 56 if !ok { 57 return 1, fmt.Errorf("Could not load database %s", dbName) 58 } 59 60 roots, err = actions.CleanUntracked(ctx, roots, apr.Args, apr.ContainsAll(cli.DryRunFlag), false) 61 if err != nil { 62 return 1, fmt.Errorf("failed to clean; %w", err) 63 } 64 65 err = dSess.SetRoots(ctx, dbName, roots) 66 if err != nil { 67 return 1, err 68 } 69 return 0, nil 70 }