github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/dolt_docs.go (about)

     1  // Copyright 2019 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 env
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    21  )
    22  
    23  // ResetWorkingDocsToStagedDocs resets the `dolt_docs` table on the working root to match the staged root.
    24  // If the `dolt_docs` table does not exist on the staged root, it will be removed from the working root.
    25  func ResetWorkingDocsToStagedDocs(ctx context.Context, ddb *doltdb.DoltDB, rsr RepoStateReader, rsw RepoStateWriter) error {
    26  	wrkRoot, err := WorkingRoot(ctx, ddb, rsr)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	stgRoot, err := StagedRoot(ctx, ddb, rsr)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	stgDocTbl, stgDocsFound, err := stgRoot.GetTable(ctx, doltdb.DocTableName)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	_, wrkDocsFound, err := wrkRoot.GetTable(ctx, doltdb.DocTableName)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	if wrkDocsFound && !stgDocsFound {
    47  		newWrkRoot, err := wrkRoot.RemoveTables(ctx, doltdb.DocTableName)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
    52  		return err
    53  	}
    54  
    55  	if stgDocsFound {
    56  		newWrkRoot, err := wrkRoot.PutTable(ctx, doltdb.DocTableName, stgDocTbl)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
    61  		return err
    62  	}
    63  	return nil
    64  }