Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
return res, nil
}

if sha, err := gitc.FullCommit(); err != nil {
if sha, err := gitc.FullCommit(); err != nil && !gitutil.IsUnknownRevision(err) {
return res, errors.Wrapf(err, "buildx: failed to get git commit")
} else if sha != "" {
if gitc.IsDirty() {
Expand Down
13 changes: 13 additions & 0 deletions util/gitutil/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gitutil
import (
"bytes"
"context"
"os"
"os/exec"
"strings"

Expand Down Expand Up @@ -116,6 +117,9 @@ func (c *Git) run(args ...string) (string, error) {
cmd.Dir = c.wd
}

// Override the locale to ensure consistent output
cmd.Env = append(os.Environ(), "LC_ALL=C")
Comment on lines +120 to +121
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I facepalm myself for not asking; did someone look (or do we need a tracking ticket) on the BuildKit side if we should also set this option there? (I think BuildKit also shells out to git - it may be in a more predictable environment, but possibly could still be a good thing to do)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful on BuildKit too so we have a consistent output when users report issues.


stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
cmd.Stdout = &stdout
Expand All @@ -134,3 +138,12 @@ func (c *Git) clean(out string, err error) (string, error) {
}
return out, err
}

func IsUnknownRevision(err error) bool {
if err == nil {
return false
}
// https://github.com/git/git/blob/a6a323b31e2bcbac2518bddec71ea7ad558870eb/setup.c#L204
errMsg := strings.ToLower(err.Error())
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
}
12 changes: 12 additions & 0 deletions util/gitutil/gitutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func TestGitShortCommit(t *testing.T) {
require.Equal(t, 7, len(out))
}

func TestGitFullCommitErr(t *testing.T) {
Mktmp(t)
c, err := New()
require.NoError(t, err)

GitInit(c, t)

_, err = c.FullCommit()
require.Error(t, err)
require.True(t, IsUnknownRevision(err))
}

func TestGitTagsPointsAt(t *testing.T) {
Mktmp(t)
c, err := New()
Expand Down