Handle added and deleted files correctly

This commit is contained in:
René Pfeuffer
2020-05-28 19:51:51 +02:00
parent abca9e9746
commit 2efd21d466
4 changed files with 115 additions and 17 deletions

View File

@@ -281,7 +281,7 @@ const HUNK_3 = {
}
]
};
const TEST_CONTENT = {
const TEST_CONTENT_WITH_HUNKS = {
oldPath: "src/main/js/CommitMessage.js",
newPath: "src/main/js/CommitMessage.js",
oldEndingNewLine: true,
@@ -300,8 +300,67 @@ const TEST_CONTENT = {
}
};
describe("diff expander", () => {
const diffExpander = new DiffExpander(TEST_CONTENT);
const TEST_CONTENT_WIT_NEW_BINARY_FILE = {
oldPath: "/dev/null",
newPath: "src/main/fileUploadV2.png",
oldEndingNewLine: true,
newEndingNewLine: true,
oldRevision: "0000000000000000000000000000000000000000",
newRevision: "86c370aae0727d628a5438f79a5cdd45752b9d99",
type: "add"
};
const TEST_CONTENT_WITH_NEW_TEXT_FILE = {
oldPath: "/dev/null",
newPath: "src/main/markdown/README.md",
oldEndingNewLine: true,
newEndingNewLine: true,
oldRevision: "0000000000000000000000000000000000000000",
newRevision: "4e173d365d796b9a9e7562fcd0ef90398ae37046",
type: "add",
language: "markdown",
hunks: [
{
content: "@@ -0,0 +1,2 @@",
newStart: 1,
newLines: 2,
changes: [
{ content: "line 1", type: "insert", lineNumber: 1, isInsert: true },
{ content: "line 2", type: "insert", lineNumber: 2, isInsert: true }
]
}
],
_links: {
lines: {
href:
"http://localhost:8081/scm/api/v2/repositories/scm-manager/scm-editor-plugin/content/c63898d35520ee47bcc3a8291660979918715762/src/main/markdown/README.md?start={start}?end={end}",
templated: true
}
}
};
const TEST_CONTENT_WITH_DELETED_TEXT_FILE = {
oldPath: "README.md",
newPath: "/dev/null",
oldEndingNewLine: true,
newEndingNewLine: true,
oldRevision: "4875ab3b7a1bb117e1948895148557fc5c0b6f75",
newRevision: "0000000000000000000000000000000000000000",
type: "delete",
language: "markdown",
hunks: [
{
content: "@@ -1 +0,0 @@",
oldStart: 1,
oldLines: 1,
changes: [{ content: "# scm-editor-plugin", type: "delete", lineNumber: 1, isDelete: true }]
}
],
_links: { lines: { href: "http://localhost:8081/dev/null?start={start}?end={end}", templated: true } }
};
describe("with hunks the diff expander", () => {
const diffExpander = new DiffExpander(TEST_CONTENT_WITH_HUNKS);
it("should have hunk count from origin", () => {
expect(diffExpander.hunkCount()).toBe(4);
});
@@ -326,3 +385,34 @@ describe("diff expander", () => {
expect(diffExpander.getHunk(3).maxExpandBottomRange).toBeGreaterThan(99999);
});
});
describe("for a new file with text input the diff expander", () => {
const diffExpander = new DiffExpander(TEST_CONTENT_WITH_NEW_TEXT_FILE);
it("should create answer for single hunk", () => {
expect(diffExpander.hunkCount()).toBe(1);
});
it("should neither give expandable lines for top nor bottom", () => {
const hunk = diffExpander.getHunk(0);
expect(hunk.maxExpandHeadRange).toBe(0);
expect(hunk.maxExpandBottomRange).toBe(0);
});
});
describe("for a deleted file with text input the diff expander", () => {
const diffExpander = new DiffExpander(TEST_CONTENT_WITH_DELETED_TEXT_FILE);
it("should create answer for single hunk", () => {
expect(diffExpander.hunkCount()).toBe(1);
});
it("should neither give expandable lines for top nor bottom", () => {
const hunk = diffExpander.getHunk(0);
expect(hunk.maxExpandHeadRange).toBe(0);
expect(hunk.maxExpandBottomRange).toBe(0);
});
});
describe("for a new file with binary input the diff expander", () => {
const diffExpander = new DiffExpander(TEST_CONTENT_WIT_NEW_BINARY_FILE);
it("should create answer for no hunk", () => {
expect(diffExpander.hunkCount()).toBe(0);
});
});

View File

@@ -32,26 +32,34 @@ class DiffExpander {
}
hunkCount = () => {
return this.file.hunks.length;
if (this.file.hunks) {
return this.file.hunks.length;
} else {
return 0;
}
};
minLineNumber = (n: number) => {
return this.file.hunks[n].newStart;
minLineNumber: (n: number) => number = (n: number) => {
return this.file.hunks![n]!.newStart!;
};
maxLineNumber = (n: number) => {
return this.file.hunks[n].newStart + this.file.hunks[n].newLines;
maxLineNumber: (n: number) => number = (n: number) => {
return this.file.hunks![n]!.newStart! + this.file.hunks![n]!.newLines!;
};
computeMaxExpandHeadRange = (n: number) => {
if (n === 0) {
if (this.file.type === "delete") {
return 0;
} else if (n === 0) {
return this.minLineNumber(n) - 1;
}
return this.minLineNumber(n) - this.maxLineNumber(n - 1);
};
computeMaxExpandBottomRange = (n: number) => {
if (n === this.file.hunks.length - 1) {
if (this.file.type === "add" || this.file.type === "delete") {
return 0;
} else if (n === this.file!.hunks!.length - 1) {
return Number.MAX_SAFE_INTEGER;
}
return this.minLineNumber(n + 1) - this.maxLineNumber(n);
@@ -67,7 +75,7 @@ class DiffExpander {
expandBottom: () => {
return this;
},
hunk: this.file.hunks[n]
hunk: this.file?.hunks![n]
};
};
}

View File

@@ -299,7 +299,7 @@ class DiffFile extends React.Component<Props, State> {
<div className="panel-block is-paddingless">
{fileAnnotations}
<TokenizedDiffView className={viewType} viewType={viewType} file={file}>
{(hunks: HunkType[]) => hunks.map((hunk, n) => this.renderHunk(file, diffExpander.getHunk(n), n))}
{(hunks: HunkType[]) => hunks?.map((hunk, n) => this.renderHunk(file, diffExpander.getHunk(n), n))}
</TokenizedDiffView>
</div>
);

View File

@@ -34,7 +34,7 @@ import { Links } from "@scm-manager/ui-types";
export type FileChangeType = "add" | "modify" | "delete" | "copy" | "rename";
export type File = {
hunks: Hunk[];
hunks?: Hunk[];
newEndingNewLine: boolean;
newMode?: string;
newPath: string;
@@ -53,10 +53,10 @@ export type File = {
export type Hunk = {
changes: Change[];
content: string;
oldStart: number;
newStart: number;
oldLines: number;
newLines: number;
oldStart?: number;
newStart?: number;
oldLines?: number;
newLines?: number;
};
export type ChangeType = "insert" | "delete" | "normal" | "conflict";