diff --git a/make.go b/make.go index 6aa4013..9f48c07 100644 --- a/make.go +++ b/make.go @@ -139,28 +139,37 @@ func (u *upstream) get(gopath, repo, rev string) error { return rr.VCS.Create(dir, rr.Repo) } -func (u *upstream) tarballFromHoster() error { - var tarURL string +func (u *upstream) tarballUrl() (string, error) { repo := strings.TrimSuffix(u.rr.Repo, ".git") repoU, err := url.Parse(repo) if err != nil { - return fmt.Errorf("parse URL: %w", err) + return "", fmt.Errorf("parse URL: %w", err) } switch repoU.Host { case "github.com": - tarURL = fmt.Sprintf("%s/archive/%s.tar.%s", - repo, u.tag, u.compression) + return fmt.Sprintf("%s/archive/%s.tar.%s", + repo, u.tag, u.compression), nil case "gitlab.com", "salsa.debian.org": parts := strings.Split(repoU.Path, "/") if len(parts) < 3 { - return fmt.Errorf("incomplete repo URL: %s", u.rr.Repo) + return "", fmt.Errorf("incomplete repo URL: %s", u.rr.Repo) } project := parts[2] - tarURL = fmt.Sprintf("%s/-/archive/%s/%s-%s.tar.%s", - repo, u.tag, project, u.tag, u.compression) + return fmt.Sprintf("%s/-/archive/%s/%s-%s.tar.%s", + repo, u.tag, project, u.tag, u.compression), nil + case "git.sr.ht": + return fmt.Sprintf("%s/archive/%s.tar.%s", + repo, u.tag, u.compression), nil default: - return errUnsupportedHoster + return "", errUnsupportedHoster + } +} + +func (u *upstream) tarballFromHoster() error { + tarURL, err := u.tarballUrl() + if err != nil { + return err } done := make(chan struct{}) diff --git a/make_test.go b/make_test.go index 46908bb..b8402f7 100644 --- a/make_test.go +++ b/make_test.go @@ -2,6 +2,8 @@ package main import ( "testing" + + "golang.org/x/tools/go/vcs" ) var shortName = []struct { @@ -67,3 +69,30 @@ func TestDebianNameFromGopkg(t *testing.T) { } } } + +var tarballUrl = []struct { + repoRoot string + tag string + compression string + url string +}{ + {"https://github.com/Debian/dh-make-golang", "0.6.0", "gz", "https://github.com/Debian/dh-make-golang/archive/0.6.0.tar.gz"}, + {"https://github.com/Debian/dh-make-golang.git", "0.6.0", "gz", "https://github.com/Debian/dh-make-golang/archive/0.6.0.tar.gz"}, + {"https://gitlab.com/gitlab-org/labkit", "1.3.0", "gz", "https://gitlab.com/gitlab-org/labkit/-/archive/1.3.0/labkit-1.3.0.tar.gz"}, + {"https://git.sr.ht/~sircmpwn/getopt", "v1.0.0", "gz", "https://git.sr.ht/~sircmpwn/getopt/archive/v1.0.0.tar.gz"}, +} + +func TestUpstreamTarmballUrl(t *testing.T) { + for _, tt := range tarballUrl { + u := upstream{ + rr: &vcs.RepoRoot{Repo: tt.repoRoot}, + compression: tt.compression, + tag: tt.tag, + } + + url, _ := u.tarballUrl() + if url != tt.url { + t.Errorf("TestUpstreamTarmballUrl(%q) => %q, want %q", tt.repoRoot, url, tt.url) + } + } +}