From cff35236ba16e2c376c59feeff310f62554a4140 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 26 Feb 2015 12:38:17 -0500 Subject: [PATCH] Add DiffComparator argument to all DiffUtils calls, default to ignore all whitespace --- .../java/com/gitblit/utils/DiffUtils.java | 70 ++++++++++++++----- .../gitblit/wicket/pages/BlobDiffPage.java | 6 +- .../gitblit/wicket/pages/CommitDiffPage.java | 5 +- .../com/gitblit/wicket/pages/ComparePage.java | 4 +- .../java/com/gitblit/tests/DiffUtilsTest.java | 7 +- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/gitblit/utils/DiffUtils.java b/src/main/java/com/gitblit/utils/DiffUtils.java index b30a203d..458a4651 100644 --- a/src/main/java/com/gitblit/utils/DiffUtils.java +++ b/src/main/java/com/gitblit/utils/DiffUtils.java @@ -88,6 +88,32 @@ public class DiffUtils { } } + /** + * Enumeration for the diff comparator types. + */ + public static enum DiffComparator { + DEFAULT(RawTextComparator.DEFAULT), + WS_IGNORE_ALL(RawTextComparator.WS_IGNORE_ALL), + WS_IGNORE_LEADING(RawTextComparator.WS_IGNORE_LEADING), + WS_IGNORE_TRAILING(RawTextComparator.WS_IGNORE_TRAILING), + WS_IGNORE_CHANGE(RawTextComparator.WS_IGNORE_CHANGE); + + public final RawTextComparator textComparator; + + DiffComparator(RawTextComparator textComparator) { + this.textComparator = textComparator; + } + + 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. */ @@ -193,12 +219,13 @@ public class DiffUtils { * * @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 +233,7 @@ public class DiffUtils { * * @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 +241,8 @@ public class DiffUtils { * @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 +253,13 @@ public class DiffUtils { * @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 +269,7 @@ public class DiffUtils { * @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 +277,8 @@ public class DiffUtils { * @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 +287,13 @@ public class DiffUtils { * @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 +302,7 @@ public class DiffUtils { * @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 +310,8 @@ public class DiffUtils { * @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 +326,12 @@ public class DiffUtils { * 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 +345,20 @@ public class DiffUtils { * @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 +371,7 @@ public class DiffUtils { break; } df.setRepository(repository); - df.setDiffComparator(cmp); + df.setDiffComparator((comparator == null ? DiffComparator.DEFAULT : comparator).textComparator); df.setDetectRenames(true); RevTree commitTree = commit.getTree(); diff --git a/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java b/src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java index ae737a53..f74e8cb3 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 org.eclipse.jgit.revwalk.RevCommit; 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; @@ -42,6 +43,7 @@ public class BlobDiffPage extends RepositoryPage { final String blobPath = WicketUtils.getPath(params); final String baseObjectId = WicketUtils.getBaseObjectId(params); + final DiffComparator diffComparator = DiffComparator.WS_IGNORE_ALL; Repository r = getRepository(); RevCommit commit = getCommit(); @@ -54,7 +56,7 @@ public class BlobDiffPage extends RepositoryPage { 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 +67,7 @@ public class BlobDiffPage extends RepositoryPage { 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.WS_IGNORE_ALL, DiffOutputType.HTML, handler).content; if (handler.getImgDiffCount() > 0) { addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs } diff --git a/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java b/src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java index c838dab5..9b3d693b 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.PathModel.PathChangeModel; 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; @@ -84,7 +85,9 @@ public class CommitDiffPage extends RepositoryPage { final List 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 DiffComparator diffComparator = DiffComparator.WS_IGNORE_ALL; + 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.java b/src/main/java/com/gitblit/wicket/pages/ComparePage.java index 62ae7c25..68e904b9 100644 --- a/src/main/java/com/gitblit/wicket/pages/ComparePage.java +++ b/src/main/java/com/gitblit/wicket/pages/ComparePage.java @@ -44,6 +44,7 @@ import com.gitblit.models.RepositoryModel; 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; @@ -116,7 +117,8 @@ public class ComparePage extends RepositoryPage { 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 = DiffComparator.WS_IGNORE_ALL; + 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 } diff --git a/src/test/java/com/gitblit/tests/DiffUtilsTest.java b/src/test/java/com/gitblit/tests/DiffUtilsTest.java index 9d627b80..34366ffa 100644 --- a/src/test/java/com/gitblit/tests/DiffUtilsTest.java +++ b/src/test/java/com/gitblit/tests/DiffUtilsTest.java @@ -23,6 +23,7 @@ import org.junit.Test; 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 @@ public class DiffUtilsTest extends GitblitUnitTest { 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.DEFAULT, 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 @@ public class DiffUtilsTest extends GitblitUnitTest { "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.DEFAULT, 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 @@ public class DiffUtilsTest extends GitblitUnitTest { 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.DEFAULT, DiffOutputType.PLAIN).content; repository.close(); assertTrue(diff != null && diff.length() > 0); String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; -- 2.39.5