gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit16a3530f16a3530fMerge branch 'heap-revert' into mainGitoria16a3530f/lib/git.hl

4.2 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, cwd } from 'hl:proc'
  9. import { exists, mkDir } from 'hl:fs'
  10. import { envOr } from './util.hl'
  11. import { safeName } from './git-helpers.hl'
  12. // where the bare repositories live: GITORIA_GIT, else ./storage/git under the launch directory
  13. static gitRoot = envOr('GITORIA_GIT', cwd() + '/storage/git', false)
  14. static repoDir = (slug) => { return gitRoot + '/' + slug + '.git' }
  15. // the hooks folder of receive-pack (docker/sshd/hooks: pre-receive refuses a direct push to the protected branch, gitoria#50); the app runs in its own folder
  16. static hooksDir = cwd() + '/docker/sshd/hooks'
  17. static maxTree = 20000 // paths looked at
  18. static seconds = 15 // one git call
  19. // one git call: argv (after `git --git-dir=<dir>`), `done({ ok, lines })` exactly once — BEFORE git() returns:
  20. // hl:proc run() waits for the program (hybriel#80), so a page's root can read git while it is built on the server
  21. // and the callback style of the readers is synchronous. Past `seconds` git is killed; run() is then an Error
  22. // (absorbed by project.hl `on Error`) and yields null → ok = false.
  23. static git = (slug, rest, limit, done) => {
  24. argv = ['git', '--git-dir=' + repoDir(slug), '-c', 'core.quotePath=false']
  25. for (a of rest) { argv.push(a) }
  26. r = run(argv, { timeout = seconds })
  27. let lines = []
  28. if (r != null) {
  29. for (l of r.lines) { if (lines.length < limit) { lines.push(l) } }
  30. }
  31. done({ ok = r != null && r.exit == 0 lines = lines })
  32. return null
  33. }
  34. // one git call answered directly: run()'s result ({ exit, lines }) or null
  35. static gitRaw = (slug, rest) => {
  36. argv = ['git', '--git-dir=' + repoDir(slug), '-c', 'core.quotePath=false']
  37. for (a of rest) { argv.push(a) }
  38. return run(argv, { timeout = seconds })
  39. }
  40. // the branch names of a repo → done([name])
  41. static branchNames = (slug, done) => {
  42. git(slug, ['for-each-ref', '--format=%(refname:short)', 'refs/heads'], 500, (r) => {
  43. out = []
  44. if (r.ok) { for (l of r.lines) { if (safeName(l)) { out.push(l) } } }
  45. done(out)
  46. return null
  47. })
  48. }
  49. // which branch is "main" for a repo: the setting if it exists, else `main`, else the first one; null = no branch
  50. static pickDefault = (names, setting) => {
  51. if (setting != null && setting != '' && names.includes(setting)) { return setting }
  52. if (names.includes('main')) { return 'main' }
  53. return names.length > 0 ? names[0] : null
  54. }
  55. static defaultBranch = (slug, setting, done) => {
  56. branchNames(slug, (names) => {
  57. done(pickDefault(names, setting))
  58. return null
  59. })
  60. }
  61. // a new empty bare repository for a repo (a failure is not fatal: a repo without one just has no files)
  62. static initRepo = (slug) => {
  63. i = slug.indexOf('/')
  64. if (i > 0 && !exists(gitRoot + '/' + slug.slice(0, i))) { mkDir(gitRoot + '/' + slug.slice(0, i), 493) }
  65. r = run(['git', 'init', '--bare', '-q', '-b', 'main', repoDir(slug)], { timeout = seconds })
  66. // refs/pull/* is gitoria's (pullsync.hl): a push to it is refused (HTTPS also says it per call, transport.hl)
  67. run(['git', '--git-dir=' + repoDir(slug), 'config', 'receive.hideRefs', 'refs/pull'], { timeout = seconds })
  68. return r
  69. }
  70. // does the repo have no branch yet (nothing pushed)? — the "how to add code" box opens itself then (gitoria#7)
  71. static isEmptyNow = (slug) => {
  72. let empty = true
  73. git(slug, ['for-each-ref', '--count=1', 'refs/heads'], 1, (r) => {
  74. empty = !r.ok || r.lines.length == 0
  75. return null
  76. })
  77. return empty
  78. }
  79. // the folder for temp files beside the repositories (the push / fetch bodies of transport.hl, a key for ssh-keygen in sshkeys.hl)
  80. static tmpDir = () => {
  81. d = gitRoot + '/.tmp'
  82. if (!exists(d)) { mkDir(d, 448) }
  83. return d
  84. }

Branches

Latest commits

  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre