wip: git: wrap upstream gitutil cli

Signed-off-by: Justin Chadwell <me@jedevc.com>
pull/2005/head
Justin Chadwell 1 year ago
parent 76c60890c0
commit ff63bfbce5

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/docker/buildx/util/gitutil" "github.com/docker/buildx/util/gitutil"
bkgitutil "github.com/moby/buildkit/util/gitutil"
specs "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -49,7 +50,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
wd, _ = filepath.Abs(filepath.Join(cwd, contextPath)) wd, _ = filepath.Abs(filepath.Join(cwd, contextPath))
} }
gitc, err := gitutil.New(gitutil.WithContext(ctx), gitutil.WithWorkingDir(wd)) gitc, err := gitutil.New(bkgitutil.WithDir(wd))
if err != nil { if err != nil {
if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() { if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() {
return res, errors.New("buildx: git was not found in the system. Current commit information was not captured by the build") return res, errors.New("buildx: git was not found in the system. Current commit information was not captured by the build")
@ -57,14 +58,14 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
return return
} }
if !gitc.IsInsideWorkTree() { if !gitc.IsInsideWorkTree(ctx) {
if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() { if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() {
return res, errors.New("buildx: failed to read current commit information with git rev-parse --is-inside-work-tree") return res, errors.New("buildx: failed to read current commit information with git rev-parse --is-inside-work-tree")
} }
return res, nil return res, nil
} }
if sha, err := gitc.FullCommit(); err != nil && !gitutil.IsUnknownRevision(err) { if sha, err := gitc.FullCommit(ctx); err != nil && !gitutil.IsUnknownRevision(err) {
return res, errors.Wrapf(err, "buildx: failed to get git commit") return res, errors.Wrapf(err, "buildx: failed to get git commit")
} else if sha != "" { } else if sha != "" {
checkDirty := false checkDirty := false
@ -73,7 +74,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
checkDirty = v checkDirty = v
} }
} }
if checkDirty && gitc.IsDirty() { if checkDirty && gitc.IsDirty(ctx) {
sha += "-dirty" sha += "-dirty"
} }
if setGitLabels { if setGitLabels {
@ -84,7 +85,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
} }
} }
if rurl, err := gitc.RemoteURL(); err == nil && rurl != "" { if rurl, err := gitc.RemoteURL(ctx); err == nil && rurl != "" {
if setGitLabels { if setGitLabels {
res["label:"+specs.AnnotationSource] = rurl res["label:"+specs.AnnotationSource] = rurl
} }
@ -94,7 +95,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
} }
if setGitLabels { if setGitLabels {
if root, err := gitc.RootDir(); err != nil { if root, err := gitc.WorkTree(ctx); err != nil {
return res, errors.Wrapf(err, "buildx: failed to get git root dir") return res, errors.Wrapf(err, "buildx: failed to get git root dir")
} else if root != "" { } else if root != "" {
if dockerfilePath == "" { if dockerfilePath == "" {

@ -6,6 +6,7 @@ import (
"github.com/containerd/continuity/fs/fstest" "github.com/containerd/continuity/fs/fstest"
"github.com/docker/buildx/util/gitutil" "github.com/docker/buildx/util/gitutil"
bkgitutil "github.com/moby/buildkit/util/gitutil"
"github.com/moby/buildkit/util/testutil/integration" "github.com/moby/buildkit/util/testutil/integration"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -66,7 +67,7 @@ EOT
) )
dirDest := t.TempDir() dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dir)) git, err := gitutil.New(bkgitutil.WithDir(dir))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(git, t) gitutil.GitInit(git, t)
@ -100,7 +101,7 @@ EOT
) )
dirDest := t.TempDir() dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec)) git, err := gitutil.New(bkgitutil.WithDir(dirSpec))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(git, t) gitutil.GitInit(git, t)
@ -134,14 +135,14 @@ EOT
) )
dirDest := t.TempDir() dirDest := t.TempDir()
gitSpec, err := gitutil.New(gitutil.WithWorkingDir(dirSpec)) gitSpec, err := gitutil.New(bkgitutil.WithDir(dirSpec))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(gitSpec, t) gitutil.GitInit(gitSpec, t)
gitutil.GitAdd(gitSpec, t, "docker-bake.hcl") gitutil.GitAdd(gitSpec, t, "docker-bake.hcl")
gitutil.GitCommit(gitSpec, t, "initial commit") gitutil.GitCommit(gitSpec, t, "initial commit")
addrSpec := gitutil.GitServeHTTP(gitSpec, t) addrSpec := gitutil.GitServeHTTP(gitSpec, t)
gitSrc, err := gitutil.New(gitutil.WithWorkingDir(dirSrc)) gitSrc, err := gitutil.New(bkgitutil.WithDir(dirSrc))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(gitSrc, t) gitutil.GitInit(gitSrc, t)
gitutil.GitAdd(gitSrc, t, "foo") gitutil.GitAdd(gitSrc, t, "foo")
@ -175,7 +176,7 @@ COPY super-cool.txt /
) )
dirDest := t.TempDir() dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dir)) git, err := gitutil.New(bkgitutil.WithDir(dir))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(git, t) gitutil.GitInit(git, t)
gitutil.GitAdd(git, t, "docker-bake.hcl", "bar") gitutil.GitAdd(git, t, "docker-bake.hcl", "bar")
@ -215,7 +216,7 @@ EOT
) )
dirDest := t.TempDir() dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec)) git, err := gitutil.New(bkgitutil.WithDir(dirSpec))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(git, t) gitutil.GitInit(git, t)
@ -262,7 +263,7 @@ EOT
) )
dirDest := t.TempDir() dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec)) git, err := gitutil.New(bkgitutil.WithDir(dirSpec))
require.NoError(t, err) require.NoError(t, err)
gitutil.GitInit(git, t) gitutil.GitInit(git, t)

@ -1,111 +1,73 @@
package gitutil package gitutil
import ( import (
"bytes"
"context" "context"
"net/url" "net/url"
"os"
"os/exec"
"path/filepath"
"strings" "strings"
bkgitutil "github.com/moby/buildkit/util/gitutil"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Git represents an active git object // GitCLI represents an active git object
type Git struct { type GitCLI struct {
ctx context.Context bkgitutil.GitCLI
wd string
gitpath string
}
// Option provides a variadic option for configuring the git client.
type Option func(b *Git)
// WithContext sets context.
func WithContext(ctx context.Context) Option {
return func(b *Git) {
b.ctx = ctx
}
}
// WithWorkingDir sets working directory.
func WithWorkingDir(wd string) Option {
return func(b *Git) {
b.wd = wd
}
} }
// New initializes a new git client // New initializes a new git client
func New(opts ...Option) (*Git, error) { func New(opts ...bkgitutil.Option) (*GitCLI, error) {
var err error cli, err := bkgitutil.NewGitCLI(opts...)
c := &Git{ if err != nil {
ctx: context.Background(), return nil, err
}
for _, opt := range opts {
opt(c)
} }
c.gitpath, err = gitPath(c.wd) gitpath, err := gitPath(cli.Dir())
if err != nil { if err != nil {
return nil, errors.New("git not found in PATH") return nil, errors.New("git not found in PATH")
} }
cli = cli.New(bkgitutil.WithGitBinary(gitpath))
return c, nil return &GitCLI{*cli}, nil
} }
func (c *Git) IsInsideWorkTree() bool { func (cli *GitCLI) IsInsideWorkTree(ctx context.Context) bool {
out, err := c.clean(c.run("rev-parse", "--is-inside-work-tree")) out, err := cli.clean(cli.Run(ctx, "rev-parse", "--is-inside-work-tree"))
return out == "true" && err == nil return out == "true" && err == nil
} }
func (c *Git) IsDirty() bool { func (cli *GitCLI) IsDirty(ctx context.Context) bool {
out, err := c.run("status", "--porcelain", "--ignored") out, err := cli.Run(ctx, "status", "--porcelain", "--ignored")
return strings.TrimSpace(out) != "" || err != nil return strings.TrimSpace(string(out)) != "" || err != nil
}
func (c *Git) RootDir() (string, error) {
return c.clean(c.run("rev-parse", "--show-toplevel"))
} }
func (c *Git) GitDir() (string, error) { func (cli *GitCLI) RemoteURL(ctx context.Context) (string, error) {
dir, err := c.RootDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".git"), nil
}
func (c *Git) RemoteURL() (string, error) {
// Try to get the remote URL from the origin remote first // Try to get the remote URL from the origin remote first
if ru, err := c.clean(c.run("remote", "get-url", "origin")); err == nil && ru != "" { if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "origin")); err == nil && ru != "" {
return stripCredentials(ru), nil return stripCredentials(ru), nil
} }
// If that fails, try to get the remote URL from the upstream remote // If that fails, try to get the remote URL from the upstream remote
if ru, err := c.clean(c.run("remote", "get-url", "upstream")); err == nil && ru != "" { if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "upstream")); err == nil && ru != "" {
return stripCredentials(ru), nil return stripCredentials(ru), nil
} }
return "", errors.New("no remote URL found for either origin or upstream") return "", errors.New("no remote URL found for either origin or upstream")
} }
func (c *Git) FullCommit() (string, error) { func (cli *GitCLI) FullCommit(ctx context.Context) (string, error) {
return c.clean(c.run("show", "--format=%H", "HEAD", "--quiet", "--")) return cli.clean(cli.Run(ctx, "show", "--format=%H", "HEAD", "--quiet", "--"))
} }
func (c *Git) ShortCommit() (string, error) { func (cli *GitCLI) ShortCommit(ctx context.Context) (string, error) {
return c.clean(c.run("show", "--format=%h", "HEAD", "--quiet", "--")) return cli.clean(cli.Run(ctx, "show", "--format=%h", "HEAD", "--quiet", "--"))
} }
func (c *Git) Tag() (string, error) { func (cli *GitCLI) Tag(ctx context.Context) (string, error) {
var tag string var tag string
var err error var err error
for _, fn := range []func() (string, error){ for _, fn := range []func() (string, error){
func() (string, error) { func() (string, error) {
return c.clean(c.run("tag", "--points-at", "HEAD", "--sort", "-version:creatordate")) return cli.clean(cli.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate"))
}, },
func() (string, error) { func() (string, error) {
return c.clean(c.run("describe", "--tags", "--abbrev=0")) return cli.clean(cli.Run(ctx, "describe", "--tags", "--abbrev=0"))
}, },
} { } {
tag, err = fn() tag, err = fn()
@ -116,32 +78,8 @@ func (c *Git) Tag() (string, error) {
return tag, err return tag, err
} }
func (c *Git) run(args ...string) (string, error) { func (cli *GitCLI) clean(dt []byte, err error) (string, error) {
var extraArgs = []string{ out := string(dt)
"-c", "log.showSignature=false",
}
args = append(extraArgs, args...)
cmd := exec.CommandContext(c.ctx, c.gitpath, args...)
if c.wd != "" {
cmd.Dir = c.wd
}
// Override the locale to ensure consistent output
cmd.Env = append(os.Environ(), "LC_ALL=C")
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", errors.New(stderr.String())
}
return stdout.String(), nil
}
func (c *Git) clean(out string, err error) (string, error) {
out = strings.ReplaceAll(strings.Split(out, "\n")[0], "'", "") out = strings.ReplaceAll(strings.Split(out, "\n")[0], "'", "")
if err != nil { if err != nil {
err = errors.New(strings.TrimSuffix(err.Error(), "\n")) err = errors.New(strings.TrimSuffix(err.Error(), "\n"))

@ -1,26 +1,29 @@
package gitutil package gitutil
import ( import (
"context"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestGit(t *testing.T) { func TestGit(t *testing.T) {
ctx := context.TODO()
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
out, err := c.run("status") out, err := c.Run(ctx, "status")
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, out) require.NotEmpty(t, out)
out, err = c.clean(c.run("not-exist")) out2, err := c.clean(c.Run(ctx, "not-exist"))
require.Error(t, err) require.Error(t, err)
require.Empty(t, out) require.Empty(t, out2)
require.Equal(t, "git: 'not-exist' is not a git command. See 'git --help'.", err.Error()) require.Contains(t, err.Error(), "git: 'not-exist' is not a git command. See 'git --help'.")
} }
func TestGitFullCommit(t *testing.T) { func TestGitFullCommit(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
@ -28,12 +31,13 @@ func TestGitFullCommit(t *testing.T) {
GitInit(c, t) GitInit(c, t)
GitCommit(c, t, "bar") GitCommit(c, t, "bar")
out, err := c.FullCommit() out, err := c.FullCommit(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 40, len(out)) require.Equal(t, 40, len(out))
} }
func TestGitShortCommit(t *testing.T) { func TestGitShortCommit(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
@ -41,38 +45,41 @@ func TestGitShortCommit(t *testing.T) {
GitInit(c, t) GitInit(c, t)
GitCommit(c, t, "bar") GitCommit(c, t, "bar")
out, err := c.ShortCommit() out, err := c.ShortCommit(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 7, len(out)) require.Equal(t, 7, len(out))
} }
func TestGitFullCommitErr(t *testing.T) { func TestGitFullCommitErr(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
GitInit(c, t) GitInit(c, t)
_, err = c.FullCommit() _, err = c.FullCommit(ctx)
require.Error(t, err) require.Error(t, err)
require.True(t, IsUnknownRevision(err)) require.True(t, IsUnknownRevision(err))
require.False(t, IsAmbiguousArgument(err)) require.False(t, IsAmbiguousArgument(err))
} }
func TestGitShortCommitErr(t *testing.T) { func TestGitShortCommitErr(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
GitInit(c, t) GitInit(c, t)
_, err = c.ShortCommit() _, err = c.ShortCommit(ctx)
require.Error(t, err) require.Error(t, err)
require.True(t, IsUnknownRevision(err)) require.True(t, IsUnknownRevision(err))
require.False(t, IsAmbiguousArgument(err)) require.False(t, IsAmbiguousArgument(err))
} }
func TestGitTagsPointsAt(t *testing.T) { func TestGitTagsPointsAt(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
@ -83,12 +90,13 @@ func TestGitTagsPointsAt(t *testing.T) {
GitCommit(c, t, "foo") GitCommit(c, t, "foo")
GitTag(c, t, "v0.9.0") GitTag(c, t, "v0.9.0")
out, err := c.clean(c.run("tag", "--points-at", "HEAD", "--sort", "-version:creatordate")) out, err := c.clean(c.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate"))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "v0.9.0", out) require.Equal(t, "v0.9.0", out)
} }
func TestGitDescribeTags(t *testing.T) { func TestGitDescribeTags(t *testing.T) {
ctx := context.TODO()
Mktmp(t) Mktmp(t)
c, err := New() c, err := New()
require.NoError(t, err) require.NoError(t, err)
@ -99,12 +107,14 @@ func TestGitDescribeTags(t *testing.T) {
GitCommit(c, t, "foo") GitCommit(c, t, "foo")
GitTag(c, t, "v0.9.0") GitTag(c, t, "v0.9.0")
out, err := c.clean(c.run("describe", "--tags", "--abbrev=0")) out, err := c.clean(c.Run(ctx, "describe", "--tags", "--abbrev=0"))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "v0.9.0", out) require.Equal(t, "v0.9.0", out)
} }
func TestGitRemoteURL(t *testing.T) { func TestGitRemoteURL(t *testing.T) {
ctx := context.TODO()
type remote struct { type remote struct {
name string name string
url string url string
@ -179,7 +189,7 @@ func TestGitRemoteURL(t *testing.T) {
GitSetRemote(c, t, r.name, r.url) GitSetRemote(c, t, r.name, r.url)
} }
ru, err := c.RemoteURL() ru, err := c.RemoteURL(ctx)
if tt.fail { if tt.fail {
require.Error(t, err) require.Error(t, err)
return return

@ -1,6 +1,7 @@
package gitutil package gitutil
import ( import (
"context"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -8,7 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func GitInit(c *Git, tb testing.TB) { func GitInit(c *GitCLI, tb testing.TB) {
tb.Helper() tb.Helper()
out, err := fakeGit(c, "init") out, err := fakeGit(c, "init")
require.NoError(tb, err) require.NoError(tb, err)
@ -18,35 +19,35 @@ func GitInit(c *Git, tb testing.TB) {
_, _ = fakeGit(c, "branch", "-D", "master") _, _ = fakeGit(c, "branch", "-D", "master")
} }
func GitCommit(c *Git, tb testing.TB, msg string) { func GitCommit(c *GitCLI, tb testing.TB, msg string) {
tb.Helper() tb.Helper()
out, err := fakeGit(c, "commit", "--allow-empty", "-m", msg) out, err := fakeGit(c, "commit", "--allow-empty", "-m", msg)
require.NoError(tb, err) require.NoError(tb, err)
require.Contains(tb, out, "main", msg) require.Contains(tb, out, "main", msg)
} }
func GitTag(c *Git, tb testing.TB, tag string) { func GitTag(c *GitCLI, tb testing.TB, tag string) {
tb.Helper() tb.Helper()
out, err := fakeGit(c, "tag", tag) out, err := fakeGit(c, "tag", tag)
require.NoError(tb, err) require.NoError(tb, err)
require.Empty(tb, out) require.Empty(tb, out)
} }
func GitCheckoutBranch(c *Git, tb testing.TB, name string) { func GitCheckoutBranch(c *GitCLI, tb testing.TB, name string) {
tb.Helper() tb.Helper()
out, err := fakeGit(c, "checkout", "-b", name) out, err := fakeGit(c, "checkout", "-b", name)
require.NoError(tb, err) require.NoError(tb, err)
require.Empty(tb, out) require.Empty(tb, out)
} }
func GitAdd(c *Git, tb testing.TB, files ...string) { func GitAdd(c *GitCLI, tb testing.TB, files ...string) {
tb.Helper() tb.Helper()
args := append([]string{"add"}, files...) args := append([]string{"add"}, files...)
_, err := fakeGit(c, args...) _, err := fakeGit(c, args...)
require.NoError(tb, err) require.NoError(tb, err)
} }
func GitSetRemote(c *Git, tb testing.TB, name string, url string) { func GitSetRemote(c *GitCLI, tb testing.TB, name string, url string) {
tb.Helper() tb.Helper()
_, err := fakeGit(c, "remote", "add", name, url) _, err := fakeGit(c, "remote", "add", name, url)
require.NoError(tb, err) require.NoError(tb, err)
@ -64,7 +65,7 @@ func Mktmp(tb testing.TB) string {
return folder return folder
} }
func fakeGit(c *Git, args ...string) (string, error) { func fakeGit(c *GitCLI, args ...string) (string, error) {
allArgs := []string{ allArgs := []string{
"-c", "user.name=buildx", "-c", "user.name=buildx",
"-c", "user.email=buildx@docker.com", "-c", "user.email=buildx@docker.com",
@ -73,7 +74,7 @@ func fakeGit(c *Git, args ...string) (string, error) {
"-c", "log.showSignature=false", "-c", "log.showSignature=false",
} }
allArgs = append(allArgs, args...) allArgs = append(allArgs, args...)
return c.clean(c.run(allArgs...)) return c.clean(c.Run(context.TODO(), allArgs...))
} }
func IsAmbiguousArgument(err error) bool { func IsAmbiguousArgument(err error) bool {

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func GitServeHTTP(c *Git, t testing.TB) (url string) { func GitServeHTTP(c *GitCLI, t testing.TB) (url string) {
t.Helper() t.Helper()
gitUpdateServerInfo(c, t) gitUpdateServerInfo(c, t)
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(context.TODO())
@ -19,7 +19,7 @@ func GitServeHTTP(c *Git, t testing.TB) (url string) {
done := make(chan struct{}) done := make(chan struct{})
name := "test.git" name := "test.git"
dir, err := c.GitDir() dir, err := c.GitDir(context.TODO())
if err != nil { if err != nil {
cancel() cancel()
} }
@ -55,7 +55,7 @@ func GitServeHTTP(c *Git, t testing.TB) (url string) {
return fmt.Sprintf("http://%s/%s", addr, name) return fmt.Sprintf("http://%s/%s", addr, name)
} }
func gitUpdateServerInfo(c *Git, tb testing.TB) { func gitUpdateServerInfo(c *GitCLI, tb testing.TB) {
tb.Helper() tb.Helper()
_, err := fakeGit(c, "update-server-info") _, err := fakeGit(c, "update-server-info")
require.NoError(tb, err) require.NoError(tb, err)

Loading…
Cancel
Save