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