git url: rename GitURLFragment to GitURLOpts

No substantial code change.
Non-fragment data can be added in this structure too.

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda
2025-05-15 18:15:29 +09:00
parent 6e34e07e61
commit f8447d305c
4 changed files with 52 additions and 52 deletions

View File

@@ -32,9 +32,9 @@ func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
}
repo := GitIdentifier{Remote: u.Remote}
if u.Fragment != nil {
repo.Ref = u.Fragment.Ref
repo.Subdir = u.Fragment.Subdir
if u.Opts != nil {
repo.Ref = u.Opts.Ref
repo.Subdir = u.Opts.Subdir
}
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {
repo.Subdir = ""

View File

@@ -93,8 +93,8 @@ func ParseGitRef(ref string) (*GitRef, error) {
if res.IndistinguishableFromLocal {
_, res.Remote, _ = strings.Cut(res.Remote, "://")
}
if remote.Fragment != nil {
res.Commit, res.SubDir = remote.Fragment.Ref, remote.Fragment.Subdir
if remote.Opts != nil {
res.Commit, res.SubDir = remote.Opts.Ref, remote.Opts.Subdir
}
repoSplitBySlash := strings.Split(res.Remote, "/")

View File

@@ -47,31 +47,31 @@ type GitURL struct {
Path string
// User is the username/password to access the host
User *url.Userinfo
// Fragment can contain additional metadata
Fragment *GitURLFragment
// Opts can contain additional metadata
Opts *GitURLOpts
// Remote is a valid URL remote to pass into the Git CLI tooling (i.e.
// without the fragment metadata)
Remote string
}
// GitURLFragment is the buildkit-specific metadata extracted from the fragment
// GitURLOpts is the buildkit-specific metadata extracted from the fragment
// of a remote URL.
type GitURLFragment struct {
type GitURLOpts struct {
// Ref is the git reference
Ref string
// Subdir is the sub-directory inside the git repository to use
Subdir string
}
// splitGitFragment splits a git URL fragment into its respective git
// parseOpts splits a git URL fragment into its respective git
// reference and subdirectory components.
func splitGitFragment(fragment string) *GitURLFragment {
func parseOpts(fragment string) *GitURLOpts {
if fragment == "" {
return nil
}
ref, subdir, _ := strings.Cut(fragment, ":")
return &GitURLFragment{Ref: ref, Subdir: subdir}
return &GitURLOpts{Ref: ref, Subdir: subdir}
}
// ParseURL parses a BuildKit-style Git URL (that may contain additional
@@ -106,27 +106,27 @@ func IsGitTransport(remote string) bool {
}
func fromURL(url *url.URL) *GitURL {
withoutFragment := *url
withoutFragment.Fragment = ""
withoutOpts := *url
withoutOpts.Fragment = ""
return &GitURL{
Scheme: url.Scheme,
User: url.User,
Host: url.Host,
Path: url.Path,
Fragment: splitGitFragment(url.Fragment),
Remote: withoutFragment.String(),
Scheme: url.Scheme,
User: url.User,
Host: url.Host,
Path: url.Path,
Opts: parseOpts(url.Fragment),
Remote: withoutOpts.String(),
}
}
func fromSCPStyleURL(url *sshutil.SCPStyleURL) *GitURL {
withoutFragment := *url
withoutFragment.Fragment = ""
withoutOpts := *url
withoutOpts.Fragment = ""
return &GitURL{
Scheme: SSHProtocol,
User: url.User,
Host: url.Host,
Path: url.Path,
Fragment: splitGitFragment(url.Fragment),
Remote: withoutFragment.String(),
Scheme: SSHProtocol,
User: url.User,
Host: url.Host,
Path: url.Path,
Opts: parseOpts(url.Fragment),
Remote: withoutOpts.String(),
}
}

View File

@@ -32,29 +32,29 @@ func TestParseURL(t *testing.T) {
{
url: "http://github.com/moby/buildkit#v1.0.0",
result: GitURL{
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Fragment: &GitURLFragment{Ref: "v1.0.0"},
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Opts: &GitURLOpts{Ref: "v1.0.0"},
},
},
{
url: "http://github.com/moby/buildkit#v1.0.0:subdir",
result: GitURL{
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Fragment: &GitURLFragment{Ref: "v1.0.0", Subdir: "subdir"},
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Opts: &GitURLOpts{Ref: "v1.0.0", Subdir: "subdir"},
},
},
{
url: "http://foo:bar@github.com/moby/buildkit#v1.0.0",
result: GitURL{
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Fragment: &GitURLFragment{Ref: "v1.0.0"},
User: url.UserPassword("foo", "bar"),
Scheme: HTTPProtocol,
Host: "github.com",
Path: "/moby/buildkit",
Opts: &GitURLOpts{Ref: "v1.0.0"},
User: url.UserPassword("foo", "bar"),
},
},
{
@@ -87,21 +87,21 @@ func TestParseURL(t *testing.T) {
{
url: "git@github.com:moby/buildkit.git#v1.0.0",
result: GitURL{
Scheme: SSHProtocol,
Host: "github.com",
Path: "moby/buildkit.git",
Fragment: &GitURLFragment{Ref: "v1.0.0"},
User: url.User("git"),
Scheme: SSHProtocol,
Host: "github.com",
Path: "moby/buildkit.git",
Opts: &GitURLOpts{Ref: "v1.0.0"},
User: url.User("git"),
},
},
{
url: "git@github.com:moby/buildkit.git#v1.0.0:hack",
result: GitURL{
Scheme: SSHProtocol,
Host: "github.com",
Path: "moby/buildkit.git",
Fragment: &GitURLFragment{Ref: "v1.0.0", Subdir: "hack"},
User: url.User("git"),
Scheme: SSHProtocol,
Host: "github.com",
Path: "moby/buildkit.git",
Opts: &GitURLOpts{Ref: "v1.0.0", Subdir: "hack"},
User: url.User("git"),
},
},
{
@@ -162,7 +162,7 @@ func TestParseURL(t *testing.T) {
require.Equal(t, test.result.Scheme, remote.Scheme)
require.Equal(t, test.result.Host, remote.Host)
require.Equal(t, test.result.Path, remote.Path)
require.Equal(t, test.result.Fragment, remote.Fragment)
require.Equal(t, test.result.Opts, remote.Opts)
require.Equal(t, test.result.User.String(), remote.User.String())
}
})