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/wicket/pages/BlobDiffPage.html   |    2 
 src/main/java/com/gitblit/wicket/pages/ComparePage.html    |    2 
 src/main/java/com/gitblit/wicket/pages/CommitDiffPage.html |    2 
 src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java   |    9 +
 src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java  |    8 +
 src/main/java/com/gitblit/wicket/WicketUtils.java          |   33 ++++++++
 src/test/java/com/gitblit/tests/DiffUtilsTest.java         |    7 +
 src/main/java/com/gitblit/wicket/GitBlitWebApp.properties  |    4 
 src/main/java/com/gitblit/utils/DiffUtils.java             |   78 +++++++++++++++----
 src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java |   12 ++-
 src/main/java/com/gitblit/wicket/pages/ComparePage.java    |   24 +++++
 11 files changed, 146 insertions(+), 35 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();
diff --git a/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java b/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
index 3c65267..8f0d223 100644
--- a/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
+++ b/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
@@ -74,6 +74,8 @@
 	 */
 	private static final int GLOBAL_DIFF_LIMIT = 20000;
 
+	private static final boolean CONVERT_TABS = true;
+
 	private final DiffOutputStream os;
 
 	private final DiffStat diffStat;
@@ -451,14 +453,14 @@
 			// Highlight trailing whitespace on deleted/added lines.
 			Matcher matcher = trailingWhitespace.matcher(line);
 			if (matcher.find()) {
-				StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), false));
+				StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), CONVERT_TABS));
 				result.append("<span class='trailingws-").append(prefix == '+' ? "add" : "sub").append("'>");
 				result.append(StringUtils.escapeForHtml(matcher.group(1), false));
 				result.append("</span>");
 				return result.toString();
 			}
 		}
-		return StringUtils.escapeForHtml(line, false);
+		return StringUtils.escapeForHtml(line, CONVERT_TABS);
 	}
 
 	/**
@@ -490,7 +492,7 @@
 					} else {
 						sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">");
 					}
-					line = StringUtils.escapeForHtml(line.substring(1), false);
+					line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS);
 				}
 				sb.append(line);
 				if (gitLinkDiff) {
diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties
index 10b318d..d037420 100644
--- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties
+++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties
@@ -761,4 +761,6 @@
 gb.blinkComparator = Blink comparator
 gb.imgdiffSubtract = Subtract (black = identical)
 gb.deleteRepositoryHeader = Delete Repository
-gb.deleteRepositoryDescription = Deleted repositories will be unrecoverable.
\ No newline at end of file
+gb.deleteRepositoryDescription = Deleted repositories will be unrecoverable.
+gb.show_whitespace = show whitespace
+gb.ignore_whitespace = ignore whitespace
\ No newline at end of file
diff --git a/src/main/java/com/gitblit/wicket/WicketUtils.java b/src/main/java/com/gitblit/wicket/WicketUtils.java
index 9a40931..0d48e2b 100644
--- a/src/main/java/com/gitblit/wicket/WicketUtils.java
+++ b/src/main/java/com/gitblit/wicket/WicketUtils.java
@@ -48,6 +48,7 @@
 import com.gitblit.Keys;
 import com.gitblit.models.FederationModel;
 import com.gitblit.models.Metric;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.HttpUtils;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.TimeUtils;
@@ -61,7 +62,7 @@
 	public static void addCssClass(Component container, String value) {
 		container.add(new AttributeAppender("class", new Model<String>(value), " "));
 	}
-	
+
 	public static void setCssStyle(Component container, String value) {
 		container.add(new SimpleAttributeModifier("style", value));
 	}
@@ -330,6 +331,31 @@
 		return new PageParameters(parameterMap);
 	}
 
+	public static PageParameters newDiffParameter(String repositoryName,
+			String objectId, DiffComparator diffComparator) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		if (StringUtils.isEmpty(objectId)) {
+			return newRepositoryParameter(repositoryName);
+		}
+		parameterMap.put("r", repositoryName);
+		parameterMap.put("h", objectId);
+		parameterMap.put("w", "" + diffComparator.ordinal());
+		return new PageParameters(parameterMap);
+	}
+
+	public static PageParameters newDiffParameter(String repositoryName,
+			String objectId, DiffComparator diffComparator, String blobPath) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		if (StringUtils.isEmpty(objectId)) {
+			return newRepositoryParameter(repositoryName);
+		}
+		parameterMap.put("r", repositoryName);
+		parameterMap.put("h", objectId);
+		parameterMap.put("w", "" + diffComparator.ordinal());
+		parameterMap.put("f", blobPath);
+		return new PageParameters(parameterMap);
+	}
+
 	public static PageParameters newRangeParameter(String repositoryName,
 			String startRange, String endRange) {
 		Map<String, String> parameterMap = new HashMap<String, String>();
@@ -494,6 +520,11 @@
 		return params.getString("st", null);
 	}
 
+	public static DiffComparator getDiffComparator(PageParameters params) {
+		int ordinal = params.getInt("w", 0);
+		return DiffComparator.values()[ordinal];
+	}
+
 	public static int getPage(PageParameters params) {
 		// index from 1
 		return params.getInt("pg", 1);
diff --git a/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.html b/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.html
index c633642..d218436 100644
--- a/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.html
+++ b/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.html
@@ -9,7 +9,7 @@
 	
 	<!-- blob nav links -->	
 	<div class="page_nav2">
-		<a wicket:id="blameLink"><wicket:message key="gb.blame"></wicket:message></a> | <a wicket:id="historyLink"><wicket:message key="gb.history"></wicket:message></a> | <a wicket:id="patchLink"><wicket:message key="gb.patch"></wicket:message></a> | <a wicket:id="commitLink"><wicket:message key="gb.commit"></wicket:message></a> | <a wicket:id="commitDiffLink"><wicket:message key="gb.commitdiff"></wicket:message></a>
+		<a wicket:id="blameLink"><wicket:message key="gb.blame"></wicket:message></a> | <a wicket:id="historyLink"><wicket:message key="gb.history"></wicket:message></a> | <a wicket:id="patchLink"><wicket:message key="gb.patch"></wicket:message></a> | <a wicket:id="commitLink"><wicket:message key="gb.commit"></wicket:message></a> | <a wicket:id="commitDiffLink"><wicket:message key="gb.commitdiff"></wicket:message></a> | <a wicket:id="whitespaceLink"></a>
 	</div>	
 	
 	<!-- commit header -->
diff --git a/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java b/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
index ae737a5..187e460 100644
--- a/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
+++ b/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
@@ -25,6 +25,7 @@
 
 import com.gitblit.Keys;
 import com.gitblit.utils.DiffUtils;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.DiffUtils.DiffOutputType;
 import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.StringUtils;
@@ -32,6 +33,7 @@
 import com.gitblit.wicket.CacheControl.LastModified;
 import com.gitblit.wicket.WicketUtils;
 import com.gitblit.wicket.panels.CommitHeaderPanel;
+import com.gitblit.wicket.panels.LinkPanel;
 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
 
 @CacheControl(LastModified.BOOT)
@@ -42,6 +44,7 @@
 
 		final String blobPath = WicketUtils.getPath(params);
 		final String baseObjectId = WicketUtils.getBaseObjectId(params);
+		final DiffComparator diffComparator = WicketUtils.getDiffComparator(params);
 
 		Repository r = getRepository();
 		RevCommit commit = getCommit();
@@ -54,7 +57,7 @@
 			RevCommit parent = commit.getParentCount() == 0 ? null : commit.getParent(0);
 			ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
 					parent.getName(), commit.getName(), imageExtensions);
-			diff = DiffUtils.getDiff(r, commit, blobPath, DiffOutputType.HTML, handler).content;
+			diff = DiffUtils.getDiff(r, commit, blobPath, diffComparator, DiffOutputType.HTML, handler).content;
 			if (handler.getImgDiffCount() > 0) {
 				addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
 			}
@@ -65,7 +68,7 @@
 			RevCommit baseCommit = JGitUtils.getCommit(r, baseObjectId);
 			ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
 					baseCommit.getName(), commit.getName(), imageExtensions);
-			diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, DiffOutputType.HTML, handler).content;
+			diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, diffComparator, DiffOutputType.HTML, handler).content;
 			if (handler.getImgDiffCount() > 0) {
 				addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
 			}
@@ -78,6 +81,8 @@
 				WicketUtils.newObjectParameter(repositoryName, objectId)));
 		add(new BookmarkablePageLink<Void>("commitDiffLink", CommitDiffPage.class,
 				WicketUtils.newObjectParameter(repositoryName, objectId)));
+		add(new LinkPanel("whitespaceLink", null, getString(diffComparator.getOpposite().getTranslationKey()),
+				BlobDiffPage.class, WicketUtils.newDiffParameter(repositoryName, objectId, diffComparator.getOpposite(), blobPath)));
 
 		// diff page links
 		add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
diff --git a/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.html b/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.html
index 2c35a28..2e0d57c 100644
--- a/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.html
+++ b/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.html
@@ -9,7 +9,7 @@
 
 	<!-- commitdiff nav links -->	
 	<div class="page_nav2">
-		<wicket:message key="gb.parent"></wicket:message>: <span wicket:id="parentLink">[parent link]</span> | <a wicket:id="patchLink"><wicket:message key="gb.patch"></wicket:message></a> | <a wicket:id="commitLink"><wicket:message key="gb.commit"></wicket:message></a>
+		<wicket:message key="gb.parent"></wicket:message>: <span wicket:id="parentLink">[parent link]</span> | <a wicket:id="patchLink"><wicket:message key="gb.patch"></wicket:message></a> | <a wicket:id="commitLink"><wicket:message key="gb.commit"></wicket:message></a> | <a wicket:id="whitespaceLink"></a>
 	</div>	
 	
 	<!-- commit header -->
diff --git a/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java b/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
index c838dab..95580ed 100644
--- a/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
+++ b/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
@@ -37,6 +37,7 @@
 import com.gitblit.models.SubmoduleModel;
 import com.gitblit.servlet.RawServlet;
 import com.gitblit.utils.DiffUtils;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.DiffUtils.DiffOutput;
 import com.gitblit.utils.DiffUtils.DiffOutputType;
 import com.gitblit.utils.JGitUtils;
@@ -56,9 +57,9 @@
 	public CommitDiffPage(PageParameters params) {
 		super(params);
 
-		Repository r = getRepository();
-
-		RevCommit commit = getCommit();
+		final Repository r = getRepository();
+		final RevCommit commit = getCommit();
+		final DiffComparator diffComparator = WicketUtils.getDiffComparator(params);
 
 		List<String> parents = new ArrayList<String>();
 		if (commit.getParentCount() > 0) {
@@ -78,13 +79,16 @@
 				WicketUtils.newObjectParameter(repositoryName, objectId)));
 		add(new BookmarkablePageLink<Void>("commitLink", CommitPage.class,
 				WicketUtils.newObjectParameter(repositoryName, objectId)));
+		add(new LinkPanel("whitespaceLink", null, getString(diffComparator.getOpposite().getTranslationKey()),
+				CommitDiffPage.class, WicketUtils.newDiffParameter(repositoryName, objectId, diffComparator.getOpposite())));
 
 		add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
 
 		final List<String> imageExtensions = app().settings().getStrings(Keys.web.imageExtensions);
 		final ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
 				parents.isEmpty() ? null : parents.get(0), commit.getName(), imageExtensions);
-		final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, DiffOutputType.HTML, handler);
+
+		final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, diffComparator, DiffOutputType.HTML, handler);
 		if (handler.getImgDiffCount() > 0) {
 			addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
 		}
diff --git a/src/main/java/com/gitblit/wicket/pages/ComparePage.html b/src/main/java/com/gitblit/wicket/pages/ComparePage.html
index d7df717..74c9831 100644
--- a/src/main/java/com/gitblit/wicket/pages/ComparePage.html
+++ b/src/main/java/com/gitblit/wicket/pages/ComparePage.html
@@ -18,6 +18,7 @@
 					<select wicket:id="fromRef" class="span3" />
 					<i class="icon-arrow-right"></i>
 					<select wicket:id="toRef" class="span3" />
+                      <label style="padding:0px 5px;" class="checkbox"><input type="checkbox" wicket:id="ignoreWhitespaceCheckbox" /> <span wicket:id="ignoreWhitespaceLabel"></span></label>
 					<button class="btn" type="submit"><wicket:message key="gb.compare"></wicket:message></button>
 				</form>
 			</div>
@@ -26,6 +27,7 @@
 					<input wicket:id="fromId" type="text" class="span3" />
 					<i class="icon-arrow-right"></i>
 					<input wicket:id="toId" type="text" class="span3" />
+                    <label style="padding:0px 5px;" class="checkbox"><input type="checkbox" wicket:id="ignoreWhitespaceCheckbox" /> <span wicket:id="ignoreWhitespaceLabel"></span></label>
 					<button class="btn" type="submit"><wicket:message key="gb.compare"></wicket:message></button>
 				</form>
 			</div>
diff --git a/src/main/java/com/gitblit/wicket/pages/ComparePage.java b/src/main/java/com/gitblit/wicket/pages/ComparePage.java
index 62ae7c2..d171044 100644
--- a/src/main/java/com/gitblit/wicket/pages/ComparePage.java
+++ b/src/main/java/com/gitblit/wicket/pages/ComparePage.java
@@ -21,6 +21,7 @@
 
 import org.apache.wicket.PageParameters;
 import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.CheckBox;
 import org.apache.wicket.markup.html.form.DropDownChoice;
 import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
@@ -44,6 +45,7 @@
 import com.gitblit.models.SubmoduleModel;
 import com.gitblit.servlet.RawServlet;
 import com.gitblit.utils.DiffUtils;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.DiffUtils.DiffOutput;
 import com.gitblit.utils.DiffUtils.DiffOutputType;
 import com.gitblit.utils.JGitUtils;
@@ -68,6 +70,8 @@
 
 	IModel<String> fromRefId = new Model<String>("");
 	IModel<String> toRefId = new Model<String>("");
+
+	IModel<Boolean> ignoreWhitespace = Model.of(true);
 
 	public ComparePage(PageParameters params) {
 		super(params);
@@ -116,7 +120,8 @@
 			final ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
 					fromCommit.getName(), toCommit.getName(), imageExtensions);
 
-			final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, DiffOutputType.HTML, handler);
+			final DiffComparator diffComparator = WicketUtils.getDiffComparator(params);
+			final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, diffComparator, DiffOutputType.HTML, handler);
 			if (handler.getImgDiffCount() > 0) {
 				addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
 			}
@@ -212,6 +217,10 @@
 			comparison.add(new Label("diffText", diff.content).setEscapeModelStrings(false));
 		}
 
+		// set the default DiffComparator
+		DiffComparator diffComparator = WicketUtils.getDiffComparator(params);
+		ignoreWhitespace.setObject(DiffComparator.IGNORE_WHITESPACE == diffComparator);
+
 		//
 		// ref selection form
 		//
@@ -223,8 +232,13 @@
 			public void onSubmit() {
 				String from = ComparePage.this.fromRefId.getObject();
 				String to = ComparePage.this.toRefId.getObject();
+				boolean ignoreWS = ignoreWhitespace.getObject();
 
 				PageParameters params = WicketUtils.newRangeParameter(repositoryName, from, to);
+				if (ignoreWS) {
+					params.put("w", 1);
+				}
+
 				String relativeUrl = urlFor(ComparePage.class, params).toString();
 				String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
 				getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
@@ -245,6 +259,8 @@
 		}
 		refsForm.add(new DropDownChoice<String>("fromRef", fromRefId, refs).setEnabled(refs.size() > 0));
 		refsForm.add(new DropDownChoice<String>("toRef", toRefId, refs).setEnabled(refs.size() > 0));
+		refsForm.add(new Label("ignoreWhitespaceLabel", getString(DiffComparator.IGNORE_WHITESPACE.getTranslationKey())));
+		refsForm.add(new CheckBox("ignoreWhitespaceCheckbox", ignoreWhitespace));
 		add(refsForm);
 
 		//
@@ -258,8 +274,12 @@
 			public void onSubmit() {
 				String from = ComparePage.this.fromCommitId.getObject();
 				String to = ComparePage.this.toCommitId.getObject();
+				boolean ignoreWS = ignoreWhitespace.getObject();
 
 				PageParameters params = WicketUtils.newRangeParameter(repositoryName, from, to);
+				if (ignoreWS) {
+					params.put("w", 1);
+				}
 				String relativeUrl = urlFor(ComparePage.class, params).toString();
 				String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
 				getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
@@ -273,6 +293,8 @@
 		TextField<String> toIdField = new TextField<String>("toId", toCommitId);
 		WicketUtils.setInputPlaceholder(toIdField, getString("gb.to") + "...");
 		idsForm.add(toIdField);
+		idsForm.add(new Label("ignoreWhitespaceLabel", getString(DiffComparator.IGNORE_WHITESPACE.getTranslationKey())));
+		idsForm.add(new CheckBox("ignoreWhitespaceCheckbox", ignoreWhitespace));
 		add(idsForm);
 
 		r.close();
diff --git a/src/test/java/com/gitblit/tests/DiffUtilsTest.java b/src/test/java/com/gitblit/tests/DiffUtilsTest.java
index 9d627b8..c73e478 100644
--- a/src/test/java/com/gitblit/tests/DiffUtilsTest.java
+++ b/src/test/java/com/gitblit/tests/DiffUtilsTest.java
@@ -23,6 +23,7 @@
 
 import com.gitblit.models.AnnotatedLine;
 import com.gitblit.utils.DiffUtils;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.DiffUtils.DiffOutputType;
 import com.gitblit.utils.JGitUtils;
 
@@ -40,7 +41,7 @@
 		Repository repository = GitBlitSuite.getHelloworldRepository();
 		RevCommit commit = JGitUtils.getCommit(repository,
 				"1d0c2933a4ae69c362f76797d42d6bd182d05176");
-		String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN).content;
+		String diff = DiffUtils.getCommitDiff(repository, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;
 		repository.close();
 		assertTrue(diff != null && diff.length() > 0);
 		String expected = "-		system.out.println(\"Hello World\");\n+		System.out.println(\"Hello World\"";
@@ -54,7 +55,7 @@
 				"8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
 		RevCommit commit = JGitUtils.getCommit(repository,
 				"1d0c2933a4ae69c362f76797d42d6bd182d05176");
-		String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffOutputType.PLAIN).content;
+		String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;
 		repository.close();
 		assertTrue(diff != null && diff.length() > 0);
 		String expected = "-		system.out.println(\"Hello World\");\n+		System.out.println(\"Hello World\"";
@@ -66,7 +67,7 @@
 		Repository repository = GitBlitSuite.getHelloworldRepository();
 		RevCommit commit = JGitUtils.getCommit(repository,
 				"1d0c2933a4ae69c362f76797d42d6bd182d05176");
-		String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffOutputType.PLAIN).content;
+		String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;
 		repository.close();
 		assertTrue(diff != null && diff.length() > 0);
 		String expected = "-		system.out.println(\"Hello World\");\n+		System.out.println(\"Hello World\"";

--
Gitblit v1.9.1