github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/git/remote.go (about)

     1  package git
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     7  	giturls "github.com/whilp/git-urls"
     8  )
     9  
    10  type GitRemote string
    11  
    12  func (gr GitRemote) String() string {
    13  	return string(gr)
    14  }
    15  
    16  func ProvideGitRemote() GitRemote {
    17  	return GitRemote(normalizeGitRemote(gitOrigin(".")))
    18  }
    19  
    20  func gitOrigin(fromDir string) string {
    21  	cmd := exec.Command("git", "-C", fromDir, "remote", "get-url", "origin")
    22  	b, err := cmd.Output()
    23  	if err != nil {
    24  		return ""
    25  	}
    26  
    27  	return strings.TrimRight(string(b), "\n")
    28  }
    29  
    30  func normalizeGitRemote(s string) string {
    31  	u, err := giturls.Parse(s)
    32  	if err != nil {
    33  		return s
    34  	}
    35  
    36  	// treat "http://", "https://", "git://", "ssh://", etc as equiv
    37  	u.Scheme = ""
    38  	u.User = nil
    39  
    40  	// github.com/tilt-dev/tilt is the same as github.com/tilt-dev/tilt/
    41  	u.Path = strings.TrimSuffix(u.Path, "/")
    42  	// github.com/tilt-dev/tilt is the same as github.com/tilt-dev/tilt.git
    43  	u.Path = strings.TrimSuffix(u.Path, ".git")
    44  
    45  	return u.String()
    46  }