GitLabgitlab-org

cli

main
ply6 git calls35 ms100 KiB5 cachedprotocol v2
481 lines13 KiBRaw
1package utils
3import (
4 "fmt"
5 "net/url"
6 "os"
7 "path/filepath"
8 "regexp"
9 "slices"
10 "strconv"
11 "strings"
12 "time"
14 "charm.land/glamour/v2"
15 "github.com/google/shlex"
16 "github.com/yuin/goldmark/v2/ast"
17 "github.com/yuin/goldmark/v2/extension"
18 "github.com/yuin/goldmark/v2/parser"
19 "github.com/yuin/goldmark/v2/text"
21 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
23 "gitlab.com/gitlab-org/cli/internal/browser"
24 "gitlab.com/gitlab-org/cli/internal/run"
27type MarkdownRenderOpts []glamour.TermRendererOption
29// OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable
30func OpenInBrowser(url, browserType string) error {
31 browseCmd, err := browser.Command(url, browserType)
32 if err != nil {
33 return err
34 }
35 return run.PrepareCmd(browseCmd).Run()
38func SanitizePathName(path string) string {
39 if !strings.HasPrefix(path, "/") {
40 // Prefix the path with "/" ensures that filepath.Clean removes all `/..`
41 // See rule 4 of filepath.Clean for more information: https://pkg.go.dev/path/filepath#Clean
42 path = "/" + path
43 }
44 return filepath.Clean(path)
47func RenderMarkdown(text, glamourStyle string) (string, error) {
48 opts := MarkdownRenderOpts{
49 glamour.WithStylePath(getStyle(glamourStyle)),
50 }
52 return renderMarkdown(text, opts)
55func RenderMarkdownWithoutIndentations(text, glamourStyle string) (string, error) {
56 opts := MarkdownRenderOpts{
57 glamour.WithStylePath(getStyle(glamourStyle)),
58 markdownWithoutIndentation(),
59 }
61 return renderMarkdown(text, opts)
64// markdownParser must use the same extensions as glamour, so that a block is
65// a paragraph here exactly when glamour treats it as one.
66var markdownParser = parser.New(parser.WithExtensions(extension.NewGFMParser(), extension.NewDefinitionListParser()))
68// collapseSoftLineBreaks replaces soft line breaks in paragraphs with spaces so
69// that glamour can preserve intentional hard line breaks without also
70// preserving the source document's line wrapping.
71//
72// Workaround for https://github.com/charmbracelet/glamour/issues/84: glamour
73// either joins every line in a paragraph or preserves every newline.
74//
75// Every edit is made between two of a paragraph's Source() segments, which give
76// the exact byte range of each line with container markers and indentation
77// already removed. Hard line breaks are left untouched: the line that follows
78// one must keep its own indentation and container markers, otherwise removing
79// them can turn a paragraph continuation into a new block (a heading, a
80// blockquote, or a list). Inline content, including code spans, is never
81// copied, and no character in the source is inspected to guess where a line or
82// a block ends.
83func collapseSoftLineBreaks(src []byte) []byte {
84 doc := markdownParser.Parse(src)
86 type edit struct {
87 start, stop int
88 text string
89 }
90 var edits []edit
92 _ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
93 if !entering {
94 return ast.WalkContinue, nil
95 }
96 paragraph, ok := n.(*ast.Paragraph)
97 if !ok {
98 return ast.WalkContinue, nil
99 }
100 lines := paragraph.Source()
101 if len(lines) < 2 {
102 return ast.WalkContinue, nil
103 }
105 breaks := paragraphBreaks(n, lines)
107 for i := 0; i < len(lines)-1; i++ {
108 kind := breaks[i]
109 // A hard break needs no rewriting. Its marker is valid Markdown
110 // and the following line stays exactly as the author wrote it.
111 if kind == breakHard {
112 continue
113 }
115 // The edited range runs from the end of this line's text to the
116 // start of the next line's text, so the line ending and the next
117 // line's container markers and indentation are replaced by one
118 // space. A break inside a code span is not a line break: its line
119 // ending becomes a space, but its trailing whitespace is content
120 // and must be kept.
121 start := lineContentEnd(src, lines[i])
122 if kind == breakSoft {
123 for start > lines[i].Start && (src[start-1] == ' ' || src[start-1] == ' ') {
124 start--
125 }
126 }
127 edits = append(edits, edit{
128 start: start,
129 stop: lines[i+1].Start,
130 text: " ",
131 })
132 }
133 return ast.WalkContinue, nil
134 })
136 if len(edits) == 0 {
137 return src
138 }
140 // Apply from the end so earlier byte offsets stay valid.
141 out := src
142 for _, e := range slices.Backward(edits) {
143 replaced := make([]byte, 0, len(out)-(e.stop-e.start)+len(e.text))
144 replaced = append(replaced, out[:e.start]...)
145 replaced = append(replaced, e.text...)
146 replaced = append(replaced, out[e.stop:]...)
147 out = replaced
148 }
149 return out
152// breakKind classifies the line ending that the parser recorded at the end of
153// a paragraph line, if any.
154type breakKind int
156const (
157 breakNone breakKind = iota
158 breakSoft
159 breakHard
162// paragraphBreaks reports, for each of the paragraph's lines, what the parser
163// recorded at its end. A break is attributed to the line whose segment contains
164// the end of the text node that carries it. A line ending inside a code span
165// has no text node of its own, so it stays breakNone: CommonMark normalizes a
166// line ending in a code span to a space, and that is the only rewrite it gets.
167func paragraphBreaks(paragraph ast.Node, lines []text.Segment) []breakKind {
168 breaks := make([]breakKind, len(lines))
169 _ = ast.Walk(paragraph, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
170 if !entering {
171 return ast.WalkContinue, nil
172 }
173 textNode, ok := n.(*ast.Text)
174 if !ok || (!textNode.HardLineBreak() && !textNode.SoftLineBreak()) {
175 return ast.WalkContinue, nil
176 }
177 kind := breakSoft
178 if textNode.HardLineBreak() {
179 kind = breakHard
180 }
181 for i, line := range lines {
182 if textNode.Value.Index().Stop <= line.Stop {
183 breaks[i] = kind
184 break
185 }
186 }
187 return ast.WalkContinue, nil
188 })
189 return breaks
192// lineContentEnd returns the offset just past a paragraph line's content,
193// excluding its line ending. goldmark includes the line ending in every line
194// segment except the block's last one.
195func lineContentEnd(src []byte, segment text.Segment) int {
196 stop := segment.Stop
197 for stop > segment.Start && (src[stop-1] == '\n' || src[stop-1] == '\r') {
198 stop--
199 }
200 return stop
203func renderMarkdown(text string, opts MarkdownRenderOpts) (string, error) {
204 // Glamour rendering preserves carriage return characters in code blocks, but
205 // we need to ensure that no such characters are present in the output.
206 text = strings.ReplaceAll(text, "\r\n", "\n")
207 text = string(collapseSoftLineBreaks([]byte(text)))
209 // Glamour joins every line in a paragraph unless newlines are preserved.
210 // Collapsing soft breaks above leaves only intentional hard breaks, which
211 // must survive the paragraph reflow.
212 opts = append(opts, glamour.WithPreservedNewLines())
214 tr, err := glamour.NewTermRenderer(opts...)
215 if err != nil {
216 return "", err
217 }
219 return tr.Render(text)
222func markdownWithoutIndentation() glamour.TermRendererOption {
223 overrides := []byte(`
224 {
225 "document": {
226 "margin": 0
227 },
228 "code_block": {
229 "margin": 0
230 }
231 }`)
233 return glamour.WithStylesFromJSONBytes(overrides)
236func getStyle(glamourStyle string) string {
237 if glamourStyle == "" || glamourStyle == "none" {
238 return "notty"
239 }
240 return glamourStyle
243func Pluralize(num int, thing string) string {
244 if num == 1 {
245 return fmt.Sprintf("%d %s", num, thing)
246 }
247 return fmt.Sprintf("%d %ss", num, thing)
250func fmtDuration(amount int, unit string) string {
251 return fmt.Sprintf("about %s ago", Pluralize(amount, unit))
254func PrettyTimeAgo(ago time.Duration) string {
255 if ago < time.Minute {
256 return "less than a minute ago"
257 }
258 if ago < time.Hour {
259 return fmtDuration(int(ago.Minutes()), "minute")
260 }
261 if ago < 24*time.Hour {
262 return fmtDuration(int(ago.Hours()), "hour")
263 }
264 if ago < 30*24*time.Hour {
265 return fmtDuration(int(ago.Hours())/24, "day")
266 }
267 if ago < 365*24*time.Hour {
268 return fmtDuration(int(ago.Hours())/24/30, "month")
269 }
271 return fmtDuration(int(ago.Hours()/24/365), "year")
274func TimeToPrettyTimeAgo(d time.Time) string {
275 now := time.Now()
276 ago := now.Sub(d)
277 return PrettyTimeAgo(ago)
280func FmtDuration(d time.Duration) string {
281 d = d.Round(time.Second)
282 m := d / time.Minute
283 d -= m * time.Minute
284 s := d / time.Second
285 return fmt.Sprintf("%02dm %02ds", m, s)
288func Humanize(s string) string {
289 // Replaces - and _ with spaces.
290 replace := "_-"
291 h := func(r rune) rune {
292 if strings.ContainsRune(replace, r) {
293 return ' '
294 }
295 return r
296 }
298 return strings.Map(h, s)
301func DisplayURL(urlStr string) string {
302 u, err := url.Parse(urlStr)
303 if err != nil {
304 return urlStr
305 }
306 return u.Hostname() + u.Path
309// PresentInStringSlice take a Hay (Slice of Strings) and a Needle (string)
310// and returns true based on whether or not the Needle is present in the hay.
311func PresentInStringSlice(hay []string, needle string) bool {
312 return slices.Contains(hay, needle)
315// CommonElementsInStringSlice takes 2 Slices of Strings and returns a Third Slice
316// that is the common elements between the first 2 Slices.
317func CommonElementsInStringSlice(s1 []string, s2 []string) []string {
318 hash := make(map[string]bool)
319 for x := range s1 {
320 hash[s1[x]] = true
321 }
322 var arr []string
323 for i := range s2 {
324 if hash[s2[i]] {
325 arr = append(arr, s2[i])
326 }
327 }
328 return arr
331// IsValidUrl tests a string to determine if it is a well-structured url or not.
332func IsValidURL(toTest string) bool {
333 _, err := url.ParseRequestURI(toTest)
334 if err != nil {
335 return false
336 }
338 u, err := url.Parse(toTest)
339 if err != nil || u.Scheme == "" || u.Host == "" {
340 return false
341 }
343 return true
346// Map transfers the elements of its first argument using the result of the second fn(e)
347func Map[T1, T2 any](elems []T1, fn func(T1) T2) []T2 {
348 r := make([]T2, len(elems))
350 for i, v := range elems {
351 r[i] = fn(v)
352 }
354 return r
357// IsEnvVarEnabled checks if an environment variable is set
358// and logs a warning to stderr if a boolean value cannot be parsed.
359func IsEnvVarEnabled(key string) (bool, bool) {
360 str, found := os.LookupEnv(key)
361 if found {
362 val, err := strconv.ParseBool(str)
363 // if the environment variable has invalid value we print a warning
364 // otherwise we return the parsed value
365 if err != nil {
366 fmt.Fprintf(os.Stderr, "WARNING: Could not parse %s environment variable value: %s\n", key, err.Error())
367 }
369 if !strings.HasPrefix(key, "GLAB_") {
370 PrintDeprecationWarning(key)
371 }
372 return val, found
373 }
375 return false, false
378// PrintDeprecationWarning prints a deprecation warning to stderr to use the
379// `GLAB_` prefix with environment variables. Diagnostics go to stderr so they
380// do not pollute stdout pipelines — see gitlab-org/cli#8371.
381func PrintDeprecationWarning(key string) {
382 fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: The environment variable %s has been deprecated and will be removed in future releases. Use GLAB_%s instead.\n", key, key)
385// FormatDueDate returns an empty string if date is nil
386func FormatDueDate(date *gitlab.ISOTime) string {
387 if date == nil {
388 return ""
389 }
390 return date.String()
393// FormatTimeStats summarizes time tracking, for example "1h 30m spent of 2d
394// estimated". It returns an empty string when nothing is tracked.
395func FormatTimeStats(ts *gitlab.TimeStats) string {
396 if ts == nil {
397 return ""
398 }
399 var parts []string
400 if ts.HumanTotalTimeSpent != "" {
401 parts = append(parts, ts.HumanTotalTimeSpent+" spent")
402 }
403 if ts.HumanTimeEstimate != "" {
404 parts = append(parts, ts.HumanTimeEstimate+" estimated")
405 }
406 return strings.Join(parts, " of ")
409// timeTrackingUnits uses the GitLab time tracking conversion rates, which
410// count a working day of 8 hours rather than a calendar day.
411var timeTrackingUnits = []struct {
412 suffix string
413 seconds int64
414}{
415 {"mo", 4 * 5 * 8 * 3600},
416 {"w", 5 * 8 * 3600},
417 {"d", 8 * 3600},
418 {"h", 3600},
419 {"m", 60},
420 {"s", 1},
423// FormatTimeSpent formats a number of seconds as GitLab displays tracked
424// time, for example "1w 2d 3h 30m". Negative values keep a leading "-".
425func FormatTimeSpent(seconds int64) string {
426 if seconds == 0 {
427 return "0h"
428 }
429 sign := ""
430 if seconds < 0 {
431 sign = "-"
432 seconds = -seconds
433 }
434 var parts []string
435 for _, unit := range timeTrackingUnits {
436 if n := seconds / unit.seconds; n > 0 {
437 parts = append(parts, fmt.Sprintf("%d%s", n, unit.suffix))
438 seconds %= unit.seconds
439 }
440 }
441 return sign + strings.Join(parts, " ")
444// ParseEditorCommand parses an editor command string into separate command and arguments.
445// This is useful when passing editor commands to libraries like huh that expect separate
446// command and argument parameters.
447//
448// For example:
449// - Input: "emacsclient -t -a \"emacs -nw\""
450// - Output: []string{"emacsclient", "-t", "-a", "emacs -nw"}
451//
452// If parsing fails, it returns a slice containing just the original string.
453func ParseEditorCommand(editorCmd string) []string {
454 if editorCmd == "" {
455 return nil
456 }
458 parts, err := shlex.Split(editorCmd)
459 if err != nil || len(parts) == 0 {
460 // Fall back to returning the full string if parsing fails
461 return []string{editorCmd}
462 }
464 return parts
467// ReplaceNonAlphaNumericChars replaces non alpha-numeric values with provided char/string
468func ReplaceNonAlphaNumericChars(words, replaceWith string) string {
469 reg := regexp.MustCompile("[^A-Za-z0-9]+")
470 newStr := reg.ReplaceAllString(strings.Trim(words, " "), replaceWith)
471 return newStr
474// StringToInt converts a string to an integer, returning 0 if conversion fails
475func StringToInt(str string) int {
476 strInt, err := strconv.Atoi(str)
477 if err != nil {
478 return 0
479 }
480 return strInt
Diagnostics6 git calls · 35 ms
CallStatusSizeTime035 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
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 · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=4
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
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
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:0200307 B · 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=307
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a37f1d670e91d25a83dd8509fb549df805c58f38
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=307
git.pkt.payload.max=280
git.sideband.band1.bytes=280
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=280
git.pack.trailer=1d8da1d6ced05f2d435b7a3203f70deda031ad4a
Open pack map
fetch2005.0 KiB · hit26 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=25
git.response.bytes=5147
git.response.cache=HIT
git.response.duration_ms=26
git.fetch.wants=86fc0a2581e74e00b5ae1b59ce774a97dc346ae2
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=5147
git.pkt.payload.max=5120
git.sideband.band1.bytes=5120
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
a5e13019ac2ca9fe
Request
6506fd22…
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