UNPKG

alinea

Version:
63 lines (61 loc) 1.86 kB
import "../../chunks/chunk-NZLE2WMY.js"; // src/cli/serve/GitHistory.ts import { JsonLoader } from "alinea/backend/loader/JsonLoader"; import { execGit } from "alinea/backend/util/ExecGit"; import { fileVersions } from "alinea/core/util/EntryFilenames"; import { parseCoAuthoredBy } from "../util/CommitMessage.js"; var encoder = new TextEncoder(); var GitHistory = class { constructor(config, rootDir) { this.config = config; this.rootDir = rootDir; } async revisions(file) { const versions = fileVersions(file); const results = Array(); for (const versioned of versions) { const output = await execGit(this.rootDir, [ "log", "--follow", "--name-status", "--pretty=format:%H%n%at%n%s%n%ae%n%an%n%f", "--", versioned ]); const revisions = output.split("\n\n").filter((entry) => { return entry.includes("\n"); }).map((entry) => { const [ref, timestamp, message, email, name, changedFile, ...rest] = entry.split("\n"); const fileLocation = rest.length ? rest[rest.length - 1].split(" ").pop().trim() : versioned; const user = parseCoAuthoredBy(message) ?? { name, email }; return { ref, createdAt: Number.parseInt(timestamp) * 1e3, description: message, file: fileLocation, user }; }); results.push(...revisions); } return results; } async revisionData(file, ref) { const { config } = this; const data = await execGit(this.rootDir, [ "show", `${ref}:${file}`, "--format=%B" ]); try { return JsonLoader.parse(config.schema, encoder.encode(data)); } catch (cause) { throw new Error(`Failed to parse revision ${ref} of ${file}`, { cause }); } } }; export { GitHistory };