GitLabgitlab-org

cli

main
ply6 git calls40 ms96 KiB4 cachedprotocol v2
153 lines3.4 KiBRaw
1//go:build !integration
3package diff
5import (
6 "testing"
7)
9func TestParse(t *testing.T) {
10 // Typical unified diff fragment from GitLab
11 diffText := `@@ -1,5 +1,6 @@
12 line1
13 line2
14+added line
15 line3
16-removed line
17 line5
20 lines := Parse(diffText)
22 // Expected:
23 // line1: unchanged old=1 new=1
24 // line2: unchanged old=2 new=2
25 // added line: added new=3
26 // line3: unchanged old=3 new=4
27 // removed line: removed old=4
28 // line5: unchanged old=5 new=5
30 expected := []Line{
31 {Unchanged, 1, 1},
32 {Unchanged, 2, 2},
33 {Added, 0, 3},
34 {Unchanged, 3, 4},
35 {Removed, 4, 0},
36 {Unchanged, 5, 5},
37 }
39 if len(lines) != len(expected) {
40 t.Fatalf("got %d lines, want %d", len(lines), len(expected))
41 }
43 for i, got := range lines {
44 want := expected[i]
45 if got != want {
46 t.Errorf("line %d: got %+v, want %+v", i, got, want)
47 }
48 }
51func TestFindNewLine(t *testing.T) {
52 lines := []Line{
53 {Unchanged, 1, 1},
54 {Added, 0, 2},
55 {Unchanged, 2, 3},
56 }
58 tests := []struct {
59 name string
60 target int
61 wantOld int
62 wantType LineType
63 wantError bool
64 }{
65 {name: "added line", target: 2, wantOld: 0, wantType: Added},
66 {name: "unchanged line", target: 3, wantOld: 2, wantType: Unchanged},
67 {name: "not found", target: 99, wantError: true},
68 }
69 for _, tt := range tests {
70 t.Run(tt.name, func(t *testing.T) {
71 oldLine, lt, err := FindNewLine(lines, tt.target)
72 if tt.wantError {
73 if err == nil {
74 t.Fatal("expected error, got nil")
75 }
76 return
77 }
78 if err != nil {
79 t.Fatal(err)
80 }
81 if lt != tt.wantType || oldLine != tt.wantOld {
82 t.Errorf("got oldLine=%d type=%d, want oldLine=%d type=%d", oldLine, lt, tt.wantOld, tt.wantType)
83 }
84 })
85 }
88func TestParseBlankContextLines(t *testing.T) {
89 // Blank context lines may appear as empty strings when trailing
90 // whitespace is stripped from the diff (the " " becomes "").
91 // The parser must count them as unchanged context lines.
92 diffText := "@@ -10,5 +10,6 @@\n line10\n+added\n\n line12\n-removed\n line14\n"
93 // The empty line between "+added" and " line12" is a blank context line.
95 lines := Parse(diffText)
97 expected := []Line{
98 {Unchanged, 10, 10}, // line10
99 {Added, 0, 11}, // added
100 {Unchanged, 11, 12}, // blank context line (empty in diff)
101 {Unchanged, 12, 13}, // line12
102 {Removed, 13, 0}, // removed
103 {Unchanged, 14, 14}, // line14
104 }
106 if len(lines) != len(expected) {
107 t.Fatalf("got %d lines, want %d\nlines: %+v", len(lines), len(expected), lines)
108 }
110 for i, got := range lines {
111 want := expected[i]
112 if got != want {
113 t.Errorf("line %d: got %+v, want %+v", i, got, want)
114 }
115 }
118func TestFindOldLine(t *testing.T) {
119 lines := []Line{
120 {Unchanged, 1, 1},
121 {Removed, 2, 0},
122 {Unchanged, 3, 2},
123 }
125 tests := []struct {
126 name string
127 target int
128 wantNew int
129 wantType LineType
130 wantError bool
131 }{
132 {name: "removed line", target: 2, wantNew: 0, wantType: Removed},
133 {name: "unchanged line", target: 1, wantNew: 1, wantType: Unchanged},
134 {name: "not found", target: 99, wantError: true},
135 }
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 newLine, lt, err := FindOldLine(lines, tt.target)
139 if tt.wantError {
140 if err == nil {
141 t.Fatal("expected error, got nil")
142 }
143 return
144 }
145 if err != nil {
146 t.Fatal(err)
147 }
148 if lt != tt.wantType || newLine != tt.wantNew {
149 t.Errorf("got newLine=%d type=%d, want newLine=%d type=%d", newLine, lt, tt.wantNew, tt.wantType)
150 }
151 })
152 }
Diagnostics6 git calls · 40 ms
CallStatusSizeTime040 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 KiB3 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=3
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 · hit4 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=4
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 · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=6
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: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=10eefc865961877fbdadfb282e2a4464608d8b1e
Open pack map
fetch2001.1 KiB · hit28 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=28
git.response.bytes=1142
git.response.cache=HIT
git.response.duration_ms=28
git.fetch.wants=8a8ad8de555055460596511bbdb95b27400bd026
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=1142
git.pkt.payload.max=1115
git.sideband.band1.bytes=1115
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
a5e11fde29927fd8
Request
0b5ccc55…
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