Skip to content

Commit

Permalink
make: download tarballs from sr.ht
Browse files Browse the repository at this point in the history
and add a test for the tarball URL generation.
  • Loading branch information
supertassu authored and anthonyfok committed Mar 22, 2024
1 parent f8c615b commit b45756b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 18 additions & 9 deletions make.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
29 changes: 29 additions & 0 deletions make_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"testing"

"golang.org/x/tools/go/vcs"
)

var shortName = []struct {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit b45756b

Please sign in to comment.