Automatically detect default git branch
Instead of just assuming that the default branch is master, use ls-remote to find out. Also removed tests that didn't specifiy a branch but required authentication, because those will fail now that the repo is actually checked. Signed-off-by: Levi Harrison <levisamuelharrison@gmail.com>v0.9
parent
5fc0b3c30a
commit
8604d7cef2
|
@ -2,10 +2,13 @@ package source
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/moby/buildkit/util/sshutil"
|
"github.com/moby/buildkit/util/sshutil"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitIdentifier struct {
|
type GitIdentifier struct {
|
||||||
|
@ -19,6 +22,8 @@ type GitIdentifier struct {
|
||||||
KnownSSHHosts string
|
KnownSSHHosts string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultBranch = regexp.MustCompile(`refs/heads/(\S+)`)
|
||||||
|
|
||||||
func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
||||||
repo := GitIdentifier{}
|
repo := GitIdentifier{}
|
||||||
|
|
||||||
|
@ -35,6 +40,7 @@ func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
fragment = parts[1]
|
fragment = parts[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
repo.Ref, repo.Subdir = getRefAndSubdir(fragment)
|
repo.Ref, repo.Subdir = getRefAndSubdir(fragment)
|
||||||
} else {
|
} else {
|
||||||
u, err := url.Parse(remoteURL)
|
u, err := url.Parse(remoteURL)
|
||||||
|
@ -49,6 +55,15 @@ func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
|
||||||
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {
|
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {
|
||||||
repo.Subdir = ""
|
repo.Subdir = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if repo.Ref == "" {
|
||||||
|
var err error
|
||||||
|
repo.Ref, err = getDefaultBranch(repo.Remote)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &repo, nil
|
return &repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +79,7 @@ func isGitTransport(str string) bool {
|
||||||
|
|
||||||
func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
||||||
refAndDir := strings.SplitN(fragment, ":", 2)
|
refAndDir := strings.SplitN(fragment, ":", 2)
|
||||||
ref = "master"
|
ref = ""
|
||||||
if len(refAndDir[0]) != 0 {
|
if len(refAndDir[0]) != 0 {
|
||||||
ref = refAndDir[0]
|
ref = refAndDir[0]
|
||||||
}
|
}
|
||||||
|
@ -73,3 +88,17 @@ func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getDefaultBranch gets the default branch of a repository using ls-remote
|
||||||
|
func getDefaultBranch(remoteURL string) (string, error) {
|
||||||
|
out, err := exec.Command("git", "ls-remote", "--symref", remoteURL, "HEAD").CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Errorf("error fetching default branch for repository %s: %v", remoteURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ss := defaultBranch.FindAllStringSubmatch(string(out), -1)
|
||||||
|
if len(ss) == 0 || len(ss[0]) != 2 {
|
||||||
|
return "", errors.Errorf("could not find default branch for repository: %s", remoteURL)
|
||||||
|
}
|
||||||
|
return ss[0][1], nil
|
||||||
|
}
|
||||||
|
|
|
@ -7,19 +7,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewGitIdentifier(t *testing.T) {
|
func TestNewGitIdentifier(t *testing.T) {
|
||||||
gi, err := NewGitIdentifier("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git")
|
gi, err := NewGitIdentifier("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git#main")
|
||||||
require.Nil(t, err)
|
|
||||||
require.Equal(t, "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git", gi.Remote)
|
|
||||||
require.Equal(t, "master", gi.Ref)
|
|
||||||
require.Equal(t, "", gi.Subdir)
|
|
||||||
|
|
||||||
gi, err = NewGitIdentifier("ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git#main")
|
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
require.Equal(t, "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git", gi.Remote)
|
require.Equal(t, "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git", gi.Remote)
|
||||||
require.Equal(t, "main", gi.Ref)
|
require.Equal(t, "main", gi.Ref)
|
||||||
require.Equal(t, "", gi.Subdir)
|
require.Equal(t, "", gi.Subdir)
|
||||||
|
|
||||||
gi, err = NewGitIdentifier("git@github.com:moby/buildkit.git")
|
gi, err = NewGitIdentifier("git@github.com:moby/buildkit.git#master")
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
require.Equal(t, "git@github.com:moby/buildkit.git", gi.Remote)
|
require.Equal(t, "git@github.com:moby/buildkit.git", gi.Remote)
|
||||||
require.Equal(t, "master", gi.Ref)
|
require.Equal(t, "master", gi.Ref)
|
||||||
|
|
Loading…
Reference in New Issue