mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
build: lookup the right git binary on WSL
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
42
util/gitutil/gitpath_unix.go
Normal file
42
util/gitutil/gitpath_unix.go
Normal file
@ -0,0 +1,42 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package gitutil
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/moby/sys/mountinfo"
|
||||
)
|
||||
|
||||
func gitPath(wd string) (string, error) {
|
||||
// On WSL2 we need to check if the current working directory is mounted on
|
||||
// a Windows drive and if so, we need to use the Windows git executable.
|
||||
if os.Getenv("WSL_DISTRO_NAME") != "" && wd != "" {
|
||||
// ensure any symlinks are resolved
|
||||
wdPath, err := filepath.EvalSymlinks(wd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(wdPath))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// find the longest mount point
|
||||
var idx, maxlen int
|
||||
for i := range mi {
|
||||
if len(mi[i].Mountpoint) > maxlen {
|
||||
maxlen = len(mi[i].Mountpoint)
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
if mi[idx].FSType == "9p" {
|
||||
if p, err := exec.LookPath("git.exe"); err == nil {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return exec.LookPath("git")
|
||||
}
|
9
util/gitutil/gitpath_windows.go
Normal file
9
util/gitutil/gitpath_windows.go
Normal file
@ -0,0 +1,9 @@
|
||||
package gitutil
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func gitPath(wd string) (string, error) {
|
||||
return exec.LookPath("git.exe")
|
||||
}
|
@ -11,8 +11,9 @@ import (
|
||||
|
||||
// Git represents an active git object
|
||||
type Git struct {
|
||||
ctx context.Context
|
||||
wd string
|
||||
ctx context.Context
|
||||
wd string
|
||||
gitpath string
|
||||
}
|
||||
|
||||
// Option provides a variadic option for configuring the git client.
|
||||
@ -33,14 +34,22 @@ func WithWorkingDir(wd string) Option {
|
||||
}
|
||||
|
||||
// New initializes a new git client
|
||||
func New(opts ...Option) *Git {
|
||||
func New(opts ...Option) (*Git, error) {
|
||||
var err error
|
||||
c := &Git{
|
||||
ctx: context.Background(),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
return c
|
||||
|
||||
c.gitpath, err = gitPath(c.wd)
|
||||
if err != nil {
|
||||
return nil, errors.New("git not found in PATH")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Git) IsInsideWorkTree() bool {
|
||||
@ -89,16 +98,12 @@ func (c *Git) Tag() (string, error) {
|
||||
}
|
||||
|
||||
func (c *Git) run(args ...string) (string, error) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
return "", errors.New("git not present in PATH")
|
||||
}
|
||||
|
||||
var extraArgs = []string{
|
||||
"-c", "log.showSignature=false",
|
||||
}
|
||||
|
||||
args = append(extraArgs, args...)
|
||||
cmd := exec.Command("git", args...)
|
||||
cmd := exec.CommandContext(c.ctx, c.gitpath, args...)
|
||||
if c.wd != "" {
|
||||
cmd.Dir = c.wd
|
||||
}
|
||||
|
@ -3,11 +3,14 @@ package gitutil
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGit(t *testing.T) {
|
||||
c := New()
|
||||
c, err := New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
out, err := c.run("status")
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, out)
|
||||
@ -19,50 +22,58 @@ func TestGit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGitFullCommit(t *testing.T) {
|
||||
Mktmp(t)
|
||||
GitInit(t)
|
||||
GitCommit(t, "bar")
|
||||
c, err := New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
Mktmp(t)
|
||||
GitInit(c, t)
|
||||
GitCommit(c, t, "bar")
|
||||
|
||||
c := New()
|
||||
out, err := c.FullCommit()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 40, len(out))
|
||||
}
|
||||
|
||||
func TestGitShortCommit(t *testing.T) {
|
||||
Mktmp(t)
|
||||
GitInit(t)
|
||||
GitCommit(t, "bar")
|
||||
c, err := New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
Mktmp(t)
|
||||
GitInit(c, t)
|
||||
GitCommit(c, t, "bar")
|
||||
|
||||
c := New()
|
||||
out, err := c.ShortCommit()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 7, len(out))
|
||||
}
|
||||
|
||||
func TestGitTagsPointsAt(t *testing.T) {
|
||||
Mktmp(t)
|
||||
GitInit(t)
|
||||
GitCommit(t, "bar")
|
||||
GitTag(t, "v0.8.0")
|
||||
GitCommit(t, "foo")
|
||||
GitTag(t, "v0.9.0")
|
||||
c, err := New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
Mktmp(t)
|
||||
GitInit(c, t)
|
||||
GitCommit(c, t, "bar")
|
||||
GitTag(c, t, "v0.8.0")
|
||||
GitCommit(c, t, "foo")
|
||||
GitTag(c, t, "v0.9.0")
|
||||
|
||||
c := New()
|
||||
out, err := c.clean(c.run("tag", "--points-at", "HEAD", "--sort", "-version:creatordate"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "v0.9.0", out)
|
||||
}
|
||||
|
||||
func TestGitDescribeTags(t *testing.T) {
|
||||
Mktmp(t)
|
||||
GitInit(t)
|
||||
GitCommit(t, "bar")
|
||||
GitTag(t, "v0.8.0")
|
||||
GitCommit(t, "foo")
|
||||
GitTag(t, "v0.9.0")
|
||||
c, err := New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
Mktmp(t)
|
||||
GitInit(c, t)
|
||||
GitCommit(c, t, "bar")
|
||||
GitTag(c, t, "v0.8.0")
|
||||
GitCommit(c, t, "foo")
|
||||
GitTag(c, t, "v0.9.0")
|
||||
|
||||
c := New()
|
||||
out, err := c.clean(c.run("describe", "--tags", "--abbrev=0"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "v0.9.0", out)
|
||||
|
@ -7,46 +7,46 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func GitInit(tb testing.TB) {
|
||||
func GitInit(c *Git, tb testing.TB) {
|
||||
tb.Helper()
|
||||
out, err := fakeGit("init")
|
||||
out, err := fakeGit(c, "init")
|
||||
require.NoError(tb, err)
|
||||
require.Contains(tb, out, "Initialized empty Git repository")
|
||||
require.NoError(tb, err)
|
||||
GitCheckoutBranch(tb, "main")
|
||||
_, _ = fakeGit("branch", "-D", "master")
|
||||
GitCheckoutBranch(c, tb, "main")
|
||||
_, _ = fakeGit(c, "branch", "-D", "master")
|
||||
}
|
||||
|
||||
func GitCommit(tb testing.TB, msg string) {
|
||||
func GitCommit(c *Git, tb testing.TB, msg string) {
|
||||
tb.Helper()
|
||||
out, err := fakeGit("commit", "--allow-empty", "-m", msg)
|
||||
out, err := fakeGit(c, "commit", "--allow-empty", "-m", msg)
|
||||
require.NoError(tb, err)
|
||||
require.Contains(tb, out, "main", msg)
|
||||
}
|
||||
|
||||
func GitTag(tb testing.TB, tag string) {
|
||||
func GitTag(c *Git, tb testing.TB, tag string) {
|
||||
tb.Helper()
|
||||
out, err := fakeGit("tag", tag)
|
||||
out, err := fakeGit(c, "tag", tag)
|
||||
require.NoError(tb, err)
|
||||
require.Empty(tb, out)
|
||||
}
|
||||
|
||||
func GitCheckoutBranch(tb testing.TB, name string) {
|
||||
func GitCheckoutBranch(c *Git, tb testing.TB, name string) {
|
||||
tb.Helper()
|
||||
out, err := fakeGit("checkout", "-b", name)
|
||||
out, err := fakeGit(c, "checkout", "-b", name)
|
||||
require.NoError(tb, err)
|
||||
require.Empty(tb, out)
|
||||
}
|
||||
|
||||
func GitAdd(tb testing.TB, file string) {
|
||||
func GitAdd(c *Git, tb testing.TB, file string) {
|
||||
tb.Helper()
|
||||
_, err := fakeGit("add", file)
|
||||
_, err := fakeGit(c, "add", file)
|
||||
require.NoError(tb, err)
|
||||
}
|
||||
|
||||
func GitSetRemote(tb testing.TB, url string) {
|
||||
func GitSetRemote(c *Git, tb testing.TB, url string) {
|
||||
tb.Helper()
|
||||
_, err := fakeGit("remote", "add", "origin", url)
|
||||
_, err := fakeGit(c, "remote", "add", "origin", url)
|
||||
require.NoError(tb, err)
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ func Mktmp(tb testing.TB) string {
|
||||
return folder
|
||||
}
|
||||
|
||||
func fakeGit(args ...string) (string, error) {
|
||||
func fakeGit(c *Git, args ...string) (string, error) {
|
||||
allArgs := []string{
|
||||
"-c", "user.name=buildx",
|
||||
"-c", "user.email=buildx@docker.com",
|
||||
@ -71,6 +71,5 @@ func fakeGit(args ...string) (string, error) {
|
||||
"-c", "log.showSignature=false",
|
||||
}
|
||||
allArgs = append(allArgs, args...)
|
||||
c := New()
|
||||
return c.clean(c.run(allArgs...))
|
||||
}
|
||||
|
Reference in New Issue
Block a user