gitoria
All repositories: gitoria
5.8 KB
// lib/transport.hl — CLONE, FETCH AND PUSH OVER HTTPS (ticket gitoria#7; docs/git-backend.md). Git's "smart HTTP" protocol,// answered by this app itself with the `git` binary: on a repo address `<slug>.<domain>` the paths// /<slug>.git/info/refs?service=git-upload-pack | git-receive-pack (what a client asks first)// /<slug>.git/git-upload-pack (fetch / clone) /<slug>.git/git-receive-pack (push)// spawn `git upload-pack | receive-pack --stateless-rpc` and hand the request body over and the answer back.// The clone URL is https://<slug>.<domain>/<slug>.git — the folder git makes is named like the repo.// * READ (clone, fetch) needs no login: every repo is public (the concept has no private repos yet).// * WRITE (push) needs a token as the password (tokens.hl) of THE REPO'S OWNER — nobody else may push (decision below).// * The body travels through temp files, byte for byte (a String here holds raw bytes; hl:fs writes and reads them as// they are): hl:proc run() has no stdin, so the child reads `< body` and writes `> answer` in a tiny `sh -c` with// the paths as positional arguments (no user text in the script). A gzip body (git compresses big requests) is// unpacked first. Git protocol v2 is passed through (`Git-Protocol` → GIT_PROTOCOL, digits only).// * No hook: pull requests and releases are read from the commits when their page is built.// The checks, the plain answers and the pkt-line are in transport-helpers.hl.import { Response } from 'hl:http1'import { run } from 'hl:proc'import { readFile, writeFile, remove, exists } from 'hl:fs'import { randomBytes } from 'hl:crypto'import { NL, slugOfHost, publicUrl } from './util.hl'import { repoBySlug, mayPush } from './repos.hl'import { slugError } from './repos-helpers.hl'import { userOfToken } from './tokens.hl'import { repoDir, tmpDir } from './git.hl'import { notifyPush } from './tickets.hl'import { passwordOf, plain, protocolEnv, pktLine } from './transport-helpers.hl'static seconds = 300 // one upload-pack / receive-pack callstatic maxBody = 500000000 // a push or fetch request above this is refused (bytes)// ONE GIT CALL: the request body (or none) in, the answer out. → the answer's bytes as a String, or null (git failed and said nothing)static callGit = (slug, service, advertise, req) => {dir = tmpDir()name = dir + '/' + randomBytes(12, 'hex')inFile = name + '.in'outFile = name + '.out'let zipped = falselet script = 'exec git ' + service.slice(4) + ' --stateless-rpc --advertise-refs "$1" > "$3"'if (!advertise) {body = req.body == null ? '' : req.bodywriteFile(inFile, body, 384)enc = req.headers['content-encoding']zipped = enc != null && hlTypeName(enc) == 'String' && (enc.toLowerCase() == 'gzip' || enc.toLowerCase() == 'x-gzip')script = zipped ? 'gzip -dc < "$2" | git ' + service.slice(4) + ' --stateless-rpc "$1" > "$3"' : 'exec git ' + service.slice(4) + ' --stateless-rpc "$1" < "$2" > "$3"'}r = run(['sh', '-c', script, 'sh', repoDir(slug), inFile, outFile], { timeout = seconds env = protocolEnv(req) })out = exists(outFile) ? readFile(outFile) : nullif (exists(inFile)) { remove(inFile) }if (exists(outFile)) { remove(outFile) }// git answers nothing to the client's "0000" probe of a big push (exit 0): that is a 200 with an empty body, as git http-backend doesif (out == null || (out == '' && (r == null || r.exit != 0))) { return null }return out}// THE ROUTE (project.hl, a function route): `/:repo/info/refs`, `/:repo/git-upload-pack`, `/:repo/git-receive-pack`static gitTransport = (route, req) => {hostHeader = req.headers['host']slug = slugOfHost(hostHeader == null ? '' : hostHeader.split(':')[0])repoName = route.params.repoif (slug == '' || slugError(slug) != null || repoName != slug + '.git' || repoBySlug(slug) == null) { return plain(404, 'no such repository') }last = req.path.slice(req.path.lastIndexOf('/') + 1)advertise = last == 'refs'service = advertise ? (req.query != null ? req.query.service : null) : lastif (service != 'git-upload-pack' && service != 'git-receive-pack') { return plain(403, 'only the smart git protocol is served here: use git clone / fetch / push') }if (advertise && req.method != 'GET') { return plain(405, 'GET only') }if (!advertise && req.method != 'POST') { return plain(405, 'POST only') }if (req.body != null && req.body.length > maxBody) { return plain(413, 'that request is too big') }if (service == 'git-receive-pack') {realm = { 'WWW-Authenticate' = 'Basic realm="gitoria"' }pw = passwordOf(req.headers['authorization'])if (pw == null) { return plain(401, 'pushing needs a token: log in at ' + publicUrl + ', make one under "Access tokens", and use it as the password', realm) }userId = userOfToken(pw)if (userId == null) { return plain(401, 'that token is not valid (removed, or mistyped)', realm) }repo = repoBySlug(slug)if (!mayPush(userId, repo)) { return plain(403, 'only the owner of this repository can push to it') }}let out = callGit(slug, service, advertise, req)if (out == null) { return plain(500, 'git gave no answer') }let headers = { 'Cache-Control' = 'no-cache, max-age=0, must-revalidate' 'Pragma' = 'no-cache' }if (advertise) {headers['Content-Type'] = 'application/x-' + service + '-advertisement'// protocol v2 answers without the service banner; v0/v1 starts with it (as git http-backend does)v2 = protocolEnv(req).GIT_PROTOCOL != null && protocolEnv(req).GIT_PROTOCOL.includes('version=2')if (!v2) { out = pktLine('# service=' + service + NL) + '0000' + out }} else {headers['Content-Type'] = 'application/x-' + service + '-result'// a push arrived: tell the connected tickets project about commits naming a ticket (tickets.hl; never fails the push)if (service == 'git-receive-pack') { notifyPush(slug, false) }}return new Response(out, { headers = headers })}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
Latest commits
- cd99d2feWIP gitoria#22: always render the repo level's slot (missing-repo boot hang), pages render nothing without a repo; hydration diagnosis in reports/G2-hydrate.mdmre
- e5404b9breport: commit idsmre
- 63cfa21ereports/G2-repotabs.mdmre
- 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
- 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
- 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
- 48a6154bre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
- e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
- f06f5349re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
- c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
- f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
- fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
- 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
- 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
- 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
- 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
- 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
- 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
- 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
- e6dff0cbgitoria#23: report 005, STATUS, LOGmre