github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/actions/remotes.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 actions
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
    24  	"github.com/dolthub/dolt/go/store/datas"
    25  )
    26  
    27  var ErrCantFF = errors.New("can't fast forward merge")
    28  
    29  // Push will update a destination branch, in a given destination database if it can be done as a fast forward merge.
    30  // This is accomplished first by verifying that the remote tracking reference for the source database can be updated to
    31  // the given commit via a fast forward merge.  If this is the case, an attempt will be made to update the branch in the
    32  // destination db to the given commit via fast forward move.  If that succeeds the tracking branch is updated in the
    33  // source db.
    34  func Push(ctx context.Context, dEnv *env.DoltEnv, mode ref.UpdateMode, destRef ref.BranchRef, remoteRef ref.RemoteRef, srcDB, destDB *doltdb.DoltDB, commit *doltdb.Commit, progChan chan datas.PullProgress, pullerEventCh chan datas.PullerEvent) error {
    35  	var err error
    36  	if mode == ref.FastForwardOnly {
    37  		canFF, err := srcDB.CanFastForward(ctx, remoteRef, commit)
    38  
    39  		if err != nil {
    40  			return err
    41  		} else if !canFF {
    42  			return ErrCantFF
    43  		}
    44  	}
    45  
    46  	rf, err := commit.GetStRef()
    47  
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	err = destDB.PushChunks(ctx, dEnv.TempTableFilesDir(), srcDB, rf, progChan, pullerEventCh)
    53  
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	switch mode {
    59  	case ref.ForceUpdate:
    60  		err = destDB.SetHeadToCommit(ctx, destRef, commit)
    61  		if err != nil {
    62  			return err
    63  		}
    64  		err = srcDB.SetHeadToCommit(ctx, remoteRef, commit)
    65  	case ref.FastForwardOnly:
    66  		err = destDB.FastForward(ctx, destRef, commit)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		err = srcDB.FastForward(ctx, remoteRef, commit)
    71  	}
    72  
    73  	return err
    74  }
    75  
    76  // PushTag pushes a commit tag and all underlying data from a local source database to a remote destination database.
    77  func PushTag(ctx context.Context, dEnv *env.DoltEnv, destRef ref.TagRef, srcDB, destDB *doltdb.DoltDB, tag *doltdb.Tag, progChan chan datas.PullProgress, pullerEventCh chan datas.PullerEvent) error {
    78  	var err error
    79  
    80  	rf, err := tag.GetStRef()
    81  
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	err = destDB.PushChunks(ctx, dEnv.TempTableFilesDir(), srcDB, rf, progChan, pullerEventCh)
    87  
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	return destDB.SetHead(ctx, destRef, rf)
    93  }
    94  
    95  // DeleteRemoteBranch validates targetRef is a branch on the remote database, and then deletes it, then deletes the
    96  // remote tracking branch from the local database.
    97  func DeleteRemoteBranch(ctx context.Context, targetRef ref.BranchRef, remoteRef ref.RemoteRef, localDB, remoteDB *doltdb.DoltDB) error {
    98  	hasRef, err := remoteDB.HasRef(ctx, targetRef)
    99  
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	if hasRef {
   105  		err = remoteDB.DeleteBranch(ctx, targetRef)
   106  	}
   107  
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	err = localDB.DeleteBranch(ctx, remoteRef)
   113  
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  // FetchCommit takes a fetches a commit and all underlying data from a remote source database to the local destination database.
   122  func FetchCommit(ctx context.Context, dEnv *env.DoltEnv, srcDB, destDB *doltdb.DoltDB, srcDBCommit *doltdb.Commit, progChan chan datas.PullProgress, pullerEventCh chan datas.PullerEvent) error {
   123  	stRef, err := srcDBCommit.GetStRef()
   124  
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	return destDB.PullChunks(ctx, dEnv.TempTableFilesDir(), srcDB, stRef, progChan, pullerEventCh)
   130  }
   131  
   132  // FetchCommit takes a fetches a commit tag and all underlying data from a remote source database to the local destination database.
   133  func FetchTag(ctx context.Context, dEnv *env.DoltEnv, srcDB, destDB *doltdb.DoltDB, srcDBTag *doltdb.Tag, progChan chan datas.PullProgress, pullerEventCh chan datas.PullerEvent) error {
   134  	stRef, err := srcDBTag.GetStRef()
   135  
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	return destDB.PullChunks(ctx, dEnv.TempTableFilesDir(), srcDB, stRef, progChan, pullerEventCh)
   141  }
   142  
   143  // Clone pulls all data from a remote source database to a local destination database.
   144  func Clone(ctx context.Context, srcDB, destDB *doltdb.DoltDB, eventCh chan<- datas.TableFileEvent) error {
   145  	return srcDB.Clone(ctx, destDB, eventCh)
   146  }