github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/git/getter.go (about)

     1  // This file mainly provides some functions that can be used to adapt for git downloading by go-getter.
     2  package git
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/go-getter"
     8  	"kcl-lang.io/kpm/pkg/constants"
     9  )
    10  
    11  var goGetterGetters = map[string]getter.Getter{
    12  	"git": new(getter.GitGetter),
    13  }
    14  
    15  var goGetterNoDetectors = []getter.Detector{}
    16  
    17  const GIT_PROTOCOL = "git::"
    18  
    19  func ForceProtocol(url, protocol string) string {
    20  	return protocol + url
    21  }
    22  
    23  // ForceGitUrl will add the branch, tag or commit to the git URL and force it to the git protocol
    24  // `<URL>` will return `Git::<URL>?ref=<branch|tag|commit>`
    25  func (cloneOpts *CloneOptions) ForceGitUrl() (string, error) {
    26  	if err := cloneOpts.Validate(); err != nil {
    27  		return "", nil
    28  	}
    29  
    30  	var attributes = []string{cloneOpts.Branch, cloneOpts.Commit, cloneOpts.Tag}
    31  	for _, attr := range attributes {
    32  		if attr != "" {
    33  			return ForceProtocol(
    34  				cloneOpts.RepoURL+fmt.Sprintf(constants.GIT_PROTOCOL_URL_PATTERN, attr),
    35  				GIT_PROTOCOL,
    36  			), nil
    37  		}
    38  	}
    39  
    40  	return ForceProtocol(cloneOpts.RepoURL, GIT_PROTOCOL), nil
    41  }