anbot

f432b40 / anbot.mjs

View file at this commit

diff --git a/anbot.mjs b/anbot.mjs
index e87bb04..234cb00 100644
--- a/anbot.mjs
+++ b/anbot.mjs
@@ -5,8 +5,9 @@ import { createServer } from 'node:http'
 import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path'
 import { parseArgs } from 'node:util'
 import {
-  checkoutRoot, clonePath, commitStat, gitEnv, gitOptions, listCommits, listTree,
-  publish, readBlob, readCommit, readPushAnnouncements, repositoryName, selectedRepository, verifySelection
+  checkoutRoot, clonePath, blobExists, commitDiff, commitFiles, gitEnv, gitOptions,
+  listCommits, listTree, publish, readBlob, readCommit, readPushAnnouncements,
+  repositoryName, selectedRepository, verifySelection
 } from './repository.mjs'
 import {
   createFsStore, initializeStore, listVerifiedPosts, listVerifiedRecords, loadIdentity,
@@ -19,7 +20,7 @@ import { pushAuth, pushWithIdentity } from './push-auth.mjs'
 import { receivePush } from './receive.mjs'
 import { publishPost, readFeed } from './social-store.mjs'
 import { pull } from './sync.mjs'
-import { blobPage, commitPage, feedPage, indexPage, notFoundPage, treePage } from './webui.mjs'
+import { blobPage, commitPage, diffPage, feedPage, indexPage, notFoundPage, treePage } from './webui.mjs'
 
 const feedScript = readFileSync(new URL('./feed-live.js', import.meta.url))
 
@@ -222,6 +223,16 @@ function matchWebRoute(pathname) {
     const path = parseWebPath(pathname.slice('/blob/'.length))
     return { kind: 'blob', path: path === '' ? null : path }
   }
+  const commitDiff = /^\/commit\/([0-9a-f]{40})\/diff\/(.+)$/.exec(pathname)
+  if (commitDiff) {
+    const path = parseWebPath(commitDiff[2])
+    return { kind: 'diff', hash: commitDiff[1], path: path === '' ? null : path }
+  }
+  const commitBlob = /^\/commit\/([0-9a-f]{40})\/blob\/(.+)$/.exec(pathname)
+  if (commitBlob) {
+    const path = parseWebPath(commitBlob[2])
+    return { kind: 'commitBlob', hash: commitBlob[1], path: path === '' ? null : path }
+  }
   const commit = /^\/commit\/([0-9a-f]{40})$/.exec(pathname)
   if (commit) return { kind: 'commit', hash: commit[1] }
   return null
@@ -325,13 +336,37 @@ async function runServer(root, identity, host, port, baseUrl, allowlist, pushOri
             serveHtml(response, 200, blobPage({ name, path: route.path, ...blob }))
             return
           }
+          if (route.kind === 'diff' || route.kind === 'commitBlob') {
+            const commit = readCommit(selected.bare, route.hash)
+            if (!commit) {
+              serveHtml(response, 404, notFoundPage(name, `Commit ${route.hash}`))
+              return
+            }
+            if (route.kind === 'diff') {
+              const diff = commitDiff(selected.bare, route.hash, route.path)
+              if (!diff) {
+                serveHtml(response, 404, notFoundPage(name, `File ${route.path} in commit ${route.hash}`))
+                return
+              }
+              const hasBlob = blobExists(selected.bare, route.hash, route.path)
+              serveHtml(response, 200, diffPage({ name, commit, path: route.path, ...diff, hasBlob }))
+              return
+            }
+            const blob = readBlob(selected.bare, route.hash, route.path)
+            if (!blob) {
+              serveHtml(response, 404, notFoundPage(name, `File ${route.path} in commit ${route.hash}`))
+              return
+            }
+            serveHtml(response, 200, blobPage({ name, path: route.path, commit, ...blob }))
+            return
+          }
           const commit = readCommit(selected.bare, route.hash)
           if (!commit) {
             serveHtml(response, 404, notFoundPage(name, `Commit ${route.hash}`))
             return
           }
           serveHtml(response, 200, commitPage({
-            name, commit, stat: commitStat(selected.bare, route.hash)
+            name, commit, files: commitFiles(selected.bare, route.hash)
           }))
         } catch (error) {
           console.error(error.message)

Git ยท Social feed