gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit34143d2c34143d2cgitoria#46: closing brace of appComments lost in the mergemre34143d2c/lib/git.hl

4.1 KB

  1. // lib/git.hl — GIT ON THE SERVER: where the bare repositories live and how git is called. Each repo is one BARE git
  2. // repository, <gitRoot>/<slug>.git (created with the repo: repos.hl createRepo → initRepo). Reads go through the
  3. // `git` binary with hl:proc (docs/git-backend.md): an argv list, never a shell, the slug is
  4. // validated by the caller (repos-helpers.hl slugError), a path only ever comes from git itself.
  5. // Every read calls git with hl:proc run() (hybriel#80: it waits), so the readers answer through their callback
  6. // before they return; the page components use the `...Now` forms at construction (server-rendered, gitoria#16).
  7. // What is read: the homepage (homepage.hl), the code browser (code.hl), pull requests (pulls.hl), releases (releases.hl).
  8. import { run, env, cwd } from 'hl:proc'
  9. import { exists, mkDir } from 'hl:fs'
  10. import { safeName } from './git-helpers.hl'
  11. // where the bare repositories live: GITORIA_GIT, else ./storage/git under the launch directory
  12. static gitRoot = env('GITORIA_GIT') != null && env('GITORIA_GIT') != '' ? env('GITORIA_GIT') : cwd() + '/storage/git'
  13. static repoDir = (slug) => { return gitRoot + '/' + slug + '.git' }
  14. static maxTree = 20000 // paths looked at
  15. static seconds = 15 // one git call
  16. // one git call: argv (after `git --git-dir=<dir>`), `done({ ok, lines })` exactly once — BEFORE git() returns:
  17. // hl:proc run() waits for the program (hybriel#80), so a page's root can read git while it is built on the server
  18. // and the callback style of the readers is synchronous. Past `seconds` git is killed; run() is then an Error
  19. // (absorbed by project.hl `on Error`) and yields null → ok = false.
  20. static git = (slug, rest, limit, done) => {
  21. argv = ['git', '--git-dir=' + repoDir(slug), '-c', 'core.quotePath=false']
  22. for (a of rest) { argv.push(a) }
  23. r = run(argv, { timeout = seconds })
  24. let lines = []
  25. if (r != null) {
  26. for (l of r.lines) { if (lines.length < limit) { lines.push(l) } }
  27. }
  28. done({ ok = r != null && r.exit == 0 lines = lines })
  29. return null
  30. }
  31. // one git call answered directly: run()'s result ({ exit, lines }) or null
  32. static gitRaw = (slug, rest) => {
  33. argv = ['git', '--git-dir=' + repoDir(slug), '-c', 'core.quotePath=false']
  34. for (a of rest) { argv.push(a) }
  35. return run(argv, { timeout = seconds })
  36. }
  37. // the branch names of a repo → done([name])
  38. static branchNames = (slug, done) => {
  39. git(slug, ['for-each-ref', '--format=%(refname:short)', 'refs/heads'], 500, (r) => {
  40. out = []
  41. if (r.ok) { for (l of r.lines) { if (safeName(l)) { out.push(l) } } }
  42. done(out)
  43. return null
  44. })
  45. }
  46. // which branch is "main" for a repo: the setting if it exists, else `main`, else the first one; null = no branch
  47. static pickDefault = (names, setting) => {
  48. if (setting != null && setting != '' && names.includes(setting)) { return setting }
  49. if (names.includes('main')) { return 'main' }
  50. return names.length > 0 ? names[0] : null
  51. }
  52. static defaultBranch = (slug, setting, done) => {
  53. branchNames(slug, (names) => {
  54. done(pickDefault(names, setting))
  55. return null
  56. })
  57. }
  58. // a new empty bare repository for a repo (a failure is not fatal: a repo without one just has no files)
  59. static initRepo = (slug) => {
  60. i = slug.indexOf('/')
  61. if (i > 0 && !exists(gitRoot + '/' + slug.slice(0, i))) { mkDir(gitRoot + '/' + slug.slice(0, i), 493) }
  62. r = run(['git', 'init', '--bare', '-q', '-b', 'main', repoDir(slug)], { timeout = seconds })
  63. // refs/pull/* is gitoria's (pullsync.hl): a push to it is refused (HTTPS also says it per call, transport.hl)
  64. run(['git', '--git-dir=' + repoDir(slug), 'config', 'receive.hideRefs', 'refs/pull'], { timeout = seconds })
  65. return r
  66. }
  67. // does the repo have no branch yet (nothing pushed)? — the "how to add code" box opens itself then (gitoria#7)
  68. static isEmptyNow = (slug) => {
  69. let empty = true
  70. git(slug, ['for-each-ref', '--count=1', 'refs/heads'], 1, (r) => {
  71. empty = !r.ok || r.lines.length == 0
  72. return null
  73. })
  74. return empty
  75. }
  76. // the folder for temp files beside the repositories (the push / fetch bodies of transport.hl, a key for ssh-keygen in sshkeys.hl)
  77. static tmpDir = () => {
  78. d = gitRoot + '/.tmp'
  79. if (!exists(d)) { mkDir(d, 448) }
  80. return d
  81. }

Branches

Latest commits

  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre