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:
parent
d846610035
commit
544f65e655
|
@ -473,9 +473,16 @@ public void testRenameRemoteTrackingBranch() throws Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreationImplicitStart() throws JGitInternalException,
|
public void testCreationImplicitStart() throws Exception {
|
||||||
GitAPIException {
|
|
||||||
git.branchCreate().setName("topic").call();
|
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,
|
public Ref createBranch(Git actGit, String name, boolean force,
|
||||||
|
|
|
@ -56,16 +56,22 @@
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.CheckoutResult.Status;
|
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.GitAPIException;
|
||||||
import org.eclipse.jgit.api.errors.InvalidRefNameException;
|
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.JGitInternalException;
|
||||||
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
|
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
|
||||||
import org.eclipse.jgit.api.errors.RefNotFoundException;
|
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.DirCache;
|
||||||
import org.eclipse.jgit.dircache.DirCacheEntry;
|
import org.eclipse.jgit.dircache.DirCacheEntry;
|
||||||
import org.eclipse.jgit.junit.RepositoryTestCase;
|
import org.eclipse.jgit.junit.RepositoryTestCase;
|
||||||
|
import org.eclipse.jgit.lib.ConfigConstants;
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
import org.eclipse.jgit.lib.Ref;
|
import org.eclipse.jgit.lib.Ref;
|
||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
|
@ -200,30 +206,38 @@ public void testCheckoutCommit() throws Exception {
|
||||||
assertEquals(initialCommit.name(), git.getRepository().getFullBranch());
|
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
|
@Test
|
||||||
public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
|
public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
|
||||||
// create second repository
|
Repository db2 = createRepositoryWithRemote();
|
||||||
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();
|
|
||||||
// checkout remote tracking branch in second repository
|
// checkout remote tracking branch in second repository
|
||||||
// (no local branches exist yet 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]",
|
assertEquals("[Test.txt, mode:100644, content:Some change]",
|
||||||
indexState(db2, CONTENT));
|
indexState(db2, CONTENT));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCheckoutOfFileWithInexistentParentDir() throws Exception {
|
public void testCheckoutOfFileWithInexistentParentDir() throws Exception {
|
||||||
File a = writeTrashFile("dir/a.txt", "A");
|
File a = writeTrashFile("dir/a.txt", "A");
|
||||||
|
@ -372,6 +386,27 @@ public void testCheckoutOrphanBranch() throws Exception {
|
||||||
assertEquals(CheckoutResult.NOT_TRIED_RESULT, co.getResult());
|
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() {
|
private CheckoutCommand newOrphanBranchCommand() {
|
||||||
return git.checkout().setOrphan(true)
|
return git.checkout().setOrphan(true)
|
||||||
.setName("orphanbranch");
|
.setName("orphanbranch");
|
||||||
|
|
|
@ -213,7 +213,10 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
|
||||||
Git git = new Git(repo);
|
Git git = new Git(repo);
|
||||||
CreateBranchCommand command = git.branchCreate();
|
CreateBranchCommand command = git.branchCreate();
|
||||||
command.setName(name);
|
command.setName(name);
|
||||||
command.setStartPoint(getStartPoint().name());
|
if (startCommit != null)
|
||||||
|
command.setStartPoint(startCommit);
|
||||||
|
else
|
||||||
|
command.setStartPoint(startPoint);
|
||||||
if (upstreamMode != null)
|
if (upstreamMode != null)
|
||||||
command.setUpstreamMode(upstreamMode);
|
command.setUpstreamMode(upstreamMode);
|
||||||
command.call();
|
command.call();
|
||||||
|
@ -234,7 +237,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
|
||||||
this.status = CheckoutResult.NOT_TRIED_RESULT;
|
this.status = CheckoutResult.NOT_TRIED_RESULT;
|
||||||
return repo.getRef(Constants.HEAD);
|
return repo.getRef(Constants.HEAD);
|
||||||
}
|
}
|
||||||
branch = getStartPoint();
|
branch = getStartPointObjectId();
|
||||||
} else {
|
} else {
|
||||||
branch = repo.resolve(name);
|
branch = repo.resolve(name);
|
||||||
if (branch == null)
|
if (branch == null)
|
||||||
|
@ -386,7 +389,7 @@ protected CheckoutCommand checkoutPaths() throws IOException,
|
||||||
if (isCheckoutIndex())
|
if (isCheckoutIndex())
|
||||||
checkoutPathsFromIndex(treeWalk, dc);
|
checkoutPathsFromIndex(treeWalk, dc);
|
||||||
else {
|
else {
|
||||||
RevCommit commit = revWalk.parseCommit(getStartPoint());
|
RevCommit commit = revWalk.parseCommit(getStartPointObjectId());
|
||||||
checkoutPathsFromCommit(treeWalk, dc, commit);
|
checkoutPathsFromCommit(treeWalk, dc, commit);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -468,21 +471,17 @@ private boolean isCheckoutIndex() {
|
||||||
return startCommit == null && startPoint == null;
|
return startCommit == null && startPoint == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectId getStartPoint() throws AmbiguousObjectException,
|
private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
|
||||||
RefNotFoundException, IOException {
|
RefNotFoundException, IOException {
|
||||||
if (startCommit != null)
|
if (startCommit != null)
|
||||||
return startCommit.getId();
|
return startCommit.getId();
|
||||||
ObjectId result = null;
|
|
||||||
try {
|
String startPointOrHead = (startPoint != null) ? startPoint
|
||||||
result = repo.resolve((startPoint == null) ? Constants.HEAD
|
: Constants.HEAD;
|
||||||
: startPoint);
|
ObjectId result = repo.resolve(startPointOrHead);
|
||||||
} catch (AmbiguousObjectException e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
if (result == null)
|
if (result == null)
|
||||||
throw new RefNotFoundException(MessageFormat.format(
|
throw new RefNotFoundException(MessageFormat.format(
|
||||||
JGitText.get().refNotResolved,
|
JGitText.get().refNotResolved, startPointOrHead));
|
||||||
startPoint != null ? startPoint : Constants.HEAD));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,7 +133,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
|
||||||
throw new RefAlreadyExistsException(MessageFormat.format(
|
throw new RefAlreadyExistsException(MessageFormat.format(
|
||||||
JGitText.get().refAlreadyExists1, name));
|
JGitText.get().refAlreadyExists1, name));
|
||||||
|
|
||||||
ObjectId startAt = getStartPoint();
|
ObjectId startAt = getStartPointObjectId();
|
||||||
String startPointFullName = null;
|
String startPointFullName = null;
|
||||||
if (startPoint != null) {
|
if (startPoint != null) {
|
||||||
Ref baseRef = repo.getRef(startPoint);
|
Ref baseRef = repo.getRef(startPoint);
|
||||||
|
@ -151,7 +151,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
|
||||||
baseCommit = startCommit.getShortMessage();
|
baseCommit = startCommit.getShortMessage();
|
||||||
else {
|
else {
|
||||||
RevCommit commit = revWalk.parseCommit(repo
|
RevCommit commit = revWalk.parseCommit(repo
|
||||||
.resolve(startPoint));
|
.resolve(getStartPointOrHead()));
|
||||||
baseCommit = commit.getShortMessage();
|
baseCommit = commit.getShortMessage();
|
||||||
}
|
}
|
||||||
if (exists)
|
if (exists)
|
||||||
|
@ -275,24 +275,22 @@ else if (upstreamMode == SetupUpstreamMode.NOTRACK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectId getStartPoint() throws AmbiguousObjectException,
|
private ObjectId getStartPointObjectId() throws AmbiguousObjectException,
|
||||||
RefNotFoundException, IOException {
|
RefNotFoundException, IOException {
|
||||||
if (startCommit != null)
|
if (startCommit != null)
|
||||||
return startCommit.getId();
|
return startCommit.getId();
|
||||||
ObjectId result = null;
|
String startPointOrHead = getStartPointOrHead();
|
||||||
try {
|
ObjectId result = repo.resolve(startPointOrHead);
|
||||||
result = repo.resolve((startPoint == null) ? Constants.HEAD
|
|
||||||
: startPoint);
|
|
||||||
} catch (AmbiguousObjectException e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
if (result == null)
|
if (result == null)
|
||||||
throw new RefNotFoundException(MessageFormat.format(
|
throw new RefNotFoundException(MessageFormat.format(
|
||||||
JGitText.get().refNotResolved,
|
JGitText.get().refNotResolved, startPointOrHead));
|
||||||
startPoint != null ? startPoint : Constants.HEAD));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getStartPointOrHead() {
|
||||||
|
return startPoint != null ? startPoint : Constants.HEAD;
|
||||||
|
}
|
||||||
|
|
||||||
private void processOptions() throws InvalidRefNameException {
|
private void processOptions() throws InvalidRefNameException {
|
||||||
if (name == null
|
if (name == null
|
||||||
|| !Repository.isValidRefName(Constants.R_HEADS + name))
|
|| !Repository.isValidRefName(Constants.R_HEADS + name))
|
||||||
|
|
Loading…
Reference in New Issue