GitLabgitlab-org
cli
main
1package glinstance
3import (
4 "errors"
5 "fmt"
6 "path"
7 "strings"
8)
10const (
11 DefaultHostname = "gitlab.com"
12 DefaultProtocol = "https"
13 DefaultClientID = "41d48f9422ebd655dd9cf2947d6979681dfaddc6d0c56f7628f6ada59559af1e"
14)
16// IsSelfHosted reports whether a non-normalized host name looks like a GitLab Self-Managed instance
17// staging.gitlab.com is considered self-managed
18func IsSelfHosted(h string) bool {
19 return NormalizeHostname(h) != DefaultHostname
20}
22// NormalizeHostname returns the canonical host name of a GitLab instance
23// Note: GitLab does not allow subdomains on gitlab.com https://gitlab.com/gitlab-org/gitlab/-/issues/26703
24func NormalizeHostname(h string) string {
25 return strings.ToLower(h)
26}
28// StripHostProtocol strips the url protocol and returns the hostname and the protocol
29func StripHostProtocol(h string) (string, string) {
30 hostname := NormalizeHostname(h)
31 var protocol string
32 if strings.HasPrefix(hostname, "http://") {
33 protocol = "http"
34 } else {
35 protocol = "https"
36 }
37 hostname = strings.TrimPrefix(hostname, protocol)
38 hostname = strings.Trim(hostname, ":/")
39 return hostname, protocol
40}
42// ExtractSubfolder splits a hostname string into hostname and optional subfolder.
43// Trims slashes from the subfolder result.
44// Example: "example.com/gitlab/" → ("example.com", "gitlab")
45func ExtractSubfolder(input string) (string, string) {
46 parts := strings.SplitN(input, "/", 2)
47 hostname := parts[0]
48 subfolder := ""
49 if len(parts) == 2 {
50 subfolder = strings.Trim(parts[1], "/")
51 }
52 return hostname, subfolder
53}
55// buildURLPath constructs the URL path with optional subfolder.
56func buildURLPath(hostname, subfolder string) string {
57 if subfolder == "" {
58 return hostname
59 }
60 return path.Join(hostname, subfolder)
61}
63// resolveHostAndSubfolder resolves the effective hostname and subfolder from the provided parameters.
64// Handles backward compatibility with api_host containing paths.
65// Returns (baseHost, effectiveSubfolder).
66func resolveHostAndSubfolder(hostname, apiHost, subfolder string) (string, string) {
67 baseHost := hostname
68 effectiveSubfolder := subfolder
70 // Backward compatibility: extract subfolder from api_host
71 if effectiveSubfolder == "" && apiHost != "" {
72 extractedHost, extractedSubfolder := ExtractSubfolder(apiHost)
73 baseHost = extractedHost
74 effectiveSubfolder = extractedSubfolder
75 } else if apiHost != "" && !strings.Contains(apiHost, "/") {
76 // api_host is alternate hostname without subfolder
77 baseHost = apiHost
78 }
80 return baseHost, effectiveSubfolder
81}
83// APIEndpoint returns the REST API endpoint prefix for a GitLab instance.
84//
85// Parameters:
86//
87// hostname: Base hostname (e.g., "example.com")
88// protocol: "http" or "https"
89// apiHost: (Deprecated) Alternate API hostname, may contain path for backward compat
90// subfolder: Subfolder path (e.g., "gitlab" for https://example.com/gitlab/)
91//
92// Precedence: subfolder parameter > path in apiHost > none
93func APIEndpoint(hostname, protocol, apiHost, subfolder string) string {
94 if protocol == "" {
95 protocol = DefaultProtocol
96 }
98 baseHost, effectiveSubfolder := resolveHostAndSubfolder(hostname, apiHost, subfolder)
99 urlPath := buildURLPath(baseHost, effectiveSubfolder)
101 if IsSelfHosted(baseHost) {
102 return fmt.Sprintf("%s://%s/api/v4/", protocol, urlPath)
103 }
104 return "https://gitlab.com/api/v4/"
105}
107// GraphQLEndpoint returns the GraphQL API endpoint prefix for a GitLab instance.
108// Parameters match APIEndpoint for consistency.
109func GraphQLEndpoint(hostname, protocol, apiHost, subfolder string) string {
110 if protocol == "" {
111 protocol = "https"
112 }
114 baseHost, effectiveSubfolder := resolveHostAndSubfolder(hostname, apiHost, subfolder)
115 urlPath := buildURLPath(baseHost, effectiveSubfolder)
117 if IsSelfHosted(baseHost) {
118 return fmt.Sprintf("%s://%s/api/graphql/", protocol, urlPath)
119 }
120 return "https://gitlab.com/api/graphql/"
121}
123// AuthEndpoint returns the base URL used by GitLab authentication endpoints.
124func AuthEndpoint(hostname, protocol, subfolder string) string {
125 if protocol == "" {
126 protocol = DefaultProtocol
127 }
129 return fmt.Sprintf("%s://%s", protocol, buildURLPath(hostname, strings.Trim(subfolder, "/")))
130}
132func HostnameValidator(v any) error {
133 hostname, valid := v.(string)
134 if !valid {
135 return errors.New("hostname is not a string")
136 }
138 if len(strings.TrimSpace(hostname)) < 1 {
139 return errors.New("a value is required")
140 }
141 if strings.ContainsRune(hostname, '/') || strings.ContainsRune(hostname, ':') {
142 return errors.New("invalid hostname")
143 }
144 return nil
145}
Diagnostics6 git calls · 306 ms
CallStatusSizeTime0306 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · hit8 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=8 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 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 · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=6 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=44Open pack map
fetchtree:0200134 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=be5e976e967e5e96675c5ec40a6a21baa01a7554 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=43619d0d925e615aae4d175fe8dbb5b1616686c7Open pack map
fetch2001.5 KiB290 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=290 git.response.bytes=1554 git.response.cache=MISS git.response.duration_ms=290 git.fetch.wants=c4a1d923829e1f522ccdc30681c2f40074dc6f0e 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=1554 git.pkt.payload.max=1527 git.sideband.band1.bytes=1527 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
- a5e28fadf94824d3
- Request
- 38a6ac78…
- 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