github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/ref/remote_ref.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 ref
    16  
    17  import (
    18  	"path"
    19  	"strings"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/utils/strhelp"
    22  )
    23  
    24  // RemoteRef is a reference to a reference that tracks a branch on a remote
    25  type RemoteRef struct {
    26  	remote string
    27  	branch string
    28  }
    29  
    30  // GetType returns RemoteRefType
    31  func (rr RemoteRef) GetType() RefType {
    32  	return RemoteRefType
    33  }
    34  
    35  // GetPath returns the remote name separated by the branch e.g. origin/master
    36  func (rr RemoteRef) GetPath() string {
    37  	return path.Join(rr.remote, rr.branch)
    38  }
    39  
    40  // String returns the fully qualified reference e.g. refs/remotes/origin/master
    41  func (rr RemoteRef) String() string {
    42  	return String(rr)
    43  }
    44  
    45  // GetRemote returns the name of the remote that this reference is referring to.
    46  func (rr RemoteRef) GetRemote() string {
    47  	return rr.remote
    48  }
    49  
    50  // GetBranch returns the name of a remote branch
    51  func (rr RemoteRef) GetBranch() string {
    52  	return rr.branch
    53  }
    54  
    55  // NewRemoteRef creates a remote ref from an origin name and a path
    56  func NewRemoteRef(remote, branch string) RemoteRef {
    57  	return RemoteRef{remote, branch}
    58  }
    59  
    60  // NewRemoteRefFromPathString creates a DoltRef from a string in the format origin/master, or remotes/origin/master, or
    61  // refs/remotes/origin/master
    62  func NewRemoteRefFromPathStr(remoteAndPath string) (DoltRef, error) {
    63  	if IsRef(remoteAndPath) {
    64  		prefix := PrefixForType(RemoteRefType)
    65  		if strings.HasPrefix(remoteAndPath, prefix) {
    66  			remoteAndPath = remoteAndPath[len(prefix):]
    67  		} else {
    68  			panic(remoteAndPath + " is a ref that is not of type " + prefix)
    69  		}
    70  	} else if strings.HasPrefix(remoteAndPath, remotesPrefix) {
    71  		remoteAndPath = remoteAndPath[len(remotesPrefix):]
    72  	}
    73  
    74  	remote, ok := strhelp.NthToken(remoteAndPath, '/', 0)
    75  
    76  	if !ok {
    77  		return nil, ErrInvalidRefSpec
    78  	}
    79  
    80  	branch := remoteAndPath[len(remote)+1:]
    81  
    82  	if remote == "" || branch == "" {
    83  		return nil, ErrInvalidRefSpec
    84  	}
    85  
    86  	return RemoteRef{remote, branch}, nil
    87  }