2020-07-09 22:40:37 +02:00
|
|
|
package git
|
2017-01-14 20:12:20 +02:00
|
|
|
|
|
|
|
import (
|
2022-04-12 13:35:19 +02:00
|
|
|
"context"
|
2020-09-21 19:47:51 +02:00
|
|
|
"errors"
|
2018-02-26 01:17:45 +02:00
|
|
|
"fmt"
|
2021-08-21 15:57:19 +02:00
|
|
|
"net/url"
|
2022-09-11 20:32:23 +02:00
|
|
|
"path"
|
2017-01-14 20:12:20 +02:00
|
|
|
"strings"
|
2017-03-23 02:01:29 +02:00
|
|
|
|
2022-06-22 02:11:15 +02:00
|
|
|
"github.com/caarlos0/log"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2017-01-14 20:12:20 +02:00
|
|
|
)
|
|
|
|
|
2020-07-09 22:40:37 +02:00
|
|
|
// ExtractRepoFromConfig gets the repo name from the Git config.
|
2022-04-12 13:35:19 +02:00
|
|
|
func ExtractRepoFromConfig(ctx context.Context) (result config.Repo, err error) {
|
|
|
|
if !IsRepo(ctx) {
|
2017-10-16 19:43:26 +02:00
|
|
|
return result, errors.New("current folder is not a git repository")
|
2017-10-15 21:46:21 +02:00
|
|
|
}
|
2022-05-09 14:32:43 +02:00
|
|
|
out, err := Clean(Run(ctx, "ls-remote", "--get-url"))
|
2017-01-14 20:12:20 +02:00
|
|
|
if err != nil {
|
2021-09-15 21:12:45 +02:00
|
|
|
return result, fmt.Errorf("no remote configured to list refs from")
|
2017-01-14 20:12:20 +02:00
|
|
|
}
|
2021-09-28 15:49:59 +02:00
|
|
|
log.WithField("rawurl", out).Debugf("got git url")
|
2021-08-21 15:57:19 +02:00
|
|
|
return ExtractRepoFromURL(out)
|
2017-01-14 20:12:20 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 15:57:19 +02:00
|
|
|
func ExtractRepoFromURL(rawurl string) (config.Repo, error) {
|
2018-02-20 23:21:20 +02:00
|
|
|
// removes the .git suffix and any new lines
|
2021-08-21 15:57:19 +02:00
|
|
|
s := strings.TrimSuffix(strings.TrimSpace(rawurl), ".git")
|
|
|
|
|
2018-02-20 23:21:20 +02:00
|
|
|
// if the URL contains a :, indicating a SSH config,
|
|
|
|
// remove all chars until it, including itself
|
|
|
|
// on HTTP and HTTPS URLs it will remove the http(s): prefix,
|
|
|
|
// which is ok. On SSH URLs the whole user@server will be removed,
|
|
|
|
// which is required.
|
2021-09-28 02:08:22 +02:00
|
|
|
|
|
|
|
// If the url contains more than 1 ':' character, assume we are doing an
|
|
|
|
// http URL with a username/password in it, and normalize the URL.
|
|
|
|
// Gitlab-CI uses this type of URL
|
|
|
|
if strings.Count(s, ":") == 1 {
|
|
|
|
s = s[strings.LastIndex(s, ":")+1:]
|
|
|
|
}
|
2021-08-21 15:57:19 +02:00
|
|
|
|
|
|
|
// now we can parse it with net/url
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
2022-05-09 14:32:43 +02:00
|
|
|
return config.Repo{
|
|
|
|
RawURL: rawurl,
|
|
|
|
}, err
|
2021-08-21 15:57:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 05:34:14 +02:00
|
|
|
// split the parsed url path by /, the last parts should be the owner and name
|
2021-08-21 15:57:19 +02:00
|
|
|
ss := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
|
|
|
|
|
2022-05-09 14:32:43 +02:00
|
|
|
// if empty, returns an error
|
|
|
|
if len(ss) == 0 || ss[0] == "" {
|
|
|
|
return config.Repo{
|
|
|
|
RawURL: rawurl,
|
|
|
|
}, fmt.Errorf("unsupported repository URL: %s", rawurl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if less than 2 parts, its likely not a valid repository, but we'll allow it.
|
2021-08-21 15:57:19 +02:00
|
|
|
if len(ss) < 2 {
|
2022-05-09 14:32:43 +02:00
|
|
|
return config.Repo{
|
|
|
|
RawURL: rawurl,
|
|
|
|
Owner: ss[0],
|
|
|
|
}, nil
|
2021-08-21 15:57:19 +02:00
|
|
|
}
|
2021-09-28 15:48:49 +02:00
|
|
|
repo := config.Repo{
|
2022-05-09 14:32:43 +02:00
|
|
|
RawURL: rawurl,
|
2022-09-11 20:32:23 +02:00
|
|
|
Owner: path.Join(ss[:len(ss)-1]...),
|
2022-05-09 14:32:43 +02:00
|
|
|
Name: ss[len(ss)-1],
|
2021-09-28 15:48:49 +02:00
|
|
|
}
|
|
|
|
log.WithField("owner", repo.Owner).WithField("name", repo.Name).Debugf("parsed url")
|
|
|
|
return repo, nil
|
2017-01-14 20:12:20 +02:00
|
|
|
}
|