]> source.dussan.org Git - jgit.git/commitdiff
Fix DiffConfig to understand "copy" resp. "copies" for diff.renames property. 48/1948/3
authorMarc Strapetz <marc.strapetz@syntevo.com>
Fri, 26 Nov 2010 10:07:04 +0000 (11:07 +0100)
committerMarc Strapetz <marc.strapetz@syntevo.com>
Mon, 29 Nov 2010 16:14:07 +0000 (17:14 +0100)
Rename detection should be considered enabled if
diff.renames config property is set to "copy" or "copies", instead of
throwing IllegalArgumentException.

Change-Id: If55d955e37235d4d00f5b0febd6aa10c0e27814e

org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffConfig.java
org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java

index 4b86f55fcd0bb34d32b45d7cf597bc7f21b1b4a5..436e606ad5f82a4a522e3df20c302411feca8eae 100644 (file)
 
 package org.eclipse.jgit.diff;
 
+import java.text.MessageFormat;
+
+import org.eclipse.jgit.JGitText;
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.Config.SectionParser;
+import org.eclipse.jgit.util.StringUtils;
 
 /** Keeps track of diff related configuration options. */
 public class DiffConfig {
@@ -55,15 +59,28 @@ public class DiffConfig {
                }
        };
 
+       /** Permissible values for {@code diff.renames}. */
+       public static enum RenameDetectionType {
+               /** Rename detection is disabled. */
+               FALSE,
+
+               /** Rename detection is enabled. */
+               TRUE,
+
+               /** Copies should be detected too. */
+               COPY
+       }
+
        private final boolean noPrefix;
 
-       private final boolean renames;
+       private final RenameDetectionType renameDetectionType;
 
        private final int renameLimit;
 
        private DiffConfig(final Config rc) {
                noPrefix = rc.getBoolean("diff", "noprefix", false);
-               renames = rc.getBoolean("diff", "renames", false);
+               renameDetectionType = parseRenameDetectionType(rc.getString("diff",
+                               null, "renames"));
                renameLimit = rc.getInt("diff", "renamelimit", 200);
        }
 
@@ -74,11 +91,37 @@ public class DiffConfig {
 
        /** @return true if rename detection is enabled by default. */
        public boolean isRenameDetectionEnabled() {
-               return renames;
+               return renameDetectionType != RenameDetectionType.FALSE;
+       }
+
+       /** @return type of rename detection to perform. */
+       public RenameDetectionType getRenameDetectionType() {
+               return renameDetectionType;
        }
 
        /** @return limit on number of paths to perform inexact rename detection. */
        public int getRenameLimit() {
                return renameLimit;
        }
+
+       private static RenameDetectionType parseRenameDetectionType(
+                       final String renameString) {
+               if (renameString == null)
+                       return RenameDetectionType.FALSE;
+               else if (StringUtils.equalsIgnoreCase("copy", renameString)
+                               || StringUtils.equalsIgnoreCase("copies", renameString))
+                       return RenameDetectionType.COPY;
+               else {
+                       final Boolean renameBoolean = StringUtils
+                                       .toBooleanOrNull(renameString);
+                       if (renameBoolean == null)
+                               throw new IllegalArgumentException(MessageFormat.format(
+                                               JGitText.get().enumValueNotSupported2, "diff",
+                                               "renames", renameString));
+                       else if (renameBoolean.booleanValue())
+                               return RenameDetectionType.TRUE;
+                       else
+                               return RenameDetectionType.FALSE;
+               }
+       }
 }
index 3759a12820b2abf56cbbed4ee6db6402da5f33ea..59f3d83ccf1fad0b3fd779897dc2a67aa4effb47 100644 (file)
@@ -120,6 +120,28 @@ public final class StringUtils {
                return true;
        }
 
+       /**
+        * Parse a string as a standard Git boolean value. See
+        * {@link #toBooleanOrNull(String)}.
+        *
+        * @param stringValue
+        *            the string to parse.
+        * @return the boolean interpretation of {@code value}.
+        * @throws IllegalArgumentException
+        *             if {@code value} is not recognized as one of the standard
+        *             boolean names.
+        */
+       public static boolean toBoolean(final String stringValue) {
+               if (stringValue == null)
+                       throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
+
+               final Boolean bool = toBooleanOrNull(stringValue);
+               if (bool == null)
+                       throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
+
+               return bool.booleanValue();
+       }
+
        /**
         * Parse a string as a standard Git boolean value.
         * <p>
@@ -133,30 +155,25 @@ public final class StringUtils {
         *
         * @param stringValue
         *            the string to parse.
-        * @return the boolean interpretation of {@code value}.
-        * @throws IllegalArgumentException
-        *             if {@code value} is not recognized as one of the standard
-        *             boolean names.
+        * @return the boolean interpretation of {@code value} or null in case the
+        *         string does not represent a boolean value
         */
-       public static boolean toBoolean(final String stringValue) {
+       public static Boolean toBooleanOrNull(final String stringValue) {
                if (stringValue == null)
-                       throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
+                       return null;
 
                if (equalsIgnoreCase("yes", stringValue)
                                || equalsIgnoreCase("true", stringValue)
                                || equalsIgnoreCase("1", stringValue)
-                               || equalsIgnoreCase("on", stringValue)) {
-                       return true;
-
-               } else if (equalsIgnoreCase("no", stringValue)
+                               || equalsIgnoreCase("on", stringValue))
+                       return Boolean.TRUE;
+               else if (equalsIgnoreCase("no", stringValue)
                                || equalsIgnoreCase("false", stringValue)
                                || equalsIgnoreCase("0", stringValue)
-                               || equalsIgnoreCase("off", stringValue)) {
-                       return false;
-
-               } else {
-                       throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
-               }
+                               || equalsIgnoreCase("off", stringValue))
+                       return Boolean.FALSE;
+               else
+                       return null;
        }
 
        /**