main
ply4 git calls306 ms43 KiB2 cachedprotocol v2
152 lines6.6 KiBRaw
1import { atIndex, captureGroup, fromAbsent } from '../absent.ts'
2import { unwrapOr } from '../option.ts'
3import { clampText } from './escape.ts'
4import { LAYOUT, RELATIVE_TIME } from './layout.ts'
6const BYTE = 1024
7const BYTE_TENTHS = 10 * BYTE
8const SECOND_MS = 1_000
10export function shortOid(oid: string): string {
11 return oid.slice(0, LAYOUT.shortOid)
14export function formatBytes(n: number): string {
15 if (!Number.isFinite(n) || n < 0) return '0 B'
16 if (n < BYTE) return `${Math.round(n)} B`
17 if (n < BYTE * BYTE) return `${(n / BYTE).toFixed(n < BYTE_TENTHS ? 1 : 0)} KiB`
18 return `${(n / (BYTE * BYTE)).toFixed(n < BYTE_TENTHS * BYTE ? 1 : 0)} MiB`
21export function formatDuration(ms: number): string {
22 if (ms >= SECOND_MS) return `${(ms / SECOND_MS).toFixed(1)} s`
23 return `${Math.round(ms)} ms`
26export type Trailer = { key: string; value: string }
27export type CommitProse = { subject: string; paragraphs: readonly string[]; trailers: readonly Trailer[] }
29// git interpret-trailers: `Token: value` lines in the final paragraph. GitHub's closing keywords count too.
30const TRAILER = /^([A-Za-z][A-Za-z0-9-]{0,63}):[ \t]+(\S.*)$/
31const CLOSING = /^(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved) +(([\w.-]+\/[\w.-]+)?#\d+.*)$/i
33const KNOWN_TRAILER = /^([A-Za-z-]+-by|change-id|closes?|closed|fix(es|ed)?|resolve[sd]?|refs?|see-also|cc|bug|issue|ticket|[A-Za-z]+-session)$/i
35function groupText(match: RegExpExecArray | null, index: number): string | null {
36 const group = captureGroup(match, index)
37 if (group.tag === 'none' || group.value === '') return null
38 return group.value
41function trailerOf(line: string): Trailer | null {
42 const t = TRAILER.exec(line)
43 const trailerKey = groupText(t, 1)
44 const trailerValue = groupText(t, 2)
45 if (trailerKey !== null && trailerValue !== null) return { key: trailerKey, value: trailerValue.trim() }
46 const c = CLOSING.exec(line)
47 const closingKey = groupText(c, 1)
48 const closingValue = groupText(c, 2)
49 if (closingKey !== null && closingValue !== null) return { key: closingKey.charAt(0).toUpperCase() + closingKey.slice(1).toLowerCase(), value: closingValue.trim() }
50 return null
53/**
54 * Subject, body paragraphs, trailers. Paragraphs keep their line breaks (git messages are often lists or hard-wrapped
55 * prose; the page shows them with white-space: pre-wrap). Trailing trailer lines (Co-Authored-By, Signed-off-by,
56 * Closes #n) are split out so the page can show them quietly.
57 */
58export function commitProse(message: string): CommitProse {
59 const clamped = clampText(message, LAYOUT.commitMessageChars).replace(/\r\n?/g, '\n')
60 const trimmed = clamped.replace(/\s+$/, '')
61 const subject = unwrapOr(atIndex(trimmed.split('\n'), 0), '').trim()
62 const rest = trimmed.slice(trimmed.indexOf('\n') === -1 ? trimmed.length : trimmed.indexOf('\n')).replace(/^\n+/, '')
63 if (!rest.trim()) return { subject, paragraphs: [], trailers: [] }
64 const blocks = rest.split(/\n[ \t]*\n/).map(block => block.replace(/^\n+|\s+$/g, '')).filter(block => block.trim().length > 0)
65 const trailers: Trailer[] = []
66 // Trailers live in the final paragraphs; a run of closing keywords may sit in its own paragraph just above them.
67 while (blocks.length) {
68 const lines = unwrapOr(atIndex(blocks, blocks.length - 1), '').split('\n').map(line => line.trim()).filter(line => line.length > 0)
69 const parsed = lines.map(trailerOf)
70 if (!parsed.length || parsed.some(t => t === null)) break
71 // "Before: 12 ms / After: 5 ms" is prose, not trailers: ask for one key git or GitHub would recognise.
72 if (!parsed.some(t => t && KNOWN_TRAILER.test(t.key))) break
73 trailers.unshift(...(parsed as Trailer[]))
74 blocks.pop()
75 }
76 return { subject, paragraphs: blocks, trailers }
79export type ExactTime = { iso: string; label: string; relative: string }
81export function exactTime(iso: string, tz: string, now: number): ExactTime {
82 const parsed = Date.parse(iso)
83 if (Number.isNaN(parsed)) {
84 const label = [iso, tz].filter(part => part.length > 0).join(' ')
85 return { iso, label, relative: label }
86 }
87 const formatted = new Intl.DateTimeFormat('en-GB', {
88 year: 'numeric',
89 month: 'short',
90 day: '2-digit',
91 hour: '2-digit',
92 minute: '2-digit',
93 second: '2-digit',
94 timeZone: 'UTC',
95 hourCycle: 'h23',
96 }).format(new Date(parsed))
97 const label = tz ? `${formatted} UTC (${tz})` : `${formatted} UTC`
98 return { iso, label, relative: relativePhrase(now - parsed) }
101function relativePhrase(deltaMs: number): string {
102 const future = deltaMs < 0
103 const abs = Math.abs(deltaMs)
104 const phrase = relativeUnit(abs)
105 if (phrase === 'just now') return phrase
106 return future ? `in ${phrase}` : `${phrase} ago`
109function relativeUnit(absMs: number): string {
110 if (absMs < RELATIVE_TIME.justNowMs) return 'just now'
111 const minutes = Math.round(absMs / RELATIVE_TIME.minuteMs)
112 if (absMs < RELATIVE_TIME.hourMs) return plural(minutes, 'minute')
113 const hours = Math.round(absMs / RELATIVE_TIME.hourMs)
114 if (absMs < RELATIVE_TIME.dayMs) return plural(hours, 'hour')
115 const days = Math.round(absMs / RELATIVE_TIME.dayMs)
116 if (days < RELATIVE_TIME.monthDays) return plural(days, 'day')
117 const months = Math.round(days / RELATIVE_TIME.monthDays)
118 if (days < RELATIVE_TIME.yearDays) return plural(months, 'month')
119 const years = Math.max(1, Math.round(days / RELATIVE_TIME.yearDays))
120 return plural(years, 'year')
123function plural(n: number, unit: string): string {
124 const count = Math.max(1, n)
125 return count === 1 ? `1 ${unit}` : `${count} ${unit}s`
128/** Integer percents that sum to 100. Zero-length parts stay zero. */
129export function portions(lengths: readonly number[]): number[] {
130 const total = lengths.reduce((sum, n) => sum + n, 0) || 1
131 const raw = lengths.map(n => (n / total) * LAYOUT.percentSteps)
132 const floors = raw.map(n => Math.floor(n))
133 let left = LAYOUT.percentSteps - floors.reduce((sum, n) => sum + n, 0)
134 const order = raw.map((n, i) => [n - unwrapOr(atIndex(floors, i), 0), i] as const).sort((a, b) => b[0] - a[0])
135 const out = floors.slice()
136 for (let i = 0; i < left; i++) {
137 const pair = atIndex(order, i % order.length)
138 let slot = 0
139 if (pair.tag === 'some') {
140 const index = atIndex(pair.value, 1)
141 if (index.tag === 'some' && typeof index.value === 'number') slot = index.value
142 }
143 const current = fromAbsent(out[slot])
144 out[slot] = (current.tag === 'some' && typeof current.value === 'number' ? current.value : 0) + 1
145 }
146 return out
149export function percentClass(prefix: 'o' | 'w', value: number): string {
150 const clamped = Math.max(0, Math.min(LAYOUT.percentSteps, value))
151 return `${prefix}${clamped}`
Diagnostics4 git calls · 306 ms
CallStatusSizeTime0306 ms
discover200152 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=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · 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=145
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 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=41349
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921
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=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.6 KiB301 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=301
git.response.bytes=2631
git.response.cache=MISS
git.response.duration_ms=301
git.fetch.wants=3feab2004084127736e92ae7f5110c50b66aefcb
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=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2631
git.pkt.payload.max=2610
git.sideband.band1.bytes=2609
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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2e8bdb9eeecfa
Request
e65522ac…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format