gitutil: use BuildKit urlutil.RedactCredentials for remote URLs

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2026-04-09 15:04:37 +02:00
parent 720f91fdd4
commit 4f89a2407a
4 changed files with 38 additions and 61 deletions
-44
View File
@@ -1,44 +0,0 @@
package gitutil
import "testing"
func TestStripCredentials(t *testing.T) {
cases := []struct {
name string
url string
want string
}{
{
name: "non-blank Password",
url: "https://user:password@host.tld/this:that",
want: "https://host.tld/this:that",
},
{
name: "blank Password",
url: "https://user@host.tld/this:that",
want: "https://host.tld/this:that",
},
{
name: "blank Username",
url: "https://:password@host.tld/this:that",
want: "https://host.tld/this:that",
},
{
name: "blank Username, blank Password",
url: "https://host.tld/this:that",
want: "https://host.tld/this:that",
},
{
name: "invalid URL",
url: "1https://foo.com",
want: "1https://foo.com",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if g, w := stripCredentials(tt.url), tt.want; g != w {
t.Fatalf("got: %q\nwant: %q", g, w)
}
})
}
}
+4 -17
View File
@@ -2,12 +2,12 @@ package gitutil
import (
"context"
"net/url"
"path/filepath"
"strings"
"github.com/docker/buildx/util/osutil"
bkgitutil "github.com/moby/buildkit/util/gitutil"
bkurlutil "github.com/moby/buildkit/util/urlutil"
"github.com/pkg/errors"
)
@@ -62,16 +62,16 @@ func (cli *GitCLI) RemoteURL(ctx context.Context) (string, error) {
// Try default remote based on remote tracking branch.
if remote, err := cli.currentRemote(ctx); err == nil && remote != "" {
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", remote)); err == nil && ru != "" {
return stripCredentials(ru), nil
return bkurlutil.RedactCredentials(ru), nil
}
}
// Next try to get the remote URL from the origin remote first.
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "origin")); err == nil && ru != "" {
return stripCredentials(ru), nil
return bkurlutil.RedactCredentials(ru), nil
}
// If that fails, try to get the remote URL from the upstream remote.
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "upstream")); err == nil && ru != "" {
return stripCredentials(ru), nil
return bkurlutil.RedactCredentials(ru), nil
}
return "", errors.New("no remote URL found for either origin or upstream")
}
@@ -135,16 +135,3 @@ func IsUnknownRevision(err error) bool {
errMsg := strings.ToLower(err.Error())
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
}
// stripCredentials takes a URL and strips username and password from it.
// e.g. "https://user:password@host.tld/path.git" will be changed to
// "https://host.tld/path.git".
// TODO: remove this function once fix from BuildKit is vendored here
func stripCredentials(s string) string {
ru, err := url.Parse(s)
if err != nil {
return s // string is not a URL, just return it
}
ru.User = nil
return ru.String()
}