소스 검색

Handle all values of branch.[name].rebase

BranchConfig treated this config property as a boolean, but git also
allows the values "preserve" and "interactive". Config property
pull.rebase also allows the same values.

Replace private enum PullCommand.PullRebaseMode by new public enum
BranchConfig.BranchRebaseMode and adapt all uses. Add a new setter to
PullCommand.

Note: PullCommand will treat "interactive" like "true", i.e., as a
non-interactive rebase. Not sure how "interactive" should be handled.
At least it won't balk on it.

Bug: 499482
Change-Id: I7309360f5662b2c2efa1bd8ea6f112c63cf064af
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
tags/v4.5.0.201609210915-r
Thomas Wolf 7 년 전
부모
커밋
aadbb158e1

+ 17
- 15
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java 파일 보기

import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
command.setURI(fileUri()); command.setURI(fileUri());
Git git2 = command.call(); Git git2 = command.call();
addRepoToClose(git2.getRepository()); addRepoToClose(git2.getRepository());
assertFalse(git2
.getRepository()
.getConfig()
.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE, false));
assertNull(git2.getRepository().getConfig().getEnum(
BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE, null));


FileBasedConfig userConfig = SystemReader.getInstance().openUserConfig( FileBasedConfig userConfig = SystemReader.getInstance().openUserConfig(
null, git.getRepository().getFS()); null, git.getRepository().getFS());
command.setURI(fileUri()); command.setURI(fileUri());
git2 = command.call(); git2 = command.call();
addRepoToClose(git2.getRepository()); addRepoToClose(git2.getRepository());
assertTrue(git2
.getRepository()
.getConfig()
.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE, false));
assertEquals(BranchRebaseMode.REBASE,
git2.getRepository().getConfig().getEnum(
BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));


userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
command.setURI(fileUri()); command.setURI(fileUri());
git2 = command.call(); git2 = command.call();
addRepoToClose(git2.getRepository()); addRepoToClose(git2.getRepository());
assertTrue(git2
.getRepository()
.getConfig()
.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE, false));
assertEquals(BranchRebaseMode.REBASE,
git2.getRepository().getConfig().getEnum(
BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));


} }



+ 41
- 22
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RenameBranchCommandTest.java 파일 보기

package org.eclipse.jgit.api; package org.eclipse.jgit.api;


import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.lib.StoredConfig;
@Test @Test
public void renameBranchSingleConfigValue() throws Exception { public void renameBranchSingleConfigValue() throws Exception {
StoredConfig config = git.getRepository().getConfig(); StoredConfig config = git.getRepository().getConfig();
config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, true);
config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
config.save(); config.save();


String branch = "b1"; String branch = "b1";


assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, true));
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
branch, ConfigConstants.CONFIG_KEY_REBASE, false));
assertEquals(BranchRebaseMode.REBASE,
config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));
assertNull(config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REBASE, null));


assertNotNull(git.branchRename().setNewName(branch).call()); assertNotNull(git.branchRename().setNewName(branch).call());


config = git.getRepository().getConfig(); config = git.getRepository().getConfig();
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, false));
assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
branch, ConfigConstants.CONFIG_KEY_REBASE, false));
assertNull(config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE, null));
assertEquals(BranchRebaseMode.REBASE,
config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));
} }


@Test @Test
public void renameBranchExistingSection() throws Exception { public void renameBranchExistingSection() throws Exception {
String branch = "b1"; String branch = "b1";
StoredConfig config = git.getRepository().getConfig(); StoredConfig config = git.getRepository().getConfig();
config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, true);
config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, "a", "a"); Constants.MASTER, "a", "a");
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a", config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
@Test @Test
public void renameBranchMultipleConfigValues() throws Exception { public void renameBranchMultipleConfigValues() throws Exception {
StoredConfig config = git.getRepository().getConfig(); StoredConfig config = git.getRepository().getConfig();
config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, true);
config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true); Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
config.save(); config.save();


String branch = "b1"; String branch = "b1";


assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, true));
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
branch, ConfigConstants.CONFIG_KEY_REBASE, false));
assertEquals(BranchRebaseMode.REBASE,
config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));
assertNull(config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REBASE, null));
assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true)); Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
assertNotNull(git.branchRename().setNewName(branch).call()); assertNotNull(git.branchRename().setNewName(branch).call());


config = git.getRepository().getConfig(); config = git.getRepository().getConfig();
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_REBASE, false));
assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
branch, ConfigConstants.CONFIG_KEY_REBASE, false));
assertNull(config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
ConfigConstants.CONFIG_KEY_REBASE, null));
assertEquals(BranchRebaseMode.REBASE,
config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, branch,
ConfigConstants.CONFIG_KEY_REBASE,
BranchRebaseMode.NONE));
assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false)); Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,

+ 3
- 2
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java 파일 보기

import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.NullProgressMonitor; import org.eclipse.jgit.lib.NullProgressMonitor;
ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE); ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE);
if (ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase) if (ConfigConstants.CONFIG_KEY_ALWAYS.equals(autosetupRebase)
|| ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase)) || ConfigConstants.CONFIG_KEY_REMOTE.equals(autosetupRebase))
clonedRepo.getConfig().setBoolean(
clonedRepo.getConfig().setEnum(
ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
ConfigConstants.CONFIG_KEY_REBASE, true);
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
clonedRepo.getConfig().save(); clonedRepo.getConfig().save();
} }



+ 67
- 37
org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java 파일 보기

import org.eclipse.jgit.api.errors.WrongRepositoryStateException; import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;


private ProgressMonitor monitor = NullProgressMonitor.INSTANCE; private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;


private PullRebaseMode pullRebaseMode = null;
private BranchRebaseMode pullRebaseMode = null;


private String remote; private String remote;




private MergeStrategy strategy = MergeStrategy.RECURSIVE; private MergeStrategy strategy = MergeStrategy.RECURSIVE;


private enum PullRebaseMode implements Config.ConfigEnum {
REBASE_PRESERVE("preserve", true, true), //$NON-NLS-1$
REBASE("true", true, false), //$NON-NLS-1$
NO_REBASE("false", false, false); //$NON-NLS-1$

private final String configValue;

private final boolean rebase;

private final boolean preserveMerges;

PullRebaseMode(String configValue, boolean rebase,
boolean preserveMerges) {
this.configValue = configValue;
this.rebase = rebase;
this.preserveMerges = preserveMerges;
}

public String toConfigValue() {
return configValue;
}

public boolean matchConfigValue(String in) {
return in.equals(configValue);
}
}

/** /**
* @param repo * @param repo
*/ */
*/ */
public PullCommand setRebase(boolean useRebase) { public PullCommand setRebase(boolean useRebase) {
checkCallable(); checkCallable();
pullRebaseMode = useRebase ? PullRebaseMode.REBASE : PullRebaseMode.NO_REBASE;
pullRebaseMode = useRebase ? BranchRebaseMode.REBASE
: BranchRebaseMode.NONE;
return this;
}

/**
* Sets the {@link BranchRebaseMode} to use after fetching.
*
* <dl>
* <dt>BranchRebaseMode.REBASE</dt>
* <dd>Equivalent to {@code --rebase} on the command line: use rebase
* instead of merge after fetching.</dd>
* <dt>BranchRebaseMode.PRESERVE</dt>
* <dd>Equivalent to {@code --preserve-merges} on the command line: rebase
* preserving local merge commits.</dd>
* <dt>BranchRebaseMode.INTERACTIVE</dt>
* <dd>Equivalent to {@code --interactive} on the command line: use
* interactive rebase.</dd>
* <dt>BranchRebaseMode.NONE</dt>
* <dd>Equivalent to {@code --no-rebase}: merge instead of rebasing.
* <dt>{@code null}</dt>
* <dd>Use the setting defined in the git configuration, either {@code
* branch.[name].rebase} or, if not set, {@code pull.rebase}</dd>
* </dl>
*
* This setting overrides the settings in the configuration file. By
* default, the setting in the repository configuration file is used.
* <p>
* A branch can be configured to use rebase by default. See
* {@code branch.[name].rebase}, {@code branch.autosetuprebase}, and
* {@code pull.rebase}.
*
* @param rebaseMode
* the {@link BranchRebaseMode} to use
* @return {@code this}
* @since 4.5
*/
public PullCommand setRebase(BranchRebaseMode rebaseMode) {
checkCallable();
pullRebaseMode = rebaseMode;
return this; return this;
} }


Repository.shortenRefName(remoteBranchName), remoteUri); Repository.shortenRefName(remoteBranchName), remoteUri);


PullResult result; PullResult result;
if (pullRebaseMode.rebase) {
if (pullRebaseMode != BranchRebaseMode.NONE) {
RebaseCommand rebase = new RebaseCommand(repo); RebaseCommand rebase = new RebaseCommand(repo);
RebaseResult rebaseRes = rebase.setUpstream(commitToMerge) RebaseResult rebaseRes = rebase.setUpstream(commitToMerge)
.setUpstreamName(upstreamName).setProgressMonitor(monitor) .setUpstreamName(upstreamName).setProgressMonitor(monitor)
.setOperation(Operation.BEGIN).setStrategy(strategy) .setOperation(Operation.BEGIN).setStrategy(strategy)
.setPreserveMerges(pullRebaseMode.preserveMerges)
.setPreserveMerges(
pullRebaseMode == BranchRebaseMode.PRESERVE)
.call(); .call();
result = new PullResult(fetchRes, remote, rebaseRes); result = new PullResult(fetchRes, remote, rebaseRes);
} else { } else {
return this; return this;
} }


private static PullRebaseMode getRebaseMode(String branchName, Config config) {
PullRebaseMode mode = config.getEnum(PullRebaseMode.values(),
ConfigConstants.CONFIG_PULL_SECTION, null,
ConfigConstants.CONFIG_KEY_REBASE, PullRebaseMode.NO_REBASE);
mode = config.getEnum(PullRebaseMode.values(),
/**
* Reads the rebase mode to use for a pull command from the repository
* configuration. This is the value defined for the configurations
* {@code branch.[branchName].rebase}, or,if not set, {@code pull.rebase}.
* If neither is set, yields {@link BranchRebaseMode#NONE}.
*
* @param branchName
* name of the local branch
* @param config
* the {@link Config} to read the value from
* @return the {@link BranchRebaseMode}
* @since 4.5
*/
public static BranchRebaseMode getRebaseMode(String branchName,
Config config) {
BranchRebaseMode mode = config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, ConfigConstants.CONFIG_BRANCH_SECTION,
branchName, ConfigConstants.CONFIG_KEY_REBASE, mode);
branchName, ConfigConstants.CONFIG_KEY_REBASE, null);
if (mode == null) {
mode = config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_PULL_SECTION, null,
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.NONE);
}
return mode; return mode;
} }
} }

+ 46
- 2
org.eclipse.jgit/src/org/eclipse/jgit/lib/BranchConfig.java 파일 보기

*/ */
public class BranchConfig { public class BranchConfig {


/**
* Config values for branch.[name].rebase (and pull.rebase).
*
* @since 4.5
*/
public enum BranchRebaseMode implements Config.ConfigEnum {

/** Value for rebasing */
REBASE("true"), //$NON-NLS-1$
/** Value for rebasing preserving local merge commits */
PRESERVE("preserve"), //$NON-NLS-1$
/** Value for rebasing interactively */
INTERACTIVE("interactive"), //$NON-NLS-1$
/** Value for not rebasing at all but merging */
NONE("false"); //$NON-NLS-1$

private final String configValue;

private BranchRebaseMode(String configValue) {
this.configValue = configValue;
}

@Override
public String toConfigValue() {
return configValue;
}

@Override
public boolean matchConfigValue(String s) {
return configValue.equals(s);
}
}

/** /**
* The value that means "local repository" for {@link #getRemote()}: * The value that means "local repository" for {@link #getRemote()}:
* {@value} * {@value}
* @since 3.5 * @since 3.5
*/ */
public boolean isRebase() { public boolean isRebase() {
return config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
branchName, ConfigConstants.CONFIG_KEY_REBASE, false);
return getRebaseMode() != BranchRebaseMode.NONE;
}

/**
* Retrieves the config value of branch.[name].rebase.
*
* @return the {@link BranchRebaseMode}
* @since 4.5
*/
public BranchRebaseMode getRebaseMode() {
return config.getEnum(BranchRebaseMode.values(),
ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.NONE);
} }


/** /**

Loading…
취소
저장