github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/release/remote.go (about)

     1  package release
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/goreleaser/goreleaser/internal/git"
     8  	"github.com/goreleaser/goreleaser/pkg/config"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // remoteRepo gets the repo name from the Git config.
    13  func remoteRepo() (result config.Repo, err error) {
    14  	if !git.IsRepo() {
    15  		return result, errors.New("current folder is not a git repository")
    16  	}
    17  	out, err := git.Run("config", "--get", "remote.origin.url")
    18  	if err != nil {
    19  		return result, fmt.Errorf("repository doesn't have an `origin` remote")
    20  	}
    21  	return extractRepoFromURL(out), nil
    22  }
    23  
    24  func extractRepoFromURL(s string) config.Repo {
    25  	// removes the .git suffix and any new lines
    26  	s = strings.NewReplacer(
    27  		".git", "",
    28  		"\n", "",
    29  	).Replace(s)
    30  	// if the URL contains a :, indicating a SSH config,
    31  	// remove all chars until it, including itself
    32  	// on HTTP and HTTPS URLs it will remove the http(s): prefix,
    33  	// which is ok. On SSH URLs the whole user@server will be removed,
    34  	// which is required.
    35  	s = s[strings.LastIndex(s, ":")+1:]
    36  	// split by /, the last to parts should be the owner and name
    37  	ss := strings.Split(s, "/")
    38  	return config.Repo{
    39  		Owner: ss[len(ss)-2],
    40  		Name:  ss[len(ss)-1],
    41  	}
    42  }