Skip to content
Permalink
Browse files
webrev: proper index.html fix and charset fixes
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 3, 2020
1 parent eed788f commit ff0c4d50ba7bc8dbcfbbf35a9f5476e5e28d19a2
@@ -26,6 +26,8 @@

import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.nio.charset.MalformedInputException;
import java.util.List;
import java.util.stream.Collectors;

@@ -43,20 +45,34 @@ public AddedFileView(ReadOnlyRepository repo, Hash base, Hash head, List<CommitM
this.out = out;
this.commits = commits;
this.formatter = formatter;
var path = patch.target().path().get();
var pathInRepo = repo.root().resolve(path);
if (patch.isTextual()) {
binaryContent = null;
if (head == null) {
newContent = Files.readAllLines(repo.root().resolve(patch.target().path().get()));
List<String> lines = null;
for (var charset : List.of(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)) {
try {
lines = Files.readAllLines(pathInRepo, charset);
break;
} catch (MalformedInputException e) {
continue;
}
}
if (lines == null) {
throw new IllegalStateException("Could not read " + pathInRepo + " as UTF-8 nor as ISO-8859-1");
}
newContent = lines;
} else {
newContent = repo.lines(patch.target().path().get(), head).orElseThrow(IllegalArgumentException::new);
newContent = repo.lines(path, head).orElseThrow(IllegalArgumentException::new);
}
stats = new WebrevStats(patch.asTextualPatch().stats(), newContent.size());
} else {
newContent = null;
if (head == null) {
binaryContent = Files.readAllBytes(repo.root().resolve(patch.target().path().get()));
binaryContent = Files.readAllBytes(pathInRepo);
} else {
binaryContent = repo.show(patch.target().path().get(), head).orElseThrow(IllegalArgumentException::new);
binaryContent = repo.show(path, head).orElseThrow(IllegalArgumentException::new);
}
stats = WebrevStats.empty();
}
@@ -26,6 +26,8 @@

import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.nio.charset.MalformedInputException;
import java.util.List;
import java.util.stream.Collectors;

@@ -64,7 +66,19 @@ public ModifiedFileView(ReadOnlyRepository repo, Hash base, Hash head, List<Comm
});
newContent = List.of(content.get(0) + "-dirty");
} else {
newContent = Files.readAllLines(path);
List<String> lines = null;
for (var charset : List.of(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1)) {
try {
lines = Files.readAllLines(repo.root().resolve(path), charset);
break;
} catch (MalformedInputException e) {
continue;
}
}
if (lines == null) {
throw new IllegalStateException("Could not read " + path + " as UTF-8 nor as ISO-8859-1");
}
newContent = lines;
}
} else {
newContent = repo.lines(patch.target().path().get(), head).orElseThrow(() -> {
@@ -49,13 +49,11 @@ public RawView(Path out, Path file, byte[] binary) {
}

public void render(Writer w) throws IOException {
var rawFile = out.resolve(file.toString());
if (Files.exists(rawFile)) {
// If the raw file collides with a file generated by the webrev (such as index.html), rename it
rawFile = out.resolve("_" + file.toString());
} else {
Files.createDirectories(rawFile.getParent());
}
// If the raw file collides with a file generated by the webrev (such as index.html), rename it
var rawFile = Webrev.STATIC_FILES.contains(file.toString()) ?
out.resolve("_" + file.toString()) :
out.resolve(file.toString());
Files.createDirectories(rawFile.getParent());

if (binary != null) {
Files.write(rawFile, binary);
@@ -40,6 +40,11 @@ public class Webrev {
private static final String ICON = "nanoduke.ico";
private static final String CSS = "style.css";

private static final String INDEX = "index.html";

public static final Set<String> STATIC_FILES =
Set.of(ANCNAV_HTML, ANCNAV_JS, ICON, CSS, INDEX);

public static class RequiredBuilder {
private final ReadOnlyRepository repository;

@@ -216,7 +221,7 @@ private void generate(Diff diff, Hash tailEnd, Hash head) throws IOException {

var issueForWebrev = issue != null && issueLinker != null ? issueLinker.apply(issue) : null;
var tailEndURL = commitLinker != null ? commitLinker.apply(tailEnd.hex()) : null;
try (var w = Files.newBufferedWriter(output.resolve("index.html"))) {
try (var w = Files.newBufferedWriter(output.resolve(INDEX))) {
var index = new IndexView(fileViews,
title,
username,
@@ -279,7 +284,7 @@ static String relativeToCSS(Path out, Path file) {
}

static String relativeToIndex(Path out, Path file) {
return relativeTo(out.resolve("index.html"), file);
return relativeTo(out.resolve(INDEX), file);
}

static String relativeToAncnavHTML(Path out, Path file) {

0 comments on commit ff0c4d5

Please sign in to comment.