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

1015: Webrev can't handle changes with binary files #16

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -227,10 +227,14 @@
body().removeEventListener("keydown", framesOnKeyDown);
body().style.margin = 0;
if (state.view !== "index") {
if (state.head.content[state.index] === null || state.base.content[state.index] === null) {
const content = fetchFileContent(state);
state.head.content[state.index] = content.head;
state.base.content[state.index] = content.base;
if (state.comparison.files[state.index].binary === false) {
if (state.head.content[state.index] === null || state.base.content[state.index] === null) {
const content = fetchFileContent(state);
if (content != null) {
state.head.content[state.index] = content.head;
state.base.content[state.index] = content.base;
}
}
}
}
if (state.view === "index") {
@@ -402,40 +406,50 @@
const p = create("p");
const code = create("code");
if (file.status === "modified" || file.status === "copied" || file.status === "renamed") {
const cdiff = create("a");
cdiff.href = "#cdiff-" + i + "-" + filename;
cdiff.innerHTML = "Cdiffs";
code.append(cdiff, " ");

const udiff = create("a");
udiff.href = "#udiff-" + i + "-" + filename;
udiff.innerHTML = "Udiffs";
code.append(udiff, " ");

const sdiff = create("a");
sdiff.href = "#sdiff-" + i + "-" + filename;
sdiff.innerHTML = "Sdiffs";
code.append(sdiff, " ");

const frames = create("a");
frames.href = "#frames-" + i + "-" + filename;
frames.innerHTML = "Frames";
code.append(frames, " ");

const oldFile = create("a");
oldFile.href = "#old-" + i + "-" + filename;
oldFile.innerHTML = "Old";
code.append(oldFile, " ");

const newFile = create("a");
newFile.href = "#new-" + i + "-" + filename;
newFile.innerHTML = "New";
code.append(newFile, " ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");
if (file.binary === true) {
code.append("------ ");
code.append("------ ");
code.append("------ ");
code.append("------ ");
code.append("--- ");
code.append("--- ");
code.append("----- ");
} else {
const cdiff = create("a");
cdiff.href = "#cdiff-" + i + "-" + filename;
cdiff.innerHTML = "Cdiffs";
code.append(cdiff, " ");

const udiff = create("a");
udiff.href = "#udiff-" + i + "-" + filename;
udiff.innerHTML = "Udiffs";
code.append(udiff, " ");

const sdiff = create("a");
sdiff.href = "#sdiff-" + i + "-" + filename;
sdiff.innerHTML = "Sdiffs";
code.append(sdiff, " ");

const frames = create("a");
frames.href = "#frames-" + i + "-" + filename;
frames.innerHTML = "Frames";
code.append(frames, " ");

const oldFile = create("a");
oldFile.href = "#old-" + i + "-" + filename;
oldFile.innerHTML = "Old";
code.append(oldFile, " ");

const newFile = create("a");
newFile.href = "#new-" + i + "-" + filename;
newFile.innerHTML = "New";
code.append(newFile, " ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");
}

const rawFile = create("a");
rawFile.href = "https://raw.githubusercontent.com/" + metadata.head.repo.full_name + "/" + metadata.head.sha + "/" + file.filename;
@@ -461,15 +475,20 @@
code.append("------ ");
code.append("--- ");

const newFile = create("a");
newFile.href = "#new-" + i + "-" + filename;
newFile.innerHTML = "New";
code.append(newFile, " ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");
if (file.binary === true) {
code.append("--- ");
code.append("----- ");
} else {
const newFile = create("a");
newFile.href = "#new-" + i + "-" + filename;
newFile.innerHTML = "New";
code.append(newFile, " ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");
}

const rawFile = create("a");
rawFile.href = "https://raw.githubusercontent.com/" + metadata.head.repo.full_name + "/" + metadata.head.sha + "/" + file.filename;
@@ -489,19 +508,26 @@
code.append("------ ");
code.append("------ ");

const oldFile = create("a");
oldFile.href = "#old-" + i + "-" + filename;
oldFile.innerHTML = "Old";
code.append(oldFile, " ");

code.append("--- ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");

code.append("--- ");
if (file.binary === true) {
code.append("--- ");
code.append("--- ");
code.append("----- ");
code.append("--- ");
} else {
const oldFile = create("a");
oldFile.href = "#old-" + i + "-" + filename;
oldFile.innerHTML = "Old";
code.append(oldFile, " ");

code.append("--- ");

const patchFile = create("a");
patchFile.href = "#patch-" + i + "-" + filename;
patchFile.innerHTML = "Patch";
code.append(patchFile, " ");

code.append("--- ");
}

p.append(code);

@@ -1477,57 +1503,59 @@

function removeContext(patch) {
const hunks = new Array();
let srcStart = 0;
let srcLines = new Array();
let dstStart = 0;
let dstLines = new Array();
let isBefore = true;

const lines = patch.split('\n');
const end = patch.endsWith('\n') ? lines.length - 1 : lines.length;
for (let i = 0; i < end; i++) {
const line = lines[i];
if (line.startsWith("@@")) {
if (srcLines.length > 0 || dstLines.length > 0) {
if (patch != null) {
Copy link
Member

@edvbld edvbld May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do an early return instead? As in

if (patch === null) {
    return hunks;
}

That way you don't have to re-indent the entire method 😄

let srcStart = 0;
let srcLines = new Array();
let dstStart = 0;
let dstLines = new Array();
let isBefore = true;

const lines = patch.split('\n');
const end = patch.endsWith('\n') ? lines.length - 1 : lines.length;
for (let i = 0; i < end; i++) {
const line = lines[i];
if (line.startsWith("@@")) {
if (srcLines.length > 0 || dstLines.length > 0) {
hunks.push(new Hunk(srcStart, srcLines, dstStart, dstLines));
}

const parts = line.split(' ');
srcStart = Number(parseRange(parts[1]).start);
dstStart = Number(parseRange(parts[2]).start);
srcLines = new Array();
dstLines = new Array();
} else if (line.startsWith(' ') && !isBefore) {
while (i < lines.length && lines[i].startsWith(' ')) {
i++;
}
hunks.push(new Hunk(srcStart, srcLines, dstStart, dstLines));
}
srcStart = srcStart + srcLines.length;
srcLines = new Array();
dstStart = dstStart + dstLines.length;
dstLines = new Array();
} else if (line.startsWith(' ') && isBefore) {
srcStart++;
dstStart++;
} else if (line.startsWith('-')) {
isBefore = false;
srcLines.push(line);

const parts = line.split(' ');
srcStart = Number(parseRange(parts[1]).start);
dstStart = Number(parseRange(parts[2]).start);
srcLines = new Array();
dstLines = new Array();
} else if (line.startsWith(' ') && !isBefore) {
while (i < lines.length && lines[i].startsWith(' ')) {
i++;
} else if (line.startsWith('+')) {
isBefore = false;
dstLines.push(line);
} else if (line.startsWith("\\ No newline at end of file")) {
continue;
} else if (line === "") {
continue;
} else {
throw "Unexpected content on line " + i + ": '" + line + "'";
}
hunks.push(new Hunk(srcStart, srcLines, dstStart, dstLines));
srcStart = srcStart + srcLines.length;
srcLines = new Array();
dstStart = dstStart + dstLines.length;
dstLines = new Array();
} else if (line.startsWith(' ') && isBefore) {
srcStart++;
dstStart++;
} else if (line.startsWith('-')) {
isBefore = false;
srcLines.push(line);

} else if (line.startsWith('+')) {
isBefore = false;
dstLines.push(line);
} else if (line.startsWith("\\ No newline at end of file")) {
continue;
} else if (line === "") {
continue;
} else {
throw "Unexpected content on line " + i + ": '" + line + "'";
}
}

// The last hunk did not have context after
if (srcLines.length > 0 || dstLines.length > 0) {
hunks.push(new Hunk(srcStart, srcLines, dstStart, dstLines));
// The last hunk did not have context after
if (srcLines.length > 0 || dstLines.length > 0) {
hunks.push(new Hunk(srcStart, srcLines, dstStart, dstLines));
}
}

return hunks;
@@ -1677,10 +1705,14 @@

// Pre-fetch base and head content for up to 25 files
for (let i = 0; i < Math.min(files.length, 25); i++) {
state.index = i;
const content = fetchFileContent(state);
state.head.content[i] = content.head;
state.base.content[i] = content.base;
if (state.comparison.files[i].binary === false) {
state.index = i;
const content = fetchFileContent(state);
if (content != null) {
state.head.content[i] = content.head;
state.base.content[i] = content.base;
}
}
}
state.index = -1;
}