anbot

425ca29 / repository.mjs

View file at this commit

diff --git a/repository.mjs b/repository.mjs
index d40444a..0cabe34 100644
--- a/repository.mjs
+++ b/repository.mjs
@@ -5,6 +5,7 @@ import {
   parseJson, persist, readOptional, readRecord, recordPath, recoverBundle,
   requireThat, storeBundle, writeRecord
 } from './storage.mjs'
+import { verifyPushAnnouncement } from './social.mjs'
 
 export const gitEnv = Object.fromEntries(
   Object.entries(process.env).filter(([name]) => !name.startsWith('GIT_'))
@@ -91,7 +92,7 @@ export function checkoutRoot(source) {
   return git(source, 'rev-parse', '--show-toplevel')
 }
 
-export async function publish(root, identity, source, branch, name, expectedUpdate) {
+export async function publish(root, identity, source, branch, name, expectedUpdate, push) {
   const ref = `refs/heads/${branch}`
   git(source, 'check-ref-format', ref)
   requireThat(git(source, 'rev-parse', '--show-object-format') === 'sha1',
@@ -117,7 +118,8 @@ export async function publish(root, identity, source, branch, name, expectedUpda
       await writeRecord(root, identity, { anbot: 1, type: 'git-repo', name })
     const updateId = await writeRecord(root, identity, {
       anbot: 1, type: 'git-update', repo: { format: 'anproto', id: repoId },
-      ref, objectFormat: 'sha1', commit, bundle: { format: 'andfs-v1', id: bundleId }
+      ref, objectFormat: 'sha1', commit, bundle: { format: 'andfs-v1', id: bundleId },
+      previousUpdate: previous?.updateId, push
     })
     const selection = { repoId, ref, updateId }
     await materialize(root, identity, selection, work)
@@ -134,7 +136,7 @@ export async function publish(root, identity, source, branch, name, expectedUpda
 export async function stageReceive(root, identity, directory) {
   const selection = readState(root, 'selected.json')
   const bare = await materialize(root, identity, selection, directory)
-  const { ref, updateId } = selection
+  const { ref, updateId, repoId } = selection
   const before = git(bare, 'rev-parse', ref)
   const hooks = join(bare, 'hooks')
   mkdirSync(hooks, { recursive: true })
@@ -148,14 +150,43 @@ done
 `, { mode: 0o700 })
   return {
     bare, hooks, ref,
-    async publish() {
-      if (git(bare, 'rev-parse', ref) !== before) {
-        await publish(root, identity, bare, ref.slice('refs/heads/'.length), undefined, updateId)
-      }
+    async publish({ identity: author, announcement }) {
+      const commit = git(bare, 'rev-parse', ref)
+      if (commit === before) return
+      const intent = await verifyPushAnnouncement(announcement, author, repoId)
+      requireThat(intent.ref === ref && intent.commit === commit, 'Announcement does not match pushed ref and commit')
+      const range = `${before}..${commit}`
+      const count = Number(git(bare, 'rev-list', '--count', range))
+      const commits = git(bare, 'rev-list', '--max-count=20', range).split('\n').map(hash => ({
+        hash, subject: git(bare, 'show', '-s', '--format=%<(240,trunc)%s', hash)
+      }))
+      await publish(root, identity, bare, ref.slice('refs/heads/'.length), undefined, updateId,
+        { announcement, count, commits })
     }
   }
 }
 
+export async function readPushAnnouncements(root, identity) {
+  const { repoId, updateId } = readState(root, 'selected.json')
+  const seen = new Set()
+  const posts = []
+  for (let id = updateId; id !== undefined;) {
+    requireThat(!seen.has(id), 'Cyclic Git update history')
+    seen.add(id)
+    const update = await readRecord(root, identity.author, id)
+    requireThat(update?.type === 'git-update' && update.repo?.id === repoId, 'Invalid Git update history')
+    if (update.push) {
+      const { announcement, count, commits } = update.push
+      const author = announcement.signature.slice(0, 44)
+      const post = await verifyPushAnnouncement(announcement, author, repoId)
+      requireThat(post.ref === update.ref && post.commit === update.commit, 'Mismatched Git announcement')
+      posts.push({ ...post, updateId: id, count, commits })
+    }
+    id = update.previousUpdate
+  }
+  return posts
+}
+
 export async function selectedRepository(root, identity, verified) {
   const selection = readState(root, 'selected.json')
   requireThat(selection, 'Publish a repository before serving it')

Git ยท Social feed