]> source.dussan.org Git - jgit.git/commitdiff
Add additional FastForwardMode enums for different config contexts 43/9743/2
authorTomasz Zarna <tzarna@gmail.com>
Thu, 17 Jan 2013 20:38:43 +0000 (21:38 +0100)
committerTomasz Zarna <tzarna@gmail.com>
Fri, 18 Jan 2013 11:04:17 +0000 (12:04 +0100)
FastForwardMode is represented by different strings depending on context
it is set or get from. E.g. FastForwardMode.FF_ONLY for
branch.<name>.mergeoptions is "--ff-only" but for merge.ff it is "only".

Change-Id: I39ae93578e4783de80ebf4af29ae23b3936eec47

org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigConstants.java

index 925976d30081fdeb937d545df6232756d70ffafb..f074273137c75a1f092e94a297476040038f713d 100644 (file)
@@ -63,6 +63,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Set;
 
+import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.junit.MockSystemReader;
 import org.eclipse.jgit.util.FS;
@@ -320,6 +321,74 @@ public class ConfigTest {
                assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
        }
 
+       @Test
+       public void testGetFastForwardMergeoptions() throws ConfigInvalidException {
+               Config c = new Config(null); // not set
+               assertSame(FastForwardMode.FF, c.getEnum(
+                               ConfigConstants.CONFIG_BRANCH_SECTION, "side",
+                               ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
+               c = parse("[branch \"side\"]\n\tmergeoptions = --ff-only\n");
+               assertSame(FastForwardMode.FF_ONLY, c.getEnum(
+                               ConfigConstants.CONFIG_BRANCH_SECTION, "side",
+                               ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
+                               FastForwardMode.FF_ONLY));
+               c = parse("[branch \"side\"]\n\tmergeoptions = --ff\n");
+               assertSame(FastForwardMode.FF, c.getEnum(
+                               ConfigConstants.CONFIG_BRANCH_SECTION, "side",
+                               ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
+               c = parse("[branch \"side\"]\n\tmergeoptions = --no-ff\n");
+               assertSame(FastForwardMode.NO_FF, c.getEnum(
+                               ConfigConstants.CONFIG_BRANCH_SECTION, "side",
+                               ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.NO_FF));
+       }
+
+       @Test
+       public void testSetFastForwardMergeoptions() {
+               final Config c = new Config();
+               c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF);
+               assertEquals("[branch \"side\"]\n\tmergeoptions = --ff\n", c.toText());
+               c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF_ONLY);
+               assertEquals("[branch \"side\"]\n\tmergeoptions = --ff-only\n",
+                               c.toText());
+               c.setEnum("branch", "side", "mergeoptions", FastForwardMode.NO_FF);
+               assertEquals("[branch \"side\"]\n\tmergeoptions = --no-ff\n",
+                               c.toText());
+       }
+
+       @Test
+       public void testGetFastForwardMerge() throws ConfigInvalidException {
+               Config c = new Config(null); // not set
+               assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
+                               ConfigConstants.CONFIG_KEY_MERGE, null,
+                               ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
+               c = parse("[merge]\n\tff = only\n");
+               assertSame(FastForwardMode.Merge.ONLY, c.getEnum(
+                               ConfigConstants.CONFIG_KEY_MERGE, null,
+                               ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.ONLY));
+               c = parse("[merge]\n\tff = true\n");
+               assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
+                               ConfigConstants.CONFIG_KEY_MERGE, null,
+                               ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
+               c = parse("[merge]\n\tff = false\n");
+               assertSame(FastForwardMode.Merge.FALSE, c.getEnum(
+                               ConfigConstants.CONFIG_KEY_MERGE, null,
+                               ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.FALSE));
+       }
+
+       @Test
+       public void testSetFastForwardMerge() {
+               final Config c = new Config();
+               c.setEnum("merge", null, "ff",
+                               FastForwardMode.Merge.valueOf(FastForwardMode.FF));
+               assertEquals("[merge]\n\tff = true\n", c.toText());
+               c.setEnum("merge", null, "ff",
+                               FastForwardMode.Merge.valueOf(FastForwardMode.FF_ONLY));
+               assertEquals("[merge]\n\tff = only\n", c.toText());
+               c.setEnum("merge", null, "ff",
+                               FastForwardMode.Merge.valueOf(FastForwardMode.NO_FF));
+               assertEquals("[merge]\n\tff = false\n", c.toText());
+       }
+
        @Test
        public void testReadLong() throws ConfigInvalidException {
                assertReadLong(1L);
index 04d91af4b07a1d237b93c85dad582f91a817ffc7..2d3a6e18779a7bad02be2279e15605e5eadc0bd7 100644 (file)
@@ -63,6 +63,7 @@ import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
 import org.eclipse.jgit.dircache.DirCacheCheckout;
 import org.eclipse.jgit.internal.JGitText;
 import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.Config.ConfigEnum;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectIdRef;
@@ -81,6 +82,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.revwalk.RevWalkUtils;
 import org.eclipse.jgit.treewalk.FileTreeIterator;
+import org.eclipse.jgit.util.StringUtils;
 
 /**
  * A class used to execute a {@code Merge} command. It has setters for all
@@ -102,10 +104,11 @@ public class MergeCommand extends GitCommand<MergeResult> {
        private FastForwardMode fastForwardMode = FastForwardMode.FF;
 
        /**
-        * The modes available for fast forward merges (corresponding to the --ff,
-        * --no-ff and --ff-only options).
+        * The modes available for fast forward merges corresponding to the
+        * <code>--ff</code>, <code>--no-ff</code> and <code>--ff-only</code>
+        * options under <code>branch.<name>.mergeoptions</code>.
         */
-       public enum FastForwardMode {
+       public enum FastForwardMode implements ConfigEnum {
                /**
                 * Corresponds to the default --ff option (for a fast forward update the
                 * branch pointer only).
@@ -121,6 +124,75 @@ public class MergeCommand extends GitCommand<MergeResult> {
                 * forward).
                 */
                FF_ONLY;
+
+               public String toConfigValue() {
+                       return "--" + name().toLowerCase().replace('_', '-'); //$NON-NLS-1$
+               }
+
+               public boolean matchConfigValue(String in) {
+                       if (StringUtils.isEmptyOrNull(in))
+                               return false;
+                       if (!in.startsWith("--")) //$NON-NLS-1$
+                               return false;
+                       return name().equalsIgnoreCase(in.substring(2).replace('-', '_'));
+               }
+
+               /**
+                * The modes available for fast forward merges corresponding to the
+                * options under <code>merge.ff</code>.
+                */
+               public enum Merge {
+                       /**
+                        * {@link FastForwardMode#FF}.
+                        */
+                       TRUE,
+                       /**
+                        * {@link FastForwardMode#NO_FF}.
+                        */
+                       FALSE,
+                       /**
+                        * {@link FastForwardMode#FF_ONLY}.
+                        */
+                       ONLY;
+
+                       /**
+                        * Map from <code>FastForwardMode</code> to
+                        * <code>FastForwardMode.Merge</code>.
+                        *
+                        * @param ffMode
+                        *            the <code>FastForwardMode</code> value to be mapped
+                        * @return the mapped code>FastForwardMode.Merge</code> value
+                        */
+                       public static Merge valueOf(FastForwardMode ffMode) {
+                               switch (ffMode) {
+                               case NO_FF:
+                                       return FALSE;
+                               case FF_ONLY:
+                                       return ONLY;
+                               default:
+                                       return TRUE;
+                               }
+                       }
+               }
+
+               /**
+                * Map from <code>FastForwardMode.Merge</code> to
+                * <code>FastForwardMode</code>.
+                *
+                * @param ffMode
+                *            the <code>FastForwardMode.Merge</code> value to be mapped
+                * @return the mapped code>FastForwardMode</code> value
+                */
+               public static FastForwardMode valueOf(FastForwardMode.Merge ffMode) {
+                       switch (ffMode) {
+                       case FALSE:
+                               return NO_FF;
+                       case ONLY:
+                               return FF_ONLY;
+                       default:
+                               return FF;
+                       }
+               }
        }
 
        /**
index f882d49ed1b6744fe83cc13e172b852f0fc80252..af04b109011cd6622767da0fc1902afec7140b6d 100644 (file)
@@ -387,6 +387,13 @@ public class Config {
                if (value == null)
                        return defaultValue;
 
+               if (all[0] instanceof ConfigEnum) {
+                       for (T t : all) {
+                               if (((ConfigEnum) t).matchConfigValue(value))
+                                       return t;
+                       }
+               }
+
                String n = value.replace(' ', '_');
 
                // Because of c98abc9c0586c73ef7df4172644b7dd21c979e9d being used in
@@ -728,7 +735,11 @@ public class Config {
         */
        public <T extends Enum<?>> void setEnum(final String section,
                        final String subsection, final String name, final T value) {
-               String n = value.name().toLowerCase().replace('_', ' ');
+               String n;
+               if (value instanceof ConfigEnum)
+                       n = ((ConfigEnum) value).toConfigValue();
+               else
+                       n = value.name().toLowerCase().replace('_', ' ');
                setString(section, subsection, name, n);
        }
 
@@ -1275,4 +1286,27 @@ public class Config {
                        pos--;
                }
        }
+
+       /**
+        * Converts enumeration values into configuration options and vice-versa,
+        * allowing to match a config option with an enum value.
+        *
+        */
+       public static interface ConfigEnum {
+               /**
+                * Converts enumeration value into a string to be save in config.
+                *
+                * @return the enum value as config string
+                */
+               String toConfigValue();
+
+               /**
+                * Checks if the given string matches with enum value.
+                *
+                * @param in
+                *            the string to match
+                * @return true if the given string matches enum value, false otherwise
+                */
+               boolean matchConfigValue(String in);
+       }
 }
index dbaa043e215257359668455c270658cca27d52cc..8fd84c8938d78b48749c9a3fa5eda58640587da0 100644 (file)
@@ -191,4 +191,7 @@ public class ConfigConstants {
 
        /** The "mergeoptions" key */
        public static final String CONFIG_KEY_MERGEOPTIONS = "mergeoptions";
+
+       /** The "ff" key */
+       public static final String CONFIG_KEY_FF = "ff";
 }