From 62ff847f820fc69c308aeff4b317963cd4eadce0 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sat, 07 Mar 2015 12:26:23 -0500
Subject: [PATCH] Merged #233 "Ignore whitespace in diff viewer"

---
 src/main/java/com/gitblit/utils/DiffUtils.java |   78 ++++++++++++++++++++++++++++++---------
 1 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/gitblit/utils/DiffUtils.java b/src/main/java/com/gitblit/utils/DiffUtils.java
index b30a203..09b8f80 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 {
@@ -193,12 +227,13 @@
 	 *
 	 * @param repository
 	 * @param commit
+	 * @param comparator
 	 * @param outputType
 	 * @return the diff
 	 */
 	public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,
-			DiffOutputType outputType) {
-		return getDiff(repository, null, commit, null, outputType);
+			DiffComparator comparator, DiffOutputType outputType) {
+		return getDiff(repository, null, commit, null, comparator, outputType);
 	}
 
 	/**
@@ -206,6 +241,7 @@
 	 *
 	 * @param repository
 	 * @param commit
+	 * @param comparator
 	 * @param outputType
 	 * @param handler
 	 *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
@@ -213,8 +249,8 @@
 	 * @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) {
+		return getDiff(repository, null, commit, null, comparator, outputType, handler);
 	}
 
 
@@ -225,12 +261,13 @@
 	 * @param repository
 	 * @param commit
 	 * @param path
+	 * @param comparator
 	 * @param outputType
 	 * @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) {
+		return getDiff(repository, null, commit, path, comparator, outputType);
 	}
 
 	/**
@@ -240,6 +277,7 @@
 	 * @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}.
@@ -247,8 +285,8 @@
 	 * @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) {
+		return getDiff(repository, null, commit, path, comparator, outputType, handler);
 	}
 
 	/**
@@ -257,12 +295,13 @@
 	 * @param repository
 	 * @param baseCommit
 	 * @param commit
+	 * @param comparator
 	 * @param outputType
 	 * @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) {
+		return getDiff(repository, baseCommit, commit, null, comparator, outputType);
 	}
 
 	/**
@@ -271,6 +310,7 @@
 	 * @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}.
@@ -278,8 +318,8 @@
 	 * @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) {
+		return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler);
 	}
 
 	/**
@@ -294,11 +334,12 @@
 	 *            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
 	 * @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) {
+		return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null);
 	}
 
 	/**
@@ -312,19 +353,20 @@
 	 * @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.
 	 * @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) {
 		DiffStat stat = null;
 		String diff = null;
 		try {
 			ByteArrayOutputStream os = null;
-			RawTextComparator cmp = RawTextComparator.DEFAULT;
+
 			DiffFormatter df;
 			switch (outputType) {
 			case HTML:
@@ -337,7 +379,7 @@
 				break;
 			}
 			df.setRepository(repository);
-			df.setDiffComparator(cmp);
+			df.setDiffComparator((comparator == null ? DiffComparator.SHOW_WHITESPACE : comparator).textComparator);
 			df.setDetectRenames(true);
 
 			RevTree commitTree = commit.getTree();

--
Gitblit v1.9.1