GitLabgitlab-org
cli
main
1// Package diff parses unified diff text to build line mappings.
2package diff
4import (
5 "fmt"
6 "regexp"
7 "strconv"
8 "strings"
9)
11// LineType describes what happened to a line.
12type LineType int
14const (
15 Unchanged LineType = iota
16 Added
17 Removed
18)
20// Line represents a single line in a diff hunk.
21type Line struct {
22 Type LineType
23 OldLine int // 0 if added
24 NewLine int // 0 if removed
25}
27var hunkRe = regexp.MustCompile(`^@@\s+-(\d+)(?:,\d+)?\s+\+(\d+)(?:,\d+)?\s+@@`)
29// Parse parses a unified diff fragment (the "diff" field from GitLab API)
30// and returns a list of Line entries.
31func Parse(diffText string) []Line {
32 var lines []Line
33 var oldLine, newLine int
34 inHunk := false
36 for raw := range strings.SplitSeq(strings.TrimRight(diffText, "\n"), "\n") {
37 if m := hunkRe.FindStringSubmatch(raw); m != nil {
38 oldLine, _ = strconv.Atoi(m[1])
39 newLine, _ = strconv.Atoi(m[2])
40 inHunk = true
41 continue
42 }
43 if strings.HasPrefix(raw, "\\ No newline") {
44 continue
45 }
46 if raw == "" {
47 if inHunk {
48 // Empty line inside a hunk = blank context line
49 // (trailing whitespace may have been stripped from " ")
50 lines = append(lines, Line{Type: Unchanged, OldLine: oldLine, NewLine: newLine})
51 oldLine++
52 newLine++
53 }
54 continue
55 }
57 switch raw[0] {
58 case '+':
59 lines = append(lines, Line{Type: Added, NewLine: newLine})
60 newLine++
61 case '-':
62 lines = append(lines, Line{Type: Removed, OldLine: oldLine})
63 oldLine++
64 default:
65 // context line (starts with ' ' or no prefix)
66 lines = append(lines, Line{Type: Unchanged, OldLine: oldLine, NewLine: newLine})
67 oldLine++
68 newLine++
69 }
70 }
71 return lines
72}
74// FindNewLine looks up a new-side line number in the parsed lines and returns
75// the corresponding old line number and the line type.
76// Returns an error if the line is not found in the diff.
77func FindNewLine(lines []Line, target int) (int, LineType, error) {
78 for _, l := range lines {
79 if l.NewLine == target && l.Type != Removed {
80 return l.OldLine, l.Type, nil
81 }
82 }
83 return 0, 0, fmt.Errorf("new line %d not found in diff", target)
84}
86// FindOldLine looks up an old-side line number in the parsed lines and returns
87// the corresponding new line number and the line type.
88func FindOldLine(lines []Line, target int) (int, LineType, error) {
89 for _, l := range lines {
90 if l.OldLine == target && l.Type != Added {
91 return l.NewLine, l.Type, nil
92 }
93 }
94 return 0, 0, fmt.Errorf("old line %d not found in diff", target)
95}
Diagnostics6 git calls · 29 ms
CallStatusSizeTime029 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 · hit2 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=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
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 · 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=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=44Open pack map
fetchtree:0200137 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=137 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=1085a30d51c3e18030848955ae214825d3f4142a 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=137 git.pkt.payload.max=110 git.sideband.band1.bytes=110 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=110 git.pack.trailer=10eefc865961877fbdadfb282e2a4464608d8b1eOpen pack map
fetch2001.0 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=1050 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=420f7cc531a1550786c191acf6ff1c98b1d7e2b7 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=1050 git.pkt.payload.max=1023 git.sideband.band1.bytes=1023 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
- a5e11f035bc17fd8
- Request
- dc310af2…
- 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