github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/ref/branch_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 "strings"
    18  
    19  var EmptyBranchRef = BranchRef{""}
    20  
    21  // BranchRef is a reference to a branch
    22  type BranchRef struct {
    23  	branch string
    24  }
    25  
    26  // GetType will return BranchRefType
    27  func (br BranchRef) GetType() RefType {
    28  	return BranchRefType
    29  }
    30  
    31  // GetPath returns the name of the branch
    32  func (br BranchRef) GetPath() string {
    33  	return br.branch
    34  }
    35  
    36  // String returns the fully qualified reference name e.g. refs/heads/master
    37  func (br BranchRef) String() string {
    38  	return String(br)
    39  }
    40  
    41  func (br BranchRef) MarshalJSON() ([]byte, error) {
    42  	return MarshalJSON(br)
    43  }
    44  
    45  // NewBranchRef creates a reference to a local branch from a branch name or a branch ref e.g. master, or refs/heads/master
    46  func NewBranchRef(branchName string) BranchRef {
    47  	if IsRef(branchName) {
    48  		prefix := PrefixForType(BranchRefType)
    49  		if strings.HasPrefix(branchName, prefix) {
    50  			branchName = branchName[len(prefix):]
    51  		} else {
    52  			panic(branchName + " is a ref that is not of type " + prefix)
    53  		}
    54  	}
    55  
    56  	return BranchRef{branchName}
    57  }