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;
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);
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;
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
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).
* 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;
+ }
+ }
}
/**
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
*/
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);
}
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);
+ }
}