github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/actions/checkout.go (about) 1 // Copyright 2021 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 actions 16 17 import ( 18 "context" 19 20 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 21 "github.com/dolthub/dolt/go/libraries/doltcore/doltdocs" 22 "github.com/dolthub/dolt/go/libraries/doltcore/env" 23 ) 24 25 func CheckoutAllTables(ctx context.Context, dbData env.DbData) error { 26 roots, err := getRoots(ctx, dbData.Ddb, dbData.Rsr, doltdb.WorkingRoot, doltdb.StagedRoot, doltdb.HeadRoot) 27 28 if err != nil { 29 return err 30 } 31 32 tbls, err := doltdb.UnionTableNames(ctx, roots[doltdb.WorkingRoot], roots[doltdb.StagedRoot], roots[doltdb.HeadRoot]) 33 34 if err != nil { 35 return err 36 } 37 38 docs := doltdocs.SupportedDocs 39 40 return checkoutTablesAndDocs(ctx, dbData, roots, tbls, docs) 41 42 } 43 44 func CheckoutTables(ctx context.Context, dbData env.DbData, tables []string) error { 45 roots, err := getRoots(ctx, dbData.Ddb, dbData.Rsr, doltdb.WorkingRoot, doltdb.StagedRoot, doltdb.HeadRoot) 46 47 if err != nil { 48 return err 49 } 50 51 return checkoutTables(ctx, dbData, roots, tables) 52 } 53 54 // CheckoutTablesAndDocs takes in a set of tables and docs and checks them out to another branch. 55 func CheckoutTablesAndDocs(ctx context.Context, dbData env.DbData, tables []string, docs doltdocs.Docs) error { 56 roots, err := getRoots(ctx, dbData.Ddb, dbData.Rsr, doltdb.WorkingRoot, doltdb.StagedRoot, doltdb.HeadRoot) 57 58 if err != nil { 59 return err 60 } 61 62 return checkoutTablesAndDocs(ctx, dbData, roots, tables, docs) 63 } 64 65 func checkoutTables(ctx context.Context, dbData env.DbData, roots map[doltdb.RootType]*doltdb.RootValue, tbls []string) error { 66 unknownTbls := []string{} 67 68 currRoot := roots[doltdb.WorkingRoot] 69 staged := roots[doltdb.StagedRoot] 70 head := roots[doltdb.HeadRoot] 71 72 for _, tblName := range tbls { 73 if tblName == doltdb.DocTableName { 74 continue 75 } 76 tbl, ok, err := staged.GetTable(ctx, tblName) 77 78 if err != nil { 79 return err 80 } 81 82 if !ok { 83 tbl, ok, err = head.GetTable(ctx, tblName) 84 85 if err != nil { 86 return err 87 } 88 89 if !ok { 90 unknownTbls = append(unknownTbls, tblName) 91 continue 92 } 93 } 94 95 currRoot, err = currRoot.PutTable(ctx, tblName, tbl) 96 97 if err != nil { 98 return err 99 } 100 } 101 102 if len(unknownTbls) > 0 { 103 // Return table not exist error before RemoveTables, which fails silently if the table is not on the root. 104 err := validateTablesExist(ctx, currRoot, unknownTbls) 105 if err != nil { 106 return err 107 } 108 109 currRoot, err = currRoot.RemoveTables(ctx, unknownTbls...) 110 111 if err != nil { 112 return err 113 } 114 } 115 116 // update the working root with currRoot 117 _, err := env.UpdateWorkingRoot(ctx, dbData.Ddb, dbData.Rsw, currRoot) 118 119 return err 120 } 121 122 func checkoutDocs(ctx context.Context, dbData env.DbData, roots map[doltdb.RootType]*doltdb.RootValue, docs doltdocs.Docs) error { 123 currRoot := roots[doltdb.WorkingRoot] 124 staged := roots[doltdb.StagedRoot] 125 head := roots[doltdb.HeadRoot] 126 127 if len(docs) > 0 { 128 currRootWithDocs, stagedWithDocs, updatedDocs, err := getUpdatedWorkingAndStagedWithDocs(ctx, currRoot, staged, head, docs) 129 if err != nil { 130 return err 131 } 132 currRoot = currRootWithDocs 133 staged = stagedWithDocs 134 docs = updatedDocs 135 } 136 137 _, err := env.UpdateWorkingRoot(ctx, dbData.Ddb, dbData.Rsw, currRoot) 138 if err != nil { 139 return err 140 } 141 142 return dbData.Drw.WriteDocsToDisk(docs) 143 } 144 145 func checkoutTablesAndDocs(ctx context.Context, dbData env.DbData, roots map[doltdb.RootType]*doltdb.RootValue, tbls []string, docs doltdocs.Docs) error { 146 err := checkoutTables(ctx, dbData, roots, tbls) 147 148 if err != nil { 149 return err 150 } 151 152 roots, err = getRoots(ctx, dbData.Ddb, dbData.Rsr, doltdb.WorkingRoot, doltdb.StagedRoot, doltdb.HeadRoot) 153 154 if err != nil { 155 return err 156 } 157 158 return checkoutDocs(ctx, dbData, roots, docs) 159 }