Skip to content

Commit 7c8ee7d

Browse files
committed
util/gitutil: add ParseURLBasic
ParseURLBasic is a simplified variant of ParseURL. Basically, the full ParseURL should be used only in the LLB frontends such as `dockerfile`. Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent dc9e4c2 commit 7c8ee7d

2 files changed

Lines changed: 165 additions & 103 deletions

File tree

util/gitutil/git_url.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ var supportedProtos = map[string]struct{}{
3030

3131
var protoRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+://`)
3232

33+
// GitURLBase is a simplified representation of GitURL.
34+
type GitURLBase struct {
35+
// Scheme is the protocol over which the git repo can be accessed
36+
Scheme string
37+
38+
// Host is the remote host that hosts the git repo
39+
Host string
40+
41+
// Path is the path on the host to access the repo
42+
Path string
43+
44+
// Remote is a valid URL remote to pass into the Git CLI tooling (i.e.
45+
// without the fragment metadata)
46+
Remote string
47+
}
48+
3349
// URL is a custom URL type that points to a remote Git repository.
3450
//
3551
// URLs can be parsed from both standard URLs (e.g.
@@ -38,21 +54,11 @@ var protoRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+://`)
3854
//
3955
// See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols
4056
type GitURL struct {
41-
// Scheme is the protocol over which the git repo can be accessed
42-
Scheme string
43-
44-
// Host is the remote host that hosts the git repo
45-
Host string
46-
// Path is the path on the host to access the repo
47-
Path string
57+
GitURLBase
4858
// User is the username/password to access the host
4959
User *url.Userinfo
5060
// Opts can contain additional metadata
5161
Opts *GitURLOpts
52-
53-
// Remote is a valid URL remote to pass into the Git CLI tooling (i.e.
54-
// without the fragment metadata)
55-
Remote string
5662
}
5763

5864
// GitURLOpts is the buildkit-specific metadata extracted from the fragment
@@ -159,6 +165,18 @@ func ParseURL(remote string) (*GitURL, error) {
159165
return nil, ErrUnknownProtocol
160166
}
161167

168+
// ParseURLBasic is a simplified version of ParseURL that only returns the basic components.
169+
// When hasOpts is true, there are optional information in the URL fragment or query.
170+
// Those optional information are omitted in this function. Use [ParseURL] to obtain them.
171+
func ParseURLBasic(remote string) (parsedBasic *GitURLBase, hasOpts bool, err error) {
172+
parsed, err := ParseURL(remote)
173+
if parsed != nil {
174+
parsedBasic = &parsed.GitURLBase
175+
hasOpts = parsed.Opts != nil
176+
}
177+
return
178+
}
179+
162180
func IsGitTransport(remote string) bool {
163181
if proto := protoRegexp.FindString(remote); proto != "" {
164182
proto = strings.ToLower(strings.TrimSuffix(proto, "://"))
@@ -179,12 +197,14 @@ func FromURL(url *url.URL) (*GitURL, error) {
179197
}
180198
}
181199
return &GitURL{
182-
Scheme: url.Scheme,
183-
User: url.User,
184-
Host: url.Host,
185-
Path: url.Path,
186-
Opts: opts,
187-
Remote: withoutOpts.String(),
200+
GitURLBase: GitURLBase{
201+
Scheme: url.Scheme,
202+
Host: url.Host,
203+
Path: url.Path,
204+
Remote: withoutOpts.String(),
205+
},
206+
User: url.User,
207+
Opts: opts,
188208
}, nil
189209
}
190210

@@ -200,11 +220,13 @@ func fromSCPStyleURL(url *sshutil.SCPStyleURL) (*GitURL, error) {
200220
}
201221
}
202222
return &GitURL{
203-
Scheme: SSHProtocol,
204-
User: url.User,
205-
Host: url.Host,
206-
Path: url.Path,
207-
Opts: opts,
208-
Remote: withoutOpts.String(),
223+
GitURLBase: GitURLBase{
224+
Scheme: SSHProtocol,
225+
Host: url.Host,
226+
Path: url.Path,
227+
Remote: withoutOpts.String(),
228+
},
229+
User: url.User,
230+
Opts: opts,
209231
}, nil
210232
}

0 commit comments

Comments
 (0)