From 544f65e655c12cbf7ea8c185cdf59ecee996d9b3 Mon Sep 17 00:00:00 2001 From: Robin Stocker Date: Tue, 5 Aug 2014 23:38:07 +1000 Subject: [PATCH] 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 --- .../eclipse/jgit/api/BranchCommandTest.java | 11 +++- .../eclipse/jgit/api/CheckoutCommandTest.java | 65 ++++++++++++++----- .../org/eclipse/jgit/api/CheckoutCommand.java | 25 ++++--- .../eclipse/jgit/api/CreateBranchCommand.java | 22 +++---- 4 files changed, 81 insertions(+), 42 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java index c0e2e2ca8..910a645e2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java @@ -473,9 +473,16 @@ public class BranchCommandTest extends RepositoryTestCase { } @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, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java index 614cdd0ce..8487f880f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java @@ -56,16 +56,22 @@ import static org.junit.Assert.fail; 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; @@ -201,29 +207,37 @@ public class CheckoutCommandTest extends RepositoryTestCase { } @Test - public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception { - // create second repository - Repository db2 = createWorkRepository(); - Git git2 = new Git(db2); + 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)); + } - // 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(); + @Test + public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception { + Repository db2 = createRepositoryWithRemote(); - // 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 class CheckoutCommandTest extends RepositoryTestCase { 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"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java index 77106f415..0eb25cf11 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java @@ -213,7 +213,10 @@ public class CheckoutCommand extends GitCommand { 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 class CheckoutCommand extends GitCommand { 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 @@ public class CheckoutCommand extends GitCommand { 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 @@ public class CheckoutCommand extends GitCommand { 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; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java index 92e8b466b..a4ad2c9bf 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java @@ -133,7 +133,7 @@ public class CreateBranchCommand extends GitCommand { 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 class CreateBranchCommand extends GitCommand { baseCommit = startCommit.getShortMessage(); else { RevCommit commit = revWalk.parseCommit(repo - .resolve(startPoint)); + .resolve(getStartPointOrHead())); baseCommit = commit.getShortMessage(); } if (exists) @@ -275,24 +275,22 @@ public class CreateBranchCommand extends GitCommand { } } - 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))