GitLabgitlab-org

cli

main
ply6 git calls33 ms97 KiB5 cachedprotocol v2
93 lines2.6 KiBRaw
1package git
3import (
4 "fmt"
5 "net/url"
6 "regexp"
7 "slices"
8 "strings"
9 "unicode"
12func isSupportedProtocol(u string) bool {
13 // URL schemes are case-insensitive (RFC 3986), so compare in lower case.
14 // Otherwise an uppercase scheme like "HTTPS://" is not recognized and gets
15 // misparsed as scp-style SSH further down.
16 u = strings.ToLower(u)
17 return strings.HasPrefix(u, "ssh:") ||
18 strings.HasPrefix(u, "git+ssh:") ||
19 strings.HasPrefix(u, "git:") ||
20 strings.HasPrefix(u, "http:") ||
21 strings.HasPrefix(u, "https:")
24func isPossibleProtocol(u string) bool {
25 lower := strings.ToLower(u)
26 return isSupportedProtocol(lower) ||
27 strings.HasPrefix(lower, "ftp:") ||
28 strings.HasPrefix(lower, "ftps:") ||
29 strings.HasPrefix(lower, "file:")
32// ParseURL normalizes git remote urls
33func ParseURL(rawURL string) (*url.URL, error) {
34 if !isPossibleProtocol(rawURL) &&
35 strings.ContainsRune(rawURL, ':') &&
36 // not a Windows path
37 !strings.ContainsRune(rawURL, '\\') {
38 // support scp-like syntax for ssh protocol
39 rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1)
40 }
42 u, err := url.Parse(rawURL)
43 if err != nil {
44 return nil, err
45 }
47 if u.Scheme == "git+ssh" {
48 u.Scheme = "ssh"
49 }
51 if u.Scheme != "ssh" {
52 return u, nil
53 }
55 if strings.HasPrefix(u.Path, "//") {
56 u.Path = strings.TrimPrefix(u.Path, "/")
57 }
59 if idx := strings.Index(u.Host, ":"); idx >= 0 {
60 u.Host = u.Host[0:idx]
61 }
63 return u, nil
66var remoteSchemes = []string{"ssh", "git+ssh", "git", "http", "https"}
68// scpLikeURL matches [user@]host:path and [user@host]:path (IPv6). The path
69// must not start with ":", which would make it git's <transport>::<address>
70// remote-helper syntax.
71var scpLikeURL = regexp.MustCompile(`^(?:\w[\w.-]*@)?(?:\w[\w.-]*|\[(?:\w[\w.-]*@)?[0-9A-Fa-f:.]+\]):[^:\s]`)
73// ValidateRemoteURL reports whether u, taken from an API response, is safe to
74// hand to git as a remote: a supported URL scheme or scp-like SSH syntax.
75// It rejects values git would read as an option or a remote-helper transport.
76func ValidateRemoteURL(u string) error {
77 if strings.ContainsFunc(u, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }) {
78 return fmt.Errorf("invalid git remote URL %q", u)
79 }
80 scheme, _, hasAuthority := strings.Cut(u, "://")
81 if hasAuthority && slices.Contains(remoteSchemes, strings.ToLower(scheme)) {
82 return nil
83 }
84 if scpLikeURL.MatchString(u) && !hasAuthority {
85 return nil
86 }
87 return fmt.Errorf("invalid git remote URL %q", u)
90// IsValidUrl tests a string to determine if it is a valid Git url or not.
91func IsValidURL(u string) bool {
92 return strings.HasPrefix(u, "git@") || isSupportedProtocol(u)
Diagnostics6 git calls · 33 ms
CallStatusSizeTime033 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit10 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=10
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:0200510 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=510
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=a3147172443266d011121808a125248c5c915972
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=510
git.pkt.payload.max=483
git.sideband.band1.bytes=483
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=483
git.pack.trailer=4ef234d86a93490a6a3da3549aa13a474a5c9eac
Open pack map
fetch2001.2 KiB · hit19 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=19
git.response.bytes=1219
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.wants=3b1342e4ee55df6f3aa4a54d2150b3bdde392fce
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1219
git.pkt.payload.max=1192
git.sideband.band1.bytes=1192
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e15c584dd3cf6a
Request
4510a1c8…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format