gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4a2d71254a2d7125initial commitmre4a2d7125/components/codebrowser.hl

5.1 KB

  1. // components/codebrowser.hl — THE CODE BROWSER (ticket #9, gitoria#16), composed into the three code pages:
  2. // code.hl (/code, /code/<path>: the main branch), branch.hl (/branch/<name>[/<path>]), commit.hl
  3. // (/commit/<id>[/<path>]); `kind` ('code' | 'branch' | 'commit') and `rest` (the part after /<kind>/) come from the
  4. // page. Rendered on the SERVER with its content: the repo from the request's host, the tree / file / branches /
  5. // commits from git (git.hl browseNow — hl:proc run() waits, hybriel#80) — in the first HTML and in every hl:web navigation.
  6. import { repoAt, repoBySlug, setMainBranch, ownsRepo } from '../repos.hl'
  7. import { browseNow, branchNames } from '../git.hl'
  8. import { slugOfHost, userOfSession } from '../users.hl'
  9. host = null
  10. session = null
  11. kind = 'code'
  12. rest = ''
  13. slug = slugOfHost(host)
  14. repo = repoAt(slug)
  15. hasRepo = repo != null
  16. codeData = repo != null ? browseNow(slug, kind, rest, repo.branch, ownsRepo(slug, session)) : null
  17. codeShown = codeData != null && !codeData.isMessage
  18. codeMessage = codeData != null && codeData.isMessage
  19. codeText = codeData != null && codeData.isMessage ? codeData.message : ''
  20. codeError = ''
  21. View {
  22. codeView {
  23. if (hasRepo) {
  24. if (codeMessage) { p { id = "codemessage" class = "empty" codeText } }
  25. if (codeShown) {
  26. codeBrowser {
  27. codeHead {
  28. span { id = "codekind" class = "muted" codeData.kindLabel }
  29. strong { id = "coderef" codeData.refLabel }
  30. latestCommit {
  31. a { id = "commitlink" href = codeData.commitHref codeData.short }
  32. span { id = "commitsubject" codeData.subject }
  33. span { class = "muted" codeData.author }
  34. time { codeData.date }
  35. }
  36. }
  37. pathCrumbs { id = "crumbs"
  38. for (c of codeData.crumbs) {
  39. if (c.linked) { a { href = c.href c.name } span { class = "muted" "/" } }
  40. if (c.last) { strong { c.name } }
  41. }
  42. }
  43. if (codeData.isTree) {
  44. if (codeData.noEntries) { p { id = "noentries" class = "empty" "This folder is empty." } }
  45. ul { id = "entries"
  46. for (e of codeData.entries) {
  47. li {
  48. if (e.isDir) { a { class = "dir" href = e.href e.name } }
  49. if (e.isFile) { a { class = "file" href = e.href e.name } }
  50. if (e.isOther) { span { class = "file" e.name } }
  51. span { class = "muted" e.size }
  52. }
  53. }
  54. }
  55. }
  56. if (codeData.isFile) {
  57. codeFile { id = "codefile"
  58. p { class = "muted" codeData.fileSize }
  59. if (codeData.binary) { p { id = "binary" class = "empty" "This file is not text (or not UTF-8) — not shown." } }
  60. if (codeData.tooBig) { p { id = "toobig" class = "empty" "This file is too large to show here." } }
  61. ol { id = "lines"
  62. for (ln of codeData.lines) { li { code { ln.text } } }
  63. }
  64. if (codeData.truncated) { p { id = "linestruncated" class = "muted" "Only the first lines are shown." } }
  65. }
  66. }
  67. codeSide {
  68. section { id = "branches"
  69. h2 { "Branches" }
  70. ul {
  71. for (b of codeData.branches) {
  72. li {
  73. if (b.isCurrent) { strong { b.name } }
  74. if (b.notCurrent) { a { href = b.href b.name } }
  75. if (b.isDefault) { span { class = "muted" "main branch" } }
  76. if (b.canSet) { button { class = "quiet setmain" type = "button" value = b.name "Make main" on click(e) { emit setMain(e.target.value) } } }
  77. }
  78. }
  79. }
  80. p { id = "codeerror" class = "message" codeError }
  81. }
  82. section { id = "commits"
  83. h2 { "Latest commits" }
  84. ul {
  85. for (c of codeData.commits) {
  86. li {
  87. a { href = c.href c.short }
  88. span { c.subject }
  89. span { class = "muted" c.author }
  90. time { c.date }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. // "Make main" (the owner): the face answers with the same view, read again with the new main branch
  102. on setMain(name) {
  103. codeError = ''
  104. let r = emit server gitoriaSetBranch(slug, name, kind, rest)
  105. if (r == null || r.error != null) {
  106. codeError = r != null ? r.error : 'could not change the main branch'
  107. return null
  108. }
  109. codeData = r.result
  110. codeShown = !r.result.isMessage
  111. codeMessage = r.result.isMessage
  112. codeText = r.result.isMessage ? r.result.message : ''
  113. }
  114. on server gitoriaSetBranch(slugOf, name, kindOf, restOf, session) {
  115. if (session == null || repoAt(slugOf) == null) { return { error = 'could not change the main branch' } }
  116. if (kindOf != 'code' && kindOf != 'branch' && kindOf != 'commit') { return { error = 'could not change the main branch' } }
  117. if (restOf == null || hlTypeName(restOf) != 'String' || restOf.length > 500 || name == null || hlTypeName(name) != 'String') { return { error = 'could not change the main branch' } }
  118. let u = userOfSession(session)
  119. if (u == null || !ownsRepo(slugOf, session)) { return { error = 'only the owner can change the main branch' } }
  120. let known = []
  121. branchNames(slugOf, (names) => {
  122. known = names
  123. return null
  124. })
  125. if (!known.includes(name)) { return { error = 'no such branch' } }
  126. let r = setMainBranch(u.id, slugOf, name)
  127. if (r.error != null) { return { error = r.error } }
  128. return { result = browseNow(slugOf, kindOf, restOf, name, true) }
  129. }

Branches

Latest commits

  • 4a2d7125initial commitmre