Add Heartwood translation layer for file diffs

Co-authored-by: xphoniex <dj.2dixx@gmail.com>
Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
This commit is contained in:
Rūdolfs Ošiņš
2022-12-29 09:58:59 +01:00
co-authored by xphoniex
parent ca10a69ee5
commit 4bb34023c2
8 changed files with 69 additions and 34 deletions
+1 -1
View File
@@ -88,7 +88,7 @@ run_binary() {
$HTTP_API_BINARY --listen 0.0.0.0:8777 --root $WORKSPACE --passphrase $PASSPHRASE
}
BINADY=false
BINARY=false
NON_INTERACTIVE=false
DETACH=false
+1 -6
View File
@@ -1,5 +1,5 @@
import type { Stats, Person } from "@app/lib/project";
import type { Diff } from "@app/lib/diff";
import type { Diff, DiffStats } from "@app/lib/diff";
import { ApiError } from "@app/lib/api";
import { getDaysPassed } from "@app/lib/utils";
@@ -63,11 +63,6 @@ export interface WeeklyActivity {
week: number;
}
export interface DiffStats {
additions: number;
deletions: number;
}
export interface Commit {
header: CommitHeader;
stats: DiffStats;
+15 -10
View File
@@ -1,10 +1,10 @@
export const lineNumberR = (line: LineDiff): string | number => {
switch (line.type) {
case LineDiffType.Addition: {
return line.lineNum;
return line.lineNo;
}
case LineDiffType.Context: {
return line.lineNumNew;
return line.lineNoNew;
}
case LineDiffType.Deletion: {
return " ";
@@ -18,10 +18,10 @@ export const lineNumberL = (line: LineDiff): string | number => {
return " ";
}
case LineDiffType.Context: {
return line.lineNumOld;
return line.lineNoOld;
}
case LineDiffType.Deletion: {
return line.lineNum;
return line.lineNo;
}
}
};
@@ -41,7 +41,7 @@ export const lineSign = (line: LineDiff): string => {
};
export enum LineDiffType {
Addition = "addition",
Addition = "insertion",
Context = "context",
Deletion = "deletion",
}
@@ -49,20 +49,20 @@ export enum LineDiffType {
export interface Addition {
type: LineDiffType.Addition;
line: string;
lineNum: number;
lineNo: number;
}
export interface Context {
type: LineDiffType.Context;
line: string;
lineNumNew: number;
lineNumOld: number;
lineNoNew: number;
lineNoOld: number;
}
export interface Deletion {
type: LineDiffType.Deletion;
line: string;
lineNum: number;
lineNo: number;
}
export type LineDiff = Addition | Deletion | Context;
@@ -84,13 +84,18 @@ export interface Hunk {
}
export interface Diff {
created: FileDiff[];
added: FileDiff[];
deleted: FileDiff[];
moved: string[];
copied: string[];
modified: FileDiff[];
}
export interface DiffStats {
insertions: number;
deletions: number;
}
export enum EofNewLine {
OldMissing = "oldMissing",
NewMissing = "newMissing",
+2 -2
View File
@@ -1,7 +1,7 @@
import type { Author, PeerInfo } from "@app/lib/cobs";
import type { Comment, Thread } from "@app/lib/issue";
import type { Commit, DiffStats } from "@app/lib/commit";
import type { Diff } from "@app/lib/diff";
import type { Commit } from "@app/lib/commit";
import type { Diff, DiffStats } from "@app/lib/diff";
import type { Host } from "@app/lib/api";
import type { PeerId, Id } from "@app/lib/project";
+33 -1
View File
@@ -276,10 +276,42 @@ export class Project implements ProjectInfo {
}
async getCommit(commit: string): Promise<Commit> {
return new Request(
const result = await new Request(
`projects/${this.id}/commits/${commit}`,
this.seed.addr,
).get();
result.stats["insertions"] = result.stats["additions"];
delete result.stats["additions"];
result.diff["added"] = result.diff["created"];
delete result.diff["created"];
for (const kind of ["added", "deleted", "modified"]) {
for (const file of result.diff[kind]) {
for (const hunk of file.diff.hunks) {
for (const line of hunk.lines) {
if (line["type"] === "addition") {
line["type"] = "insertion";
}
if (line["lineNumOld"]) {
line["lineNoOld"] = line["lineNumOld"];
delete line["lineNumOld"];
}
if (line["lineNumNew"]) {
line["lineNoNew"] = line["lineNumNew"];
delete line["lineNumNew"];
}
if (line["lineNum"]) {
line["lineNo"] = line["lineNum"];
delete line["lineNum"];
}
}
}
}
}
return result;
}
async getTree(commit: string, path: string): Promise<Tree> {
@@ -1,19 +1,18 @@
<script lang="ts">
import type { DiffStats } from "@app/lib/commit";
import type { Diff } from "@app/lib/diff";
import type { Diff, DiffStats } from "@app/lib/diff";
import FileDiff from "@app/views/projects/SourceBrowser/FileDiff.svelte";
export let diff: Diff;
export let stats: DiffStats;
const diffDescription = ({ modified, created, deleted }: Diff): string => {
const diffDescription = ({ modified, added, deleted }: Diff): string => {
const s = [];
if (modified.length) {
s.push(`${modified.length} file(s) changed`);
}
if (created.length) {
s.push(`${created.length} file(s) created`);
if (added.length) {
s.push(`${added.length} file(s) added`);
}
if (deleted.length) {
s.push(`${deleted.length} file(s) deleted`);
@@ -38,13 +37,13 @@
<div class="changeset-summary">
<span>{diffDescription(diff)}</span>
with
<span class="additions">{stats.additions} addition(s)</span>
<span class="additions">{stats.insertions} insertion(s)</span>
and
<span class="deletions">{stats.deletions} deletion(s)</span>
</div>
<div class="diff-listing">
{#each diff.created as file}
<FileDiff on:browse {file} mode="created" />
{#each diff.added as file}
<FileDiff on:browse {file} mode="added" />
{/each}
{#each diff.deleted as file}
<FileDiff on:browse {file} mode="deleted" />
@@ -118,8 +118,8 @@
<header class="file-header" on:click={collapse}>
<div class="actions">
<p class="txt-regular">{file.path}</p>
{#if mode === "created"}
<Badge variant="positive">created</Badge>
{#if mode === "added"}
<Badge variant="positive">added</Badge>
{:else if mode === "deleted"}
<Badge variant="negative">deleted</Badge>
{/if}
+8 -4
View File
@@ -48,7 +48,9 @@ test("modified file", async ({ page }) => {
// Diff header.
await expect(
page.locator("text=1 file(s) changed with 1 addition(s) and 4 deletion(s)"),
page.locator(
"text=1 file(s) changed with 1 insertion(s) and 4 deletion(s)",
),
).toBeVisible();
// Diff.
@@ -61,9 +63,9 @@ test("created file", async ({ page }) => {
`${projectFixtureUrl}/remotes/${bobRemote}/commits/d6318f7f3d9c15b8ac6dd52267c53220d00f0982`,
);
await expect(
page.locator("text=1 file(s) created with 9 addition(s) and 0 deletion(s)"),
page.locator("text=1 file(s) added with 9 insertion(s) and 0 deletion(s)"),
).toBeVisible();
await expect(page.locator("text=subconscious.txt created")).toBeVisible();
await expect(page.locator("text=subconscious.txt added")).toBeVisible();
});
test("deleted file", async ({ page }) => {
@@ -71,7 +73,9 @@ test("deleted file", async ({ page }) => {
`${projectFixtureUrl}/remotes/${bobRemote}/commits/cd13c2d9a8a930d64a82b6134b44d1b872e33662`,
);
await expect(
page.locator("text=1 file(s) deleted with 0 addition(s) and 1 deletion(s)"),
page.locator(
"text=1 file(s) deleted with 0 insertion(s) and 1 deletion(s)",
),
).toBeVisible();
await expect(page.locator("text=.hidden deleted")).toBeVisible();
});