anbot

f432b40 / repository.mjs

13094 bytes

import { execFileSync } from 'node:child_process'
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import {
  parseJson, persist, readOptional, readRecord, readRecordAuthor, 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_'))
)
Object.assign(gitEnv, {
  GIT_CONFIG_NOSYSTEM: '1',
  GIT_CONFIG_GLOBAL: '/dev/null',
  GIT_NO_REPLACE_OBJECTS: '1',
  GIT_TERMINAL_PROMPT: '0'
})
export const gitOptions = ['-c', 'core.hooksPath=/dev/null']

function git(cwd, ...args) {
  try {
    return execFileSync('git', [...gitOptions, '-C', cwd, ...args], {
      env: gitEnv, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']
    }).trimEnd()
  } catch (error) {
    throw new Error(error.stderr?.trim() || error.message)
  }
}

function gitBuffer(cwd, ...args) {
  try {
    return execFileSync('git', [...gitOptions, '-C', cwd, ...args], {
      env: gitEnv, stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 8 * 1024 * 1024
    })
  } catch (error) {
    throw new Error(error.stderr?.toString().trim() || error.message)
  }
}

function gitOrNull(cwd, ...args) {
  try {
    return git(cwd, ...args)
  } catch {
    return null
  }
}

function initBare(directory, repository) {
  git(directory, 'init', '--bare', '--template=', '--object-format=sha1', repository)
}

function readState(root, name) {
  const bytes = readOptional(join(root, name))
  return bytes && parseJson(bytes)
}

async function validateRepository(root, repoId) {
  const author = await readRecordAuthor(root, repoId)
  const repo = await readRecord(root, author, repoId)
  requireThat(repo?.anbot === 1 && repo.type === 'git-repo' &&
    typeof repo.name === 'string', 'Invalid repository record')
  return author
}

async function materialize(root, selection, directory) {
  const author = await validateRepository(root, selection.repoId)
  const update = await readRecord(root, author, selection.updateId)
  requireThat(update?.anbot === 1 && update.type === 'git-update' &&
    update.repo?.format === 'anproto' && update.repo.id === selection.repoId &&
    update.bundle?.format === 'andfs-v1' && update.objectFormat === 'sha1' &&
    typeof update.commit === 'string' && /^[0-9a-f]{40}$/.test(update.commit) &&
    typeof update.ref === 'string' && update.ref.startsWith('refs/heads/') &&
    selection.ref === update.ref, 'Invalid update record')

  const bare = join(directory, 'serve.git')
  const bundle = join(directory, 'recover.bundle')
  await recoverBundle(root, update.bundle.id, bundle)
  initBare(directory, bare)
  git(bare, 'check-ref-format', update.ref)
  // Verification in an empty repository rejects prerequisites.
  git(bare, 'bundle', 'verify', bundle)
  requireThat(git(bare, 'bundle', 'list-heads', bundle) ===
    `${update.commit} ${update.ref}`, 'Bundle heads do not match the signed update')
  git(bare, 'bundle', 'unbundle', bundle)
  requireThat(git(bare, 'cat-file', '-t', update.commit) === 'commit', 'Expected a commit')
  git(bare, 'update-ref', update.ref, update.commit)
  git(bare, 'symbolic-ref', 'HEAD', update.ref)
  git(bare, 'fsck', '--full', '--no-dangling')
  return bare
}

export function checkoutRoot(source) {
  return git(source, 'rev-parse', '--show-toplevel')
}

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',
    'Only SHA-1 Git repositories are supported')
  const commit = git(source, 'show-ref', '--verify', '--hash', ref)
  const previous = readState(root, 'selected.json')
  requireThat(!expectedUpdate || previous?.updateId === expectedUpdate, 'Selected update changed. Fetch and retry.')
  requireThat(!previous || previous.ref === ref, `This node publishes ${previous?.ref}`)

  const work = mkdtempSync(join(root, '.publish-'))
  try {
    // Bundle from a private ref so the source branch can move once resolved.
    const snapshot = join(work, 'snapshot.git')
    initBare(work, snapshot)
    git(snapshot, 'fetch', '--no-tags', '--no-write-fetch-head', source, commit)
    requireThat(git(snapshot, 'cat-file', '-t', commit) === 'commit', 'Expected a commit')
    git(snapshot, 'update-ref', ref, commit)
    const bundle = join(work, 'source.bundle')
    git(snapshot, 'bundle', 'create', bundle, ref)
    const bundleId = await storeBundle(root, bundle)

    const repoId = previous?.repoId ??
      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 },
      previous: previous?.updateId ?? null, push
    })
    const selection = { repoId, ref, updateId }
    await materialize(root, selection, work)
    requireThat(!expectedUpdate || readState(root, 'selected.json')?.updateId === expectedUpdate,
      'Selected update changed. Fetch and retry.')
    const installed = persist(root, 'selected.json', JSON.stringify(selection), Boolean(previous))
    requireThat(installed, 'Another process made the first publication. Retry with its branch.')
    return { repoId, updateId, commit }
  } finally {
    rmSync(work, { recursive: true, force: true })
  }
}

export async function stageReceive(root, identity, directory) {
  const selection = readState(root, 'selected.json')
  const bare = await materialize(root, selection, directory)
  const { ref, updateId, repoId } = selection
  const before = git(bare, 'rev-parse', ref)
  const hooks = join(bare, 'hooks')
  mkdirSync(hooks, { recursive: true })
  writeFileSync(join(hooks, 'pre-receive'), `#!/bin/sh
while read -r old new ref; do
  if [ "$ref" != "$ANBOT_RECEIVE_REF" ]; then
    echo "Only $ANBOT_RECEIVE_REF may be pushed" >&2
    exit 1
  fi
done
`, { mode: 0o700 })
  return {
    bare, hooks, ref,
    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) {
  const { repoId, updateId } = readState(root, 'selected.json')
  const publisher = await readRecordAuthor(root, repoId)
  const seen = new Set()
  const posts = []
  for (let id = updateId; id != null;) {
    requireThat(!seen.has(id), 'Cyclic Git update history')
    seen.add(id)
    const update = await readRecord(root, publisher, 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.previous ?? update.previousUpdate
  }
  return posts
}

export async function selectedRepository(root, verified) {
  const selection = readState(root, 'selected.json')
  requireThat(selection, 'Publish a repository before serving it')
  const { repoId, updateId } = selection
  const key = JSON.stringify([repoId, selection.ref, updateId])
  let bare = verified.get(key)
  if (!bare || !existsSync(bare)) {
    const cache = join(root, 'cache')
    mkdirSync(cache, { mode: 0o700, recursive: true })
    const work = mkdtempSync(join(cache, 'publication-'))
    try {
      bare = await materialize(root, selection, work)
      rmSync(join(work, 'recover.bundle'))
      verified.set(key, bare)
    } catch (error) {
      rmSync(work, { recursive: true, force: true })
      throw error
    }
  }
  return { repoId, ref: selection.ref, updateId, bare }
}

// Verify a selection through the full materialize path in a disposable
// directory, without keeping the bare repository. Sync uses this to check
// a pulled update before advancing the selection.
export async function verifySelection(root, selection) {
  const directory = mkdtempSync(join(root, '.verify-'))
  try {
    await materialize(root, selection, directory)
  } finally {
    rmSync(directory, { recursive: true, force: true })
  }
}

export async function repositoryName(root, repoId) {
  const author = await readRecordAuthor(root, repoId)
  const repo = await readRecord(root, author, repoId)
  requireThat(repo?.anbot === 1 && repo.type === 'git-repo' &&
    typeof repo.name === 'string', 'Invalid repository record')
  return repo.name
}

export function listCommits(bare, ref, limit) {
  requireThat(Number.isSafeInteger(limit) && limit > 0 && limit <= 100, 'Invalid commit limit')
  const output = git(bare, 'log', `-n${limit}`, '--format=%H%x00%an%x00%aI%x00%s%x1e', ref)
  return output.split('\x1e').map(record => record.replace(/^\n/, '')).filter(Boolean).map(record => {
    const [hash, author, date, subject] = record.split('\x00')
    return { hash, author, date, subject }
  })
}

export function readCommit(bare, hash) {
  requireThat(/^[0-9a-f]{40}$/.test(hash), 'Invalid commit')
  if (gitOrNull(bare, 'cat-file', '-t', hash) !== 'commit') return null
  const output = git(bare, 'log', '-1', '--format=%H%x00%an%x00%ae%x00%aI%x00%B', hash)
  const [found, author, email, date, ...rest] = output.split('\x00')
  return { hash: found, author, email, date, message: rest.join('\x00') }
}

export function commitFiles(bare, hash) {
  const output = git(bare, 'show', '--numstat', '-z', '--format=', hash)
  const tokens = output.split('\0').filter(Boolean)
  const files = []
  for (let index = 0; index < tokens.length; index++) {
    const match = /^(\d+|-)\t(\d+|-)\t(.*)$/.exec(tokens[index])
    if (!match) continue
    let path = match[3]
    if (!path) {
      // A rename with -z carries the old and new paths as separate tokens.
      index += 2
      path = tokens[index]
      if (!path) continue
    }
    const count = value => value === '-' ? null : Number(value)
    files.push({ path, added: count(match[1]), removed: count(match[2]) })
  }
  return files
}

const maxDiffShown = 262144

export function commitDiff(bare, hash, path) {
  const bytes = gitBuffer(bare, 'show', '--format=', '--no-ext-diff', hash, '--', path)
  if (bytes.length === 0) return null
  if (bytes.length > maxDiffShown) {
    return {
      truncated: true, shownBytes: maxDiffShown,
      text: new TextDecoder().decode(bytes.subarray(0, maxDiffShown))
    }
  }
  return { text: new TextDecoder().decode(bytes) }
}

export function blobExists(bare, ref, path) {
  return gitOrNull(bare, 'cat-file', '-t', `${ref}:${path}`) === 'blob'
}

export function listTree(bare, ref, path) {
  if (path) {
    if (gitOrNull(bare, 'cat-file', '-t', `${ref}:${path}`) !== 'tree') return null
  }
  const spec = path ? `${ref}:${path}` : ref
  const output = gitOrNull(bare, 'ls-tree', '-l', '-z', spec)
  if (output === null) return null
  return output.split('\0').filter(Boolean).map(record => {
    const tab = record.indexOf('\t')
    const [mode, type, oid, size] = record.slice(0, tab).split(' ').filter(Boolean)
    return { mode, type, oid, size: size === '-' ? null : Number(size), name: record.slice(tab + 1) }
  })
}

const maxBlobRead = 4 * 1024 * 1024
const maxBlobShown = 262144

export function readBlob(bare, ref, path) {
  const spec = `${ref}:${path}`
  if (gitOrNull(bare, 'cat-file', '-t', spec) !== 'blob') return null
  const size = Number(git(bare, 'cat-file', '-s', spec))
  if (size > maxBlobRead) return { size, tooLarge: true }
  const bytes = gitBuffer(bare, 'cat-file', '-p', spec)
  requireThat(bytes.length === size, 'Unexpected blob size')
  if (bytes.includes(0)) return { size, binary: true }
  if (size > maxBlobShown) {
    return {
      size, truncated: true, shownBytes: maxBlobShown,
      text: new TextDecoder().decode(bytes.subarray(0, maxBlobShown))
    }
  }
  try {
    return { size, text: new TextDecoder('utf-8', { fatal: true }).decode(bytes) }
  } catch {
    return { size, binary: true }
  }
}

export function clonePath(repoId) {
  return `/git/${recordPath(repoId)}.git`
}

Git ยท Social feed