From eecaad8b8e2c447429c31a01d49260ddd6b4ee03 Mon Sep 17 00:00:00 2001 From: Paul Martin <paul@paulsputer.com> Date: Sat, 16 Apr 2016 17:35:32 -0400 Subject: [PATCH] Proof of concept #1026 --- src/main/java/com/gitblit/utils/DiffUtils.java | 98 ++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 76 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/gitblit/utils/DiffUtils.java b/src/main/java/com/gitblit/utils/DiffUtils.java index b30a203..41aab4c 100644 --- a/src/main/java/com/gitblit/utils/DiffUtils.java +++ b/src/main/java/com/gitblit/utils/DiffUtils.java @@ -89,6 +89,40 @@ } /** + * Enumeration for the diff comparator types. + */ + public static enum DiffComparator { + SHOW_WHITESPACE(RawTextComparator.DEFAULT), + IGNORE_WHITESPACE(RawTextComparator.WS_IGNORE_ALL), + IGNORE_LEADING(RawTextComparator.WS_IGNORE_LEADING), + IGNORE_TRAILING(RawTextComparator.WS_IGNORE_TRAILING), + IGNORE_CHANGES(RawTextComparator.WS_IGNORE_CHANGE); + + public final RawTextComparator textComparator; + + DiffComparator(RawTextComparator textComparator) { + this.textComparator = textComparator; + } + + public DiffComparator getOpposite() { + return this == SHOW_WHITESPACE ? IGNORE_WHITESPACE : SHOW_WHITESPACE; + } + + public String getTranslationKey() { + return "gb." + name().toLowerCase(); + } + + public static DiffComparator forName(String name) { + for (DiffComparator type : values()) { + if (type.name().equalsIgnoreCase(name)) { + return type; + } + } + return null; + } + } + + /** * Encapsulates the output of a diff. */ public static class DiffOutput implements Serializable { @@ -123,13 +157,16 @@ public final List<PathChangeModel> paths = new ArrayList<PathChangeModel>(); private final String commitId; + + private final Repository repository; - public DiffStat(String commitId) { + public DiffStat(String commitId, Repository repository) { this.commitId = commitId; + this.repository = repository; } public PathChangeModel addPath(DiffEntry entry) { - PathChangeModel pcm = PathChangeModel.from(entry, commitId); + PathChangeModel pcm = PathChangeModel.from(entry, commitId, repository); paths.add(pcm); return pcm; } @@ -193,12 +230,14 @@ * * @param repository * @param commit + * @param comparator * @param outputType + * @param tabLength * @return the diff */ public static DiffOutput getCommitDiff(Repository repository, RevCommit commit, - DiffOutputType outputType) { - return getDiff(repository, null, commit, null, outputType); + DiffComparator comparator, DiffOutputType outputType, int tabLength) { + return getDiff(repository, null, commit, null, comparator, outputType, tabLength); } /** @@ -206,15 +245,17 @@ * * @param repository * @param commit + * @param comparator * @param outputType * @param handler * to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}. * May be {@code null}, resulting in the default behavior. + * @param tabLength * @return the diff */ public static DiffOutput getCommitDiff(Repository repository, RevCommit commit, - DiffOutputType outputType, BinaryDiffHandler handler) { - return getDiff(repository, null, commit, null, outputType, handler); + DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) { + return getDiff(repository, null, commit, null, comparator, outputType, handler, tabLength); } @@ -225,12 +266,14 @@ * @param repository * @param commit * @param path + * @param comparator * @param outputType + * @param tabLength * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit commit, String path, - DiffOutputType outputType) { - return getDiff(repository, null, commit, path, outputType); + DiffComparator comparator, DiffOutputType outputType, int tabLength) { + return getDiff(repository, null, commit, path, comparator, outputType, tabLength); } /** @@ -240,15 +283,17 @@ * @param repository * @param commit * @param path + * @param comparator * @param outputType * @param handler * to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}. * May be {@code null}, resulting in the default behavior. + * @param tabLength * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit commit, String path, - DiffOutputType outputType, BinaryDiffHandler handler) { - return getDiff(repository, null, commit, path, outputType, handler); + DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) { + return getDiff(repository, null, commit, path, comparator, outputType, handler, tabLength); } /** @@ -257,12 +302,15 @@ * @param repository * @param baseCommit * @param commit + * @param comparator * @param outputType + * @param tabLength + * * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, - DiffOutputType outputType) { - return getDiff(repository, baseCommit, commit, null, outputType); + DiffComparator comparator, DiffOutputType outputType, int tabLength) { + return getDiff(repository, baseCommit, commit, null, comparator, outputType, tabLength); } /** @@ -271,15 +319,17 @@ * @param repository * @param baseCommit * @param commit + * @param comparator * @param outputType * @param handler * to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}. * May be {@code null}, resulting in the default behavior. + * @param tabLength * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, - DiffOutputType outputType, BinaryDiffHandler handler) { - return getDiff(repository, baseCommit, commit, null, outputType, handler); + DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) { + return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler, tabLength); } /** @@ -294,11 +344,13 @@ * if the path is specified, the diff is restricted to that file * or folder. if unspecified, the diff is for the entire commit. * @param outputType + * @param diffComparator + * @param tabLength * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, - String path, DiffOutputType outputType) { - return getDiff(repository, baseCommit, commit, path, outputType, null); + String path, DiffComparator diffComparator, DiffOutputType outputType, int tabLength) { + return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null, tabLength); } /** @@ -312,23 +364,25 @@ * @param path * if the path is specified, the diff is restricted to that file * or folder. if unspecified, the diff is for the entire commit. + * @param comparator * @param outputType * @param handler * to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}. * May be {@code null}, resulting in the default behavior. + * @param tabLength * @return the diff */ - public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, DiffOutputType outputType, - final BinaryDiffHandler handler) { + public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, + DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) { DiffStat stat = null; String diff = null; try { ByteArrayOutputStream os = null; - RawTextComparator cmp = RawTextComparator.DEFAULT; + DiffFormatter df; switch (outputType) { case HTML: - df = new GitBlitDiffFormatter(commit.getName(), path, handler); + df = new GitBlitDiffFormatter(commit.getName(), repository, path, handler, tabLength); break; case PLAIN: default: @@ -337,7 +391,7 @@ break; } df.setRepository(repository); - df.setDiffComparator(cmp); + df.setDiffComparator((comparator == null ? DiffComparator.SHOW_WHITESPACE : comparator).textComparator); df.setDetectRenames(true); RevTree commitTree = commit.getTree(); @@ -497,7 +551,7 @@ DiffStat stat = null; try { RawTextComparator cmp = RawTextComparator.DEFAULT; - DiffStatFormatter df = new DiffStatFormatter(commit.getName()); + DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository); df.setRepository(repository); df.setDiffComparator(cmp); df.setDetectRenames(true); -- Gitblit v1.9.1