gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf3e196b5f3e196b5Document Markdown direct-child selector remaindersmref3e196b5/lib/git.hl

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

Branches

Latest commits

  • f3e196b5Document Markdown direct-child selector remaindersmre
  • 70597528Reference pull list rules and explain runtime class predicatesmre
  • 1ce7ac24Reference ticket row and feedback styles from its Viewmre
  • 10cdf54bGive organization repository rows their own Style referencesmre
  • 0d019521Document shared class selectors in the styles-only filemre
  • c67404a7Scope token labels and feedback through the token Viewmre
  • 944e3dabScope key names, fingerprints and errors to the keys Viewmre
  • 7bcf7bc2Nest repo navigation media rules and document selector gapsmre
  • 46a76272Reference release row styles from the Viewmre
  • a383f428Tie README definition and path styles to its own Viewmre
  • a90812ffConsolidate add-code styles under its existing View idmre
  • 89107a3eDocument the index direct-child selector remaindermre
  • 07403362Scope reply form layout through the component form rulemre
  • 097c52baStyle the login error through its View idmre
  • c6d9c86aOwn the not-found code rule in its componentmre
  • 055424c2Record W61 replacement theme gates, sidebar repro and baselinemre
  • 2ed09441Use layouts theme 17e0f03 for W61 compatibilitymre
  • b8609a91Merge branch 'main' into w61-stylemre
  • 4639caf7Report W61 theme startup blocker and day39 Style probemre
  • 81f0a0bbREADME: two caches were restored by 90b4f72; log W62mre