GitLabgitlab-org

cli

main
ply7 git calls67 ms97 KiB5 cachedprotocol v2
177 lines4.4 KiBRaw
1package urlwrapper
3import (
4 "fmt"
6 "mvdan.cc/xurls/v2"
7)
9// MDWrap wraps bare URLs in markdown link syntax while preserving markdown context.
10// It avoids double-wrapping URLs that are already in markdown format.
11// URLs inside backticks are left as-is since they typically represent literal values.
12func MDWrap(text string) string {
13 xurlsStrict := xurls.Strict()
15 // Build backtick map once at the start
16 insideBackticks := buildBacktickMap(text)
18 offset := 0
19 result := text
21 for {
22 match := xurlsStrict.FindStringIndex(result[offset:])
23 if match == nil {
24 break
25 }
27 startIdx := offset + match[0]
28 endIdx := offset + match[1]
29 url := result[startIdx:endIdx]
31 if shouldWrapURL(result, startIdx, insideBackticks) {
32 wrapped := fmt.Sprintf("<%s>", url)
33 lenDiff := len(wrapped) - len(url)
35 // Replace the URL with wrapped version
36 result = result[:startIdx] + wrapped + result[endIdx:]
38 // Update the backtick map by shifting positions after the insertion
39 insideBackticks = shiftBacktickMap(insideBackticks, startIdx, lenDiff)
41 // Move offset past the wrapped URL
42 offset = startIdx + len(wrapped)
43 } else {
44 // Move offset past this URL
45 offset = endIdx
46 }
47 }
49 return result
52// shiftBacktickMap adjusts the backtick map positions after text insertion.
53// All positions at or after insertPos are shifted by shiftAmount.
54func shiftBacktickMap(insideBackticks map[int]bool, insertPos int, shiftAmount int) map[int]bool {
55 if shiftAmount == 0 {
56 return insideBackticks
57 }
59 newMap := make(map[int]bool, len(insideBackticks)+shiftAmount)
61 for pos, isInside := range insideBackticks {
62 if pos < insertPos {
63 // Positions before insertion stay the same
64 newMap[pos] = isInside
65 } else {
66 // Positions at or after insertion are shifted
67 newMap[pos+shiftAmount] = isInside
68 }
69 }
71 return newMap
74// shouldWrapURL determines if a URL at the given position should be wrapped in markdown link syntax.
75func shouldWrapURL(text string, startIdx int, insideBackticks map[int]bool) bool {
76 // Don't wrap if already part of a markdown link: [text](URL)
77 if startIdx >= 2 && text[startIdx-2:startIdx] == "](" {
78 return false
79 }
81 // Don't wrap if inside backticks (literal values)
82 if insideBackticks[startIdx] {
83 return false
84 }
86 return true
89// buildBacktickMap creates a map of character positions that are inside backtick code spans.
90// It properly handles paired backticks and ignores escaped backticks.
91func buildBacktickMap(text string) map[int]bool {
92 insideCode := make(map[int]bool)
93 i := 0
95 for i < len(text) {
96 if isEscapedBacktick(text, i) {
97 i++
98 continue
99 }
101 if text[i] == '`' {
102 backtickCount := countConsecutiveBackticks(text, i)
103 startPos := i
104 i += backtickCount
106 closingPos := findClosingBackticks(text, i, backtickCount)
107 if closingPos != -1 {
108 markRangeAsCode(insideCode, startPos, closingPos+backtickCount)
109 i = closingPos + backtickCount
110 }
111 continue
112 }
113 i++
114 }
116 return insideCode
119// isEscapedBacktick checks if the backtick at position i is escaped with a backslash.
120// It properly handles cases where the backslash itself is escaped (e.g., \\`).
121func isEscapedBacktick(text string, i int) bool {
122 if i == 0 || text[i] != '`' {
123 return false
124 }
126 // Count consecutive backslashes before the backtick
127 backslashCount := 0
128 pos := i - 1
129 for pos >= 0 && text[pos] == '\\' {
130 backslashCount++
131 pos--
132 }
134 // If odd number of backslashes, the backtick is escaped
135 // If even number (including 0), the backtick is not escaped
136 return backslashCount%2 == 1
139// countConsecutiveBackticks counts how many backticks appear consecutively starting at position i.
140func countConsecutiveBackticks(text string, i int) int {
141 count := 0
142 for i < len(text) && text[i] == '`' {
143 count++
144 i++
145 }
146 return count
149// markRangeAsCode marks all positions in the given range as being inside code.
150func markRangeAsCode(insideCode map[int]bool, start, end int) {
151 for pos := start; pos < end; pos++ {
152 insideCode[pos] = true
153 }
156// findClosingBackticks finds the position of closing backticks that match the opening count.
157// Returns -1 if no matching closing backticks are found.
158func findClosingBackticks(text string, startPos int, count int) int {
159 i := startPos
160 for i < len(text) {
161 if isEscapedBacktick(text, i) {
162 i++
163 continue
164 }
166 if text[i] == '`' {
167 closingCount := countConsecutiveBackticks(text, i)
168 if closingCount == count {
169 return i
170 }
171 i += closingCount
172 continue
173 }
174 i++
175 }
176 return -1
Diagnostics7 git calls · 67 ms
CallStatusSizeTime067 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 KiB2 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=UPDATING
git.response.duration_ms=2
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
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 · hit4 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=4
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:0200174 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=174
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=9a364b543bd220d89afac6cc877ed8db92abf022
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=174
git.pkt.payload.max=147
git.sideband.band1.bytes=147
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=147
git.pack.trailer=5a3ab82bf6fbb95581196169deb2e857fd8acdf5
Open pack map
fetchtree:0200139 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=53
git.response.bytes=139
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=bbde363f8833c634e2072815d97a5ff00a237286
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=139
git.pkt.payload.max=112
git.sideband.band1.bytes=112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=112
git.pack.trailer=49411249929c380d2e6c935751bf23441e468ec5
Open pack map
fetch2001.6 KiB · hit53 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1660
git.response.cache=HIT
git.response.duration_ms=53
git.fetch.wants=191bc463715999cbcbcbcd8ce75972a41704d367
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=1660
git.pkt.payload.max=1633
git.sideband.band1.bytes=1633
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
a5e19827ac7fb86e
Request
c3963d03…
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