mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-10-26 15:10:36 +08:00
148 lines
3.4 KiB
TypeScript
148 lines
3.4 KiB
TypeScript
import type { z } from "zod";
|
|
export type { Commits, HunkLine, Commit, Diff, CommitHeader, DiffContent };
|
|
|
|
import { array, literal, number, object, optional, string, union } from "zod";
|
|
export { commitHeaderSchema, diffSchema, commitSchema, commitsSchema };
|
|
|
|
const gitPersonSchema = object({
|
|
name: string(),
|
|
email: string(),
|
|
});
|
|
|
|
type CommitHeader = z.infer<typeof commitHeaderSchema>;
|
|
|
|
const commitHeaderSchema = object({
|
|
id: string(),
|
|
author: gitPersonSchema,
|
|
summary: string(),
|
|
description: string(),
|
|
parents: array(string()),
|
|
committer: gitPersonSchema.merge(object({ time: number() })),
|
|
});
|
|
|
|
type AdditionHunkLine = z.infer<typeof additionHunkLineSchema>;
|
|
|
|
const additionHunkLineSchema = object({
|
|
line: string(),
|
|
lineNo: number(),
|
|
type: literal("addition"),
|
|
});
|
|
|
|
type DeletionHunkLine = z.infer<typeof deletionHunkLineSchema>;
|
|
|
|
const deletionHunkLineSchema = object({
|
|
line: string(),
|
|
lineNo: number(),
|
|
type: literal("deletion"),
|
|
});
|
|
|
|
const diffFileSchema = object({
|
|
oid: string(),
|
|
mode: union([
|
|
literal("blob"),
|
|
literal("blobExecutable"),
|
|
literal("tree"),
|
|
literal("link"),
|
|
literal("commit"),
|
|
]),
|
|
});
|
|
|
|
type ContextHunkLine = z.infer<typeof contextHunkLineSchema>;
|
|
|
|
const contextHunkLineSchema = object({
|
|
line: string(),
|
|
lineNoNew: number(),
|
|
lineNoOld: number(),
|
|
type: literal("context"),
|
|
});
|
|
|
|
type HunkLine = AdditionHunkLine | DeletionHunkLine | ContextHunkLine;
|
|
|
|
const hunkLineSchema = union([
|
|
additionHunkLineSchema,
|
|
deletionHunkLineSchema,
|
|
contextHunkLineSchema,
|
|
]);
|
|
|
|
const changesetHunkSchema = object({
|
|
header: string(),
|
|
lines: array(hunkLineSchema),
|
|
});
|
|
|
|
type DiffContent = z.infer<typeof diffContentSchema>;
|
|
|
|
const diffContentSchema = object({
|
|
type: union([literal("plain"), literal("binary"), literal("empty")]),
|
|
hunks: array(changesetHunkSchema),
|
|
eof: union([
|
|
literal("noneMissing"),
|
|
literal("oldMissing"),
|
|
literal("newMissing"),
|
|
literal("bothMissing"),
|
|
]),
|
|
});
|
|
|
|
const diffChangesetSchema = object({
|
|
path: string(),
|
|
diff: diffContentSchema,
|
|
});
|
|
|
|
const diffAddedChangesetSchema = diffChangesetSchema.merge(
|
|
object({ new: diffFileSchema }),
|
|
);
|
|
|
|
const diffDeletedChangesetSchema = diffChangesetSchema.merge(
|
|
object({ old: diffFileSchema }),
|
|
);
|
|
|
|
const diffModifiedChangesetSchema = diffChangesetSchema.merge(
|
|
object({ new: diffFileSchema, old: diffFileSchema }),
|
|
);
|
|
|
|
const diffCopiedChangesetSchema = object({
|
|
newPath: string(),
|
|
oldPath: string(),
|
|
});
|
|
|
|
const diffMovedChangesetSchema = diffCopiedChangesetSchema.merge(
|
|
object({
|
|
old: optional(diffFileSchema),
|
|
new: optional(diffFileSchema),
|
|
diff: optional(diffContentSchema),
|
|
}),
|
|
);
|
|
|
|
type Diff = z.infer<typeof diffSchema>;
|
|
|
|
const diffSchema = object({
|
|
added: array(diffAddedChangesetSchema),
|
|
deleted: array(diffDeletedChangesetSchema),
|
|
modified: array(diffModifiedChangesetSchema),
|
|
moved: array(diffMovedChangesetSchema),
|
|
copied: array(diffCopiedChangesetSchema),
|
|
stats: object({
|
|
filesChanged: number(),
|
|
insertions: number(),
|
|
deletions: number(),
|
|
}),
|
|
});
|
|
|
|
type Commit = z.infer<typeof commitSchema>;
|
|
|
|
const commitSchema = object({
|
|
commit: commitHeaderSchema,
|
|
diff: diffSchema,
|
|
branches: array(string()),
|
|
});
|
|
|
|
type Commits = z.infer<typeof commitsSchema>;
|
|
|
|
const commitsSchema = object({
|
|
commits: array(commitSchema),
|
|
stats: object({
|
|
commits: number(),
|
|
branches: number(),
|
|
contributors: number(),
|
|
}),
|
|
});
|