]> source.dussan.org Git - gitblit.git/commitdiff
Add DiffComparator argument to all DiffUtils calls, default to ignore all whitespace
authorJames Moger <james.moger@gitblit.com>
Thu, 26 Feb 2015 17:38:17 +0000 (12:38 -0500)
committerJames Moger <james.moger@gitblit.com>
Sat, 7 Mar 2015 15:57:27 +0000 (10:57 -0500)
src/main/java/com/gitblit/utils/DiffUtils.java
src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
src/main/java/com/gitblit/wicket/pages/ComparePage.java
src/test/java/com/gitblit/tests/DiffUtilsTest.java

index b30a203de477bf8bd6e77e2078ba7b7197976d9b..458a465146808be616e56d1a3b0cb31bd38e31b4 100644 (file)
@@ -88,6 +88,32 @@ public class DiffUtils {
                }\r
        }\r
 \r
+       /**\r
+        * Enumeration for the diff comparator types.\r
+        */\r
+       public static enum DiffComparator {\r
+               DEFAULT(RawTextComparator.DEFAULT),\r
+               WS_IGNORE_ALL(RawTextComparator.WS_IGNORE_ALL),\r
+               WS_IGNORE_LEADING(RawTextComparator.WS_IGNORE_LEADING),\r
+               WS_IGNORE_TRAILING(RawTextComparator.WS_IGNORE_TRAILING),\r
+               WS_IGNORE_CHANGE(RawTextComparator.WS_IGNORE_CHANGE);\r
+\r
+               public final RawTextComparator textComparator;\r
+\r
+               DiffComparator(RawTextComparator textComparator) {\r
+                       this.textComparator = textComparator;\r
+               }\r
+\r
+               public static DiffComparator forName(String name) {\r
+                       for (DiffComparator type : values()) {\r
+                               if (type.name().equalsIgnoreCase(name)) {\r
+                                       return type;\r
+                               }\r
+                       }\r
+                       return null;\r
+               }\r
+       }\r
+\r
        /**\r
         * Encapsulates the output of a diff.\r
         */\r
@@ -193,12 +219,13 @@ public class DiffUtils {
         *\r
         * @param repository\r
         * @param commit\r
+        * @param comparator\r
         * @param outputType\r
         * @return the diff\r
         */\r
        public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,\r
-                       DiffOutputType outputType) {\r
-               return getDiff(repository, null, commit, null, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType) {\r
+               return getDiff(repository, null, commit, null, comparator, outputType);\r
        }\r
 \r
        /**\r
@@ -206,6 +233,7 @@ public class DiffUtils {
         *\r
         * @param repository\r
         * @param commit\r
+        * @param comparator\r
         * @param outputType\r
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
@@ -213,8 +241,8 @@ public class DiffUtils {
         * @return the diff\r
         */\r
        public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,\r
-                       DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, null, commit, null, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
+               return getDiff(repository, null, commit, null, comparator, outputType, handler);\r
        }\r
 \r
 \r
@@ -225,12 +253,13 @@ public class DiffUtils {
         * @param repository\r
         * @param commit\r
         * @param path\r
+        * @param comparator\r
         * @param outputType\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,\r
-                       DiffOutputType outputType) {\r
-               return getDiff(repository, null, commit, path, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType) {\r
+               return getDiff(repository, null, commit, path, comparator, outputType);\r
        }\r
 \r
        /**\r
@@ -240,6 +269,7 @@ public class DiffUtils {
         * @param repository\r
         * @param commit\r
         * @param path\r
+        * @param comparator\r
         * @param outputType\r
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
@@ -247,8 +277,8 @@ public class DiffUtils {
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,\r
-                       DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, null, commit, path, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
+               return getDiff(repository, null, commit, path, comparator, outputType, handler);\r
        }\r
 \r
        /**\r
@@ -257,12 +287,13 @@ public class DiffUtils {
         * @param repository\r
         * @param baseCommit\r
         * @param commit\r
+        * @param comparator\r
         * @param outputType\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       DiffOutputType outputType) {\r
-               return getDiff(repository, baseCommit, commit, null, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType) {\r
+               return getDiff(repository, baseCommit, commit, null, comparator, outputType);\r
        }\r
 \r
        /**\r
@@ -271,6 +302,7 @@ public class DiffUtils {
         * @param repository\r
         * @param baseCommit\r
         * @param commit\r
+        * @param comparator\r
         * @param outputType\r
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
@@ -278,8 +310,8 @@ public class DiffUtils {
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, baseCommit, commit, null, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
+               return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler);\r
        }\r
 \r
        /**\r
@@ -294,11 +326,12 @@ public class DiffUtils {
         *            if the path is specified, the diff is restricted to that file\r
         *            or folder. if unspecified, the diff is for the entire commit.\r
         * @param outputType\r
+        * @param diffComparator\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       String path, DiffOutputType outputType) {\r
-               return getDiff(repository, baseCommit, commit, path, outputType, null);\r
+                       String path, DiffComparator diffComparator, DiffOutputType outputType) {\r
+               return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null);\r
        }\r
 \r
        /**\r
@@ -312,19 +345,20 @@ public class DiffUtils {
         * @param path\r
         *            if the path is specified, the diff is restricted to that file\r
         *            or folder. if unspecified, the diff is for the entire commit.\r
+        * @param comparator\r
         * @param outputType\r
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
         *            May be {@code null}, resulting in the default behavior.\r
         * @return the diff\r
         */\r
-       public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, DiffOutputType outputType,\r
-                       final BinaryDiffHandler handler) {\r
+       public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path,\r
+                       DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler) {\r
                DiffStat stat = null;\r
                String diff = null;\r
                try {\r
                        ByteArrayOutputStream os = null;\r
-                       RawTextComparator cmp = RawTextComparator.DEFAULT;\r
+\r
                        DiffFormatter df;\r
                        switch (outputType) {\r
                        case HTML:\r
@@ -337,7 +371,7 @@ public class DiffUtils {
                                break;\r
                        }\r
                        df.setRepository(repository);\r
-                       df.setDiffComparator(cmp);\r
+                       df.setDiffComparator((comparator == null ? DiffComparator.DEFAULT : comparator).textComparator);\r
                        df.setDetectRenames(true);\r
 \r
                        RevTree commitTree = commit.getTree();\r
index ae737a53285c78a7bf76bdf5c3f42e3e21988511..f74e8cb3f2f43fbe8021d1f51ddb1ac98c86672f 100644 (file)
@@ -25,6 +25,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
 \r
 import com.gitblit.Keys;\r
 import com.gitblit.utils.DiffUtils;\r
+import com.gitblit.utils.DiffUtils.DiffComparator;\r
 import com.gitblit.utils.DiffUtils.DiffOutputType;\r
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.StringUtils;\r
@@ -42,6 +43,7 @@ public class BlobDiffPage extends RepositoryPage {
 \r
                final String blobPath = WicketUtils.getPath(params);\r
                final String baseObjectId = WicketUtils.getBaseObjectId(params);\r
+               final DiffComparator diffComparator = DiffComparator.WS_IGNORE_ALL;\r
 \r
                Repository r = getRepository();\r
                RevCommit commit = getCommit();\r
@@ -54,7 +56,7 @@ public class BlobDiffPage extends RepositoryPage {
                        RevCommit parent = commit.getParentCount() == 0 ? null : commit.getParent(0);\r
                        ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,\r
                                        parent.getName(), commit.getName(), imageExtensions);\r
-                       diff = DiffUtils.getDiff(r, commit, blobPath, DiffOutputType.HTML, handler).content;\r
+                       diff = DiffUtils.getDiff(r, commit, blobPath, diffComparator, DiffOutputType.HTML, handler).content;\r
                        if (handler.getImgDiffCount() > 0) {\r
                                addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
                        }\r
@@ -65,7 +67,7 @@ public class BlobDiffPage extends RepositoryPage {
                        RevCommit baseCommit = JGitUtils.getCommit(r, baseObjectId);\r
                        ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,\r
                                        baseCommit.getName(), commit.getName(), imageExtensions);\r
-                       diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, DiffOutputType.HTML, handler).content;\r
+                       diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, DiffComparator.WS_IGNORE_ALL, DiffOutputType.HTML, handler).content;\r
                        if (handler.getImgDiffCount() > 0) {\r
                                addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
                        }\r
index c838dab5500a41bd7e5f6bac19f7fbb6c78a3fbb..9b3d693b4b6dfbb750e021320de011d71d5ec74b 100644 (file)
@@ -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<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 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
                }
index 62ae7c25c92d6220f2a403bb4be20df28b0883b4..68e904b916e8460c3d5840e39e22cff462c2b4a8 100644 (file)
@@ -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
                        }
index 9d627b80116c2500c474e9f310efd7669bea593c..34366ffa3c0c743d7de0a23a0894798e7d8cbbc1 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 \r
 import com.gitblit.models.AnnotatedLine;\r
 import com.gitblit.utils.DiffUtils;\r
+import com.gitblit.utils.DiffUtils.DiffComparator;\r
 import com.gitblit.utils.DiffUtils.DiffOutputType;\r
 import com.gitblit.utils.JGitUtils;\r
 \r
@@ -40,7 +41,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getCommitDiff(repository, commit, DiffComparator.DEFAULT, DiffOutputType.PLAIN).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r
@@ -54,7 +55,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                                "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffComparator.DEFAULT, DiffOutputType.PLAIN).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r
@@ -66,7 +67,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.DEFAULT, DiffOutputType.PLAIN).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r