mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 02:00:31 +08:00
Refactor commit and diff types, updates outdated diff types
- Adds support for modified moved files see https://github.com/radicle-dev/radicle-git/pull/141 Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
+106
-102
@@ -1,171 +1,175 @@
|
||||
import type { ZodSchema } from "zod";
|
||||
import { array, literal, number, object, string, union } from "zod";
|
||||
import type { z } from "zod";
|
||||
import { array, literal, number, object, optional, string, union } from "zod";
|
||||
|
||||
interface GitPerson {
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
export { commitHeaderSchema, diffSchema, commitSchema, commitsSchema };
|
||||
|
||||
export type {
|
||||
Commits,
|
||||
HunkLine,
|
||||
Commit,
|
||||
DiffFile,
|
||||
Diff,
|
||||
DiffCopiedChangeset,
|
||||
DiffMovedChangeset,
|
||||
CommitHeader,
|
||||
DiffAddedChangeset,
|
||||
DiffDeletedChangeset,
|
||||
DiffModifiedChangeset,
|
||||
DiffContent,
|
||||
};
|
||||
|
||||
const gitPersonSchema = object({
|
||||
name: string(),
|
||||
email: string(),
|
||||
}) satisfies ZodSchema<GitPerson>;
|
||||
});
|
||||
|
||||
export interface CommitHeader {
|
||||
id: string;
|
||||
author: GitPerson;
|
||||
summary: string;
|
||||
description: string;
|
||||
parents: string[];
|
||||
committer: GitPerson & { time: number };
|
||||
}
|
||||
type CommitHeader = z.infer<typeof commitHeaderSchema>;
|
||||
|
||||
export const commitHeaderSchema = object({
|
||||
const commitHeaderSchema = object({
|
||||
id: string(),
|
||||
author: gitPersonSchema,
|
||||
summary: string(),
|
||||
description: string(),
|
||||
parents: array(string()),
|
||||
committer: gitPersonSchema.merge(object({ time: number() })),
|
||||
}) satisfies ZodSchema<CommitHeader>;
|
||||
});
|
||||
|
||||
interface AdditionHunkLine {
|
||||
line: string;
|
||||
lineNo: number;
|
||||
type: "addition";
|
||||
}
|
||||
type AdditionHunkLine = z.infer<typeof additionHunkLineSchema>;
|
||||
|
||||
const additionHunkLineSchema = object({
|
||||
line: string(),
|
||||
lineNo: number(),
|
||||
type: literal("addition"),
|
||||
}) satisfies ZodSchema<AdditionHunkLine>;
|
||||
});
|
||||
|
||||
interface DeletionHunkLine {
|
||||
line: string;
|
||||
lineNo: number;
|
||||
type: "deletion";
|
||||
}
|
||||
type DeletionHunkLine = z.infer<typeof deletionHunkLineSchema>;
|
||||
|
||||
const deletionHunkLineSchema = object({
|
||||
line: string(),
|
||||
lineNo: number(),
|
||||
type: literal("deletion"),
|
||||
}) satisfies ZodSchema<DeletionHunkLine>;
|
||||
});
|
||||
|
||||
interface ContextHunkLine {
|
||||
line: string;
|
||||
lineNoNew: number;
|
||||
lineNoOld: number;
|
||||
type: "context";
|
||||
}
|
||||
type DiffFile = z.infer<typeof diffFileSchema>;
|
||||
|
||||
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"),
|
||||
}) satisfies ZodSchema<ContextHunkLine>;
|
||||
});
|
||||
|
||||
export type HunkLine = AdditionHunkLine | DeletionHunkLine | ContextHunkLine;
|
||||
type HunkLine = AdditionHunkLine | DeletionHunkLine | ContextHunkLine;
|
||||
|
||||
const hunkLineSchema = union([
|
||||
additionHunkLineSchema,
|
||||
deletionHunkLineSchema,
|
||||
contextHunkLineSchema,
|
||||
]) satisfies ZodSchema<HunkLine>;
|
||||
|
||||
interface ChangesetHunk {
|
||||
header: string;
|
||||
lines: HunkLine[];
|
||||
}
|
||||
]);
|
||||
|
||||
const changesetHunkSchema = object({
|
||||
header: string(),
|
||||
lines: array(hunkLineSchema),
|
||||
}) satisfies ZodSchema<ChangesetHunk>;
|
||||
});
|
||||
|
||||
export interface DiffAddedDeletedModifiedChangeset {
|
||||
path: string;
|
||||
diff: {
|
||||
type: "plain" | "binary" | "empty";
|
||||
hunks: ChangesetHunk[];
|
||||
eof: "noneMissing" | "oldMissing" | "newMissing" | "bothMissing";
|
||||
};
|
||||
}
|
||||
type DiffContent = z.infer<typeof diffContentSchema>;
|
||||
|
||||
const diffAddedDeletedModifiedChangesetSchema = object({
|
||||
const diffContentSchema = object({
|
||||
type: union([literal("plain"), literal("binary"), literal("empty")]),
|
||||
hunks: array(changesetHunkSchema),
|
||||
eof: union([
|
||||
literal("noneMissing"),
|
||||
literal("oldMissing"),
|
||||
literal("newMissing"),
|
||||
literal("bothMissing"),
|
||||
]),
|
||||
});
|
||||
|
||||
type DiffAddedChangeset = z.infer<typeof diffAddedChangesetSchema>;
|
||||
|
||||
const diffAddedChangesetSchema = object({
|
||||
path: string(),
|
||||
diff: object({
|
||||
type: union([literal("plain"), literal("binary"), literal("empty")]),
|
||||
hunks: array(changesetHunkSchema),
|
||||
eof: union([
|
||||
literal("noneMissing"),
|
||||
literal("oldMissing"),
|
||||
literal("newMissing"),
|
||||
literal("bothMissing"),
|
||||
]),
|
||||
}),
|
||||
}) satisfies ZodSchema<DiffAddedDeletedModifiedChangeset>;
|
||||
diff: diffContentSchema,
|
||||
new: diffFileSchema,
|
||||
});
|
||||
|
||||
export interface DiffCopiedMovedChangeset {
|
||||
newPath: string;
|
||||
oldPath: string;
|
||||
}
|
||||
type DiffDeletedChangeset = z.infer<typeof diffDeletedChangesetSchema>;
|
||||
|
||||
const diffCopiedMovedChangesetSchema = object({
|
||||
const diffDeletedChangesetSchema = object({
|
||||
path: string(),
|
||||
diff: diffContentSchema,
|
||||
old: diffFileSchema,
|
||||
});
|
||||
|
||||
type DiffModifiedChangeset = z.infer<typeof diffModifiedChangesetSchema>;
|
||||
|
||||
const diffModifiedChangesetSchema = object({
|
||||
path: string(),
|
||||
diff: diffContentSchema,
|
||||
new: diffFileSchema,
|
||||
old: diffFileSchema,
|
||||
});
|
||||
|
||||
type DiffCopiedChangeset = z.infer<typeof diffCopiedChangesetSchema>;
|
||||
|
||||
const diffCopiedChangesetSchema = object({
|
||||
newPath: string(),
|
||||
oldPath: string(),
|
||||
}) satisfies ZodSchema<DiffCopiedMovedChangeset>;
|
||||
});
|
||||
|
||||
export interface Diff {
|
||||
added: DiffAddedDeletedModifiedChangeset[];
|
||||
deleted: DiffAddedDeletedModifiedChangeset[];
|
||||
moved: DiffCopiedMovedChangeset[];
|
||||
copied: DiffCopiedMovedChangeset[];
|
||||
modified: DiffAddedDeletedModifiedChangeset[];
|
||||
stats: {
|
||||
filesChanged: number;
|
||||
insertions: number;
|
||||
deletions: number;
|
||||
};
|
||||
}
|
||||
type DiffMovedChangeset = z.infer<typeof diffMovedChangesetSchema>;
|
||||
|
||||
export const diffSchema = object({
|
||||
added: array(diffAddedDeletedModifiedChangesetSchema),
|
||||
deleted: array(diffAddedDeletedModifiedChangesetSchema),
|
||||
moved: array(diffCopiedMovedChangesetSchema),
|
||||
copied: array(diffCopiedMovedChangesetSchema),
|
||||
modified: array(diffAddedDeletedModifiedChangesetSchema),
|
||||
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(),
|
||||
}),
|
||||
}) satisfies ZodSchema<Diff>;
|
||||
});
|
||||
|
||||
export interface Commit {
|
||||
commit: CommitHeader;
|
||||
diff: Diff;
|
||||
branches: string[];
|
||||
}
|
||||
type Commit = z.infer<typeof commitSchema>;
|
||||
|
||||
export const commitSchema = object({
|
||||
const commitSchema = object({
|
||||
commit: commitHeaderSchema,
|
||||
diff: diffSchema,
|
||||
branches: array(string()),
|
||||
}) satisfies ZodSchema<Commit>;
|
||||
});
|
||||
|
||||
export interface Commits {
|
||||
commits: Commit[];
|
||||
stats: { commits: number; branches: number; contributors: number };
|
||||
}
|
||||
type Commits = z.infer<typeof commitsSchema>;
|
||||
|
||||
export const commitsSchema = object({
|
||||
const commitsSchema = object({
|
||||
commits: array(commitSchema),
|
||||
stats: object({
|
||||
commits: number(),
|
||||
branches: number(),
|
||||
contributors: number(),
|
||||
}),
|
||||
}) satisfies ZodSchema<Commits>;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user