From f8447d305cf5c014dd53d41412de7fc4fd78aec6 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 15 May 2025 18:15:29 +0900 Subject: [PATCH] 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 --- source/git/identifier.go | 6 ++--- util/gitutil/git_ref.go | 4 +-- util/gitutil/git_url.go | 46 +++++++++++++++++----------------- util/gitutil/git_url_test.go | 48 ++++++++++++++++++------------------ 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/source/git/identifier.go b/source/git/identifier.go index ac2b0dbe6..5cfe39e6b 100644 --- a/source/git/identifier.go +++ b/source/git/identifier.go @@ -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 = "" diff --git a/util/gitutil/git_ref.go b/util/gitutil/git_ref.go index 59d658264..cb562acf5 100644 --- a/util/gitutil/git_ref.go +++ b/util/gitutil/git_ref.go @@ -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, "/") diff --git a/util/gitutil/git_url.go b/util/gitutil/git_url.go index 0f1ff505c..338893b27 100644 --- a/util/gitutil/git_url.go +++ b/util/gitutil/git_url.go @@ -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(), } } diff --git a/util/gitutil/git_url_test.go b/util/gitutil/git_url_test.go index 63ee90aa1..3306b06f7 100644 --- a/util/gitutil/git_url_test.go +++ b/util/gitutil/git_url_test.go @@ -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()) } })