gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite7af3ecee7af3eceMerge branch 'w52b-approval-followup' into mainGitoriae7af3ece/lib/homepage.hl

5.2 KB

  1. // lib/homepage.hl — THE REPO HOMEPAGE (ticket gitoria#8; the creator, ticket #1): the repo's `/` shows README.md as a page;
  2. // if there is a `$docs` folder, all Markdown files inside it form the homepage instead. Read from the repo's main
  3. // branch (the owner's setting, git.hl defaultBranch) while the page is built on the server (homepageNow).
  4. import { NL } from './util.hl'
  5. import { git, pickDefault, maxTree } from './git.hl'
  6. import { safePath, safeName } from './git-helpers.hl'
  7. import { markdownBlocks } from './markdown.hl'
  8. import { sha256 } from 'hl:crypto'
  9. static maxFiles = 30 // Markdown files of a $docs folder
  10. static maxLines = 5000 // lines of one file
  11. // ---- the homepage cache (gitoria#25) -------------------------------------------------------------
  12. // A README request used to run git ls-tree and git cat-file and parse the Markdown on every single request (about 21 ms of
  13. // the server's time even for a small README). The homepage is a pure function of the repo's commit, so it is kept under
  14. // "slug + commit id of the main branch": a push gives the branch a new commit id, hence a new key — nothing to invalidate,
  15. // a stale entry can only be unused. The hit costs one git call (for-each-ref, which also gives the commit id). BOUNDED: at
  16. // most hpMaxEntries homepages and hpMaxChars characters of source in total (the cost of an entry is its text length); the
  17. // least recently used goes first; a homepage over hpMaxOne characters is built but not kept. Only a found homepage is kept
  18. // (a failed git call must not stick). Flat statics written key by key, like lib/markdown.hl.
  19. static hpMaxEntries = 64
  20. static hpMaxChars = 8000000
  21. static hpMaxOne = 2000000
  22. static hpCache = {} // "slug commit" → the homepage result
  23. static hpSize = {} // "slug commit" → characters of its texts
  24. static hpOrder = [] // keys, least recently used first
  25. static hpChars = [0] // the sum of hpSize (one slot: a static number cannot be written from a lambda)
  26. static hpStats = [0, 0] // [hits, builds]: the gates read it through homepageStats
  27. // how often the cache answered and how often the homepage was built
  28. static homepageStats = () => { return { hits = hpStats[0] builds = hpStats[1] } }
  29. static hpTouch = (key) => {
  30. rest = []
  31. for (k of hpOrder) { if (k != key) { rest.push(k) } }
  32. rest.push(key)
  33. hpOrder = rest
  34. return null
  35. }
  36. static hpKeep = (key, result, chars) => {
  37. if (chars > hpMaxOne) { return null }
  38. while (hpOrder.length > 0 && (hpOrder.length >= hpMaxEntries || hpChars[0] + chars > hpMaxChars)) {
  39. old = hpOrder[0]
  40. hpChars[0] = hpChars[0] - hpSize[old]
  41. delete hpCache[old]
  42. delete hpSize[old]
  43. hpOrder = hpOrder.slice(1)
  44. }
  45. hpCache[key] = result
  46. hpSize[key] = chars
  47. hpChars[0] = hpChars[0] + chars
  48. hpOrder.push(key)
  49. return null
  50. }
  51. // the homepage of a repo, SYNCHRONOUSLY (git() calls back before it returns): what the Readme page's root uses
  52. static homepageNow = (slug, setting) => {
  53. let out = { files = [] found = false truncated = false }
  54. // one git call: every branch with its commit id
  55. names = []
  56. ids = {}
  57. git(slug, ['for-each-ref', '--format=%(objectname) %(refname:short)', 'refs/heads'], 500, (r) => {
  58. if (r.ok) {
  59. for (l of r.lines) {
  60. sp = l.indexOf(' ')
  61. if (sp > 0) {
  62. n = l.slice(sp + 1)
  63. if (safeName(n)) {
  64. names.push(n)
  65. ids[n] = l.slice(0, sp)
  66. }
  67. }
  68. }
  69. }
  70. return null
  71. })
  72. ref = pickDefault(names, setting)
  73. if (ref == null) { return out }
  74. key = slug + ' ' + ids[ref]
  75. hit = hpCache[key]
  76. if (hit != null) {
  77. hpTouch(key)
  78. hpStats[0] = hpStats[0] + 1
  79. return hit
  80. }
  81. hpStats[1] = hpStats[1] + 1
  82. loadHomepage(slug, ids[ref], (result) => {
  83. out = result
  84. return null
  85. })
  86. if (out.found) { hpKeep(key, out, out.chars) }
  87. return out
  88. }
  89. static isMarkdown = (path) => { return path.toLowerCase().endsWith('.md') }
  90. // the files that make the homepage: [] (none), or the paths in tree order
  91. static homepagePaths = (tree) => {
  92. docs = []
  93. let readme = null
  94. for (path of tree) {
  95. if (safePath(path)) {
  96. if (path.startsWith('$docs/') && isMarkdown(path) && docs.length < maxFiles) { docs.push(path) }
  97. if (readme == null && !path.includes('/') && path.toLowerCase() == 'readme.md') { readme = path }
  98. }
  99. }
  100. if (docs.length > 0) { return docs }
  101. return readme != null ? [readme] : []
  102. }
  103. // the homepage of a branch (its ref) → done({ files: [{ path, isDocs, blocks }], found, truncated })
  104. static loadHomepage = (slug, ref, done) => {
  105. if (ref == null) {
  106. done({ files = [] found = false truncated = false })
  107. return null
  108. }
  109. git(slug, ['ls-tree', '-r', '--name-only', ref], maxTree, (t) => {
  110. paths = t.ok ? homepagePaths(t.lines) : []
  111. let files = []
  112. let truncated = false
  113. let chars = 0
  114. next = (i) => {
  115. if (i >= paths.length) {
  116. done({ files = files found = files.length > 0 truncated = truncated chars = chars })
  117. return null
  118. }
  119. git(slug, ['cat-file', 'blob', ref + ':' + paths[i]], maxLines + 1, (r) => {
  120. if (r.ok) {
  121. if (r.lines.length > maxLines) { truncated = true }
  122. lines = r.lines.length > maxLines ? r.lines.slice(0, maxLines) : r.lines
  123. text = lines.join(NL)
  124. chars = chars + text.length
  125. files.push({ path = paths[i] isDocs = paths[i].startsWith('$docs/') hash = sha256(text) blocks = markdownBlocks(text) })
  126. }
  127. next(i + 1)
  128. return null
  129. })
  130. return null
  131. }
  132. next(0)
  133. return null
  134. })
  135. }

Branches

Latest commits

  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
  • 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
  • 83f0c953Report person approval verificationmre
  • 3ae94b86Record browser failures and verify owned tab cleanupmre
  • 54eec687Gate person approvals and merge alternativesmre
  • 12b08829Add person approvals for pull request headsmre
  • 55c84779Merge branch 'gp-gate-ports' into mainGitoria
  • 57af42bbREADME: point to deploy.sh for the port listmre
  • d63375d2README: gate commands with explicit portsmre
  • feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
  • 6cc1f943Merge branch 'records-1112' into mainGitoria
  • 9448c438|||PR records: LOG, missions and reports since 11-09mre
  • 6dc08a8eMerge branch 'p51-public-pr-reads' into mainGitoria