Fix CheckoutCommand not setting up tracking

Instead of passing on the start point as is to CreateBranchCommand, the
resolved ObjectId was used. Given this, CreateBranchCommand did not set
up tracking.

This also fixes CreateBranchCommand with setStartPoint(null) to use HEAD
(instead of NPEing), as documented in the Javadoc.

Bug: 441153
Change-Id: I5ed82b4a4b4a32a81a7fa2854636b921bcb3d471
Signed-off-by: Robin Stocker <robin@nibor.org>
This commit is contained in:
Robin Stocker 2014-08-05 23:38:07 +10:00
parent d846610035
commit 544f65e655
4 changed files with 81 additions and 42 deletions

View File

@ -473,9 +473,16 @@ public void testRenameRemoteTrackingBranch() throws Exception {
}
@Test
public void testCreationImplicitStart() throws JGitInternalException,
GitAPIException {
public void testCreationImplicitStart() throws Exception {
git.branchCreate().setName("topic").call();
assertEquals(db.resolve("HEAD"), db.resolve("topic"));
}
@Test
public void testCreationNullStartPoint() throws Exception {
String startPoint = null;
git.branchCreate().setName("topic").setStartPoint(startPoint).call();
assertEquals(db.resolve("HEAD"), db.resolve("topic"));
}
public Ref createBranch(Git actGit, String name, boolean force,

View File

@ -56,16 +56,22 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.eclipse.jgit.api.CheckoutResult.Status;
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
@ -200,30 +206,38 @@ public void testCheckoutCommit() throws Exception {
assertEquals(initialCommit.name(), git.getRepository().getFullBranch());
}
@Test
public void testCheckoutRemoteTrackingWithUpstream() throws Exception {
Repository db2 = createRepositoryWithRemote();
Git.wrap(db2).checkout().setCreateBranch(true).setName("test")
.setStartPoint("origin/test")
.setUpstreamMode(SetupUpstreamMode.TRACK).call();
assertEquals("refs/heads/test", db2.getRef(Constants.HEAD).getTarget()
.getName());
StoredConfig config = db2.getConfig();
assertEquals("origin", config.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_REMOTE));
assertEquals("refs/heads/test", config.getString(
ConfigConstants.CONFIG_BRANCH_SECTION, "test",
ConfigConstants.CONFIG_KEY_MERGE));
}
@Test
public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
// create second repository
Repository db2 = createWorkRepository();
Git git2 = new Git(db2);
Repository db2 = createRepositoryWithRemote();
// setup the second repository to fetch from the first repository
final StoredConfig config = db2.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
URIish uri = new URIish(db.getDirectory().toURI().toURL());
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
// fetch from first repository
RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*");
git2.fetch().setRemote("origin").setRefSpecs(spec).call();
// checkout remote tracking branch in second repository
// (no local branches exist yet in second repository)
git2.checkout().setName("remotes/origin/test").call();
Git.wrap(db2).checkout().setName("remotes/origin/test").call();
assertEquals("[Test.txt, mode:100644, content:Some change]",
indexState(db2, CONTENT));
}
@Test
public void testCheckoutOfFileWithInexistentParentDir() throws Exception {
File a = writeTrashFile("dir/a.txt", "A");
@ -372,6 +386,27 @@ public void testCheckoutOrphanBranch() throws Exception {
assertEquals(CheckoutResult.NOT_TRIED_RESULT, co.getResult());
}
private Repository createRepositoryWithRemote() throws IOException,
URISyntaxException, MalformedURLException, GitAPIException,
InvalidRemoteException, TransportException {
// create second repository
Repository db2 = createWorkRepository();
Git git2 = new Git(db2);
// setup the second repository to fetch from the first repository
final StoredConfig config = db2.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
URIish uri = new URIish(db.getDirectory().toURI().toURL());
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
// fetch from first repository
RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*");
git2.fetch().setRemote("origin").setRefSpecs(spec).call();
return db2;
}
private CheckoutCommand newOrphanBranchCommand() {
return git.checkout().setOrphan(true)
.setName("orphanbranch");

View File

@ -213,7 +213,10 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
Git git = new Git(repo);
CreateBranchCommand command = git.branchCreate();
command.setName(name);
command.setStartPoint(getStartPoint().name());
if (startCommit != null)
command.setStartPoint(startCommit);
else
command.setStartPoint(startPoint);
if (upstreamMode != null)
command.setUpstreamMode(upstreamMode);
command.call();
@ -234,7 +237,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
this.status = CheckoutResult.NOT_TRIED_RESULT;
return repo.getRef(Constants.HEAD);
}
branch = getStartPoint();
branch = getStartPointObjectId();
} else {
branch = repo.resolve(name);
if (branch == null)
@ -386,7 +389,7 @@ protected CheckoutCommand checkoutPaths() throws IOException,
if (isCheckoutIndex())
checkoutPathsFromIndex(treeWalk, dc);
else {
RevCommit commit = revWalk.parseCommit(getStartPoint());
RevCommit commit = revWalk.parseCommit(getStartPointObjectId());
checkoutPathsFromCommit(treeWalk, dc, commit);
}
} finally {
@ -468,21 +471,17 @@ private boolean isCheckoutIndex() {
return startCommit == null && startPoint == null;
}
private ObjectId getStartPoint() throws AmbiguousObjectException,
private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
RefNotFoundException, IOException {
if (startCommit != null)
return startCommit.getId();
ObjectId result = null;
try {
result = repo.resolve((startPoint == null) ? Constants.HEAD
: startPoint);
} catch (AmbiguousObjectException e) {
throw e;
}
String startPointOrHead = (startPoint != null) ? startPoint
: Constants.HEAD;
ObjectId result = repo.resolve(startPointOrHead);
if (result == null)
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved,
startPoint != null ? startPoint : Constants.HEAD));
JGitText.get().refNotResolved, startPointOrHead));
return result;
}

View File

@ -133,7 +133,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
throw new RefAlreadyExistsException(MessageFormat.format(
JGitText.get().refAlreadyExists1, name));
ObjectId startAt = getStartPoint();
ObjectId startAt = getStartPointObjectId();
String startPointFullName = null;
if (startPoint != null) {
Ref baseRef = repo.getRef(startPoint);
@ -151,7 +151,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
baseCommit = startCommit.getShortMessage();
else {
RevCommit commit = revWalk.parseCommit(repo
.resolve(startPoint));
.resolve(getStartPointOrHead()));
baseCommit = commit.getShortMessage();
}
if (exists)
@ -275,24 +275,22 @@ else if (upstreamMode == SetupUpstreamMode.NOTRACK)
}
}
private ObjectId getStartPoint() throws AmbiguousObjectException,
private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
RefNotFoundException, IOException {
if (startCommit != null)
return startCommit.getId();
ObjectId result = null;
try {
result = repo.resolve((startPoint == null) ? Constants.HEAD
: startPoint);
} catch (AmbiguousObjectException e) {
throw e;
}
String startPointOrHead = getStartPointOrHead();
ObjectId result = repo.resolve(startPointOrHead);
if (result == null)
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved,
startPoint != null ? startPoint : Constants.HEAD));
JGitText.get().refNotResolved, startPointOrHead));
return result;
}
private String getStartPointOrHead() {
return startPoint != null ? startPoint : Constants.HEAD;
}
private void processOptions() throws InvalidRefNameException {
if (name == null
|| !Repository.isValidRefName(Constants.R_HEADS + name))