Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor commit and diff types, updates outdated diff types #1024

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions httpd-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import type {
Commit,
CommitHeader,
Diff,
DiffAddedDeletedModifiedChangeset,
DiffCopiedMovedChangeset,
DiffContent,
HunkLine,
} from "./lib/project/commit.js";
import type { Issue, IssueState } from "./lib/project/issue.js";
Expand Down Expand Up @@ -44,8 +43,7 @@ export type {
Commit,
CommitHeader,
Diff,
DiffAddedDeletedModifiedChangeset,
DiffCopiedMovedChangeset,
DiffContent,
DiffResponse,
HunkLine,
Issue,
Expand Down
192 changes: 84 additions & 108 deletions httpd-client/lib/project/commit.ts
Original file line number Diff line number Diff line change
@@ -1,171 +1,147 @@
import type { ZodSchema } from "zod";
import { array, literal, number, object, string, union } from "zod";
import type { z } from "zod";
export type { Commits, HunkLine, Commit, Diff, CommitHeader, DiffContent };

interface GitPerson {
name: string;
email: string;
}
import { array, literal, number, object, optional, string, union } from "zod";
export { commitHeaderSchema, diffSchema, commitSchema, commitsSchema };

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 };
}

export const commitHeaderSchema = object({
});

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() })),
}) 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>;
});

const diffFileSchema = object({
oid: string(),
mode: union([
literal("blob"),
literal("blobExecutable"),
literal("tree"),
literal("link"),
literal("commit"),
]),
});

interface ContextHunkLine {
line: string;
lineNoNew: number;
lineNoOld: number;
type: "context";
}
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";
};
}

const diffAddedDeletedModifiedChangesetSchema = object({
});

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: 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,
});

const diffAddedChangesetSchema = diffChangesetSchema.merge(
object({ new: diffFileSchema }),
);

export interface DiffCopiedMovedChangeset {
newPath: string;
oldPath: string;
}
const diffDeletedChangesetSchema = diffChangesetSchema.merge(
object({ old: diffFileSchema }),
);

const diffCopiedMovedChangesetSchema = object({
const diffModifiedChangesetSchema = diffChangesetSchema.merge(
object({ new: diffFileSchema, old: diffFileSchema }),
);

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;
};
}

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>;
});
40 changes: 35 additions & 5 deletions src/views/projects/Changeset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,55 @@
<FileDiff
{projectId}
{baseUrl}
{file}
{revision}
filePath={file.path}
fileDiff={file.diff}
headerBadgeCaption="added" />
{/each}
{#each diff.deleted as file}
<FileDiff
{projectId}
{baseUrl}
{file}
{revision}
filePath={file.path}
fileDiff={file.diff}
headerBadgeCaption="deleted" />
{/each}
{#each diff.modified as file}
<FileDiff {projectId} {baseUrl} {file} {revision} />
<FileDiff
{projectId}
{baseUrl}
{revision}
filePath={file.path}
fileDiff={file.diff} />
{/each}
{#each diff.moved as file}
<FileLocationChange {projectId} {baseUrl} {file} {revision} mode="moved" />
{#if file.diff}
<FileDiff
{projectId}
{baseUrl}
{revision}
filePath={file.newPath}
oldFilePath={file.oldPath}
fileDiff={file.diff}
headerBadgeCaption="moved" />
{:else}
<FileLocationChange
{projectId}
{baseUrl}
{revision}
newPath={file.newPath}
oldPath={file.oldPath}
mode="moved" />
{/if}
{/each}
{#each diff.copied as file}
<FileLocationChange {projectId} {baseUrl} {file} {revision} mode="copied" />
<FileLocationChange
{projectId}
{baseUrl}
{revision}
newPath={file.newPath}
oldPath={file.oldPath}
mode="copied" />
{/each}
</div>
Loading