From 792b93bc62bb3714eb04ccf6e0c800d3b5b85f5b Mon Sep 17 00:00:00 2001 From: Stefan Lay Date: Wed, 6 Apr 2011 14:58:48 +0200 Subject: [PATCH] Try to checkout branch after cloning When no branch was specified in the clone command, HEAD pointed to a commit after clone. Now the clone command tries to find a branch which points to the same commit and checks out this branch. Bug: 339354 Change-Id: Ie3844465329f213dee4a8868dbf434ac3ce23a08 Signed-off-by: Stefan Lay --- .../eclipse/jgit/api/CloneCommandTest.java | 19 +++++++- .../eclipse/jgit/api/GitConstructionTest.java | 4 +- .../org/eclipse/jgit/api/CloneCommand.java | 48 +++++++++++++++---- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index 51ae054da..f2ad9e5f1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -50,6 +50,7 @@ import java.io.IOException; import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.RefUpdate; @@ -99,6 +100,20 @@ public void testCloneRepository() { assertNotNull(git2); ObjectId id = git2.getRepository().resolve("tag-for-blob"); assertNotNull(id); + assertEquals(git2.getRepository().getFullBranch(), + "refs/heads/test"); + assertEquals( + "origin", + git2.getRepository() + .getConfig() + .getString(ConfigConstants.CONFIG_BRANCH_SECTION, + "test", ConfigConstants.CONFIG_KEY_REMOTE)); + assertEquals( + "refs/heads/test", + git2.getRepository() + .getConfig() + .getString(ConfigConstants.CONFIG_BRANCH_SECTION, + "test", ConfigConstants.CONFIG_KEY_MERGE)); } catch (Exception e) { fail(e.getMessage()); } @@ -109,14 +124,14 @@ public void testCloneRepositoryWithBranch() { try { File directory = createTempDirectory("testCloneRepositoryWithBranch"); CloneCommand command = Git.cloneRepository(); - command.setBranch("refs/heads/test"); + command.setBranch("refs/heads/master"); command.setDirectory(directory); command.setURI("file://" + git.getRepository().getWorkTree().getPath()); Git git2 = command.call(); assertNotNull(git2); assertEquals(git2.getRepository().getFullBranch(), - "refs/heads/test"); + "refs/heads/master"); } catch (Exception e) { fail(e.getMessage()); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GitConstructionTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GitConstructionTest.java index cfaac6352..4075586b5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GitConstructionTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GitConstructionTest.java @@ -78,7 +78,7 @@ public void testWrap() { assertEquals(1, git.branchList().call().size()); git = Git.wrap(bareRepo); - assertEquals(1, git.branchList().setListMode(ListMode.ALL).call() + assertEquals(2, git.branchList().setListMode(ListMode.ALL).call() .size()); try { @@ -95,7 +95,7 @@ public void testOpen() throws IOException { assertEquals(1, git.branchList().call().size()); git = Git.open(bareRepo.getDirectory()); - assertEquals(1, git.branchList().setListMode(ListMode.ALL).call() + assertEquals(2, git.branchList().setListMode(ListMode.ALL).call() .size()); git = Git.open(db.getWorkTree()); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index b41a44a0e..52d9e21b0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -139,11 +139,6 @@ private FetchResult fetch(Repository repo, URIish u) config.addFetchRefSpec(refSpec); config.update(repo.getConfig()); - repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, - branch, ConfigConstants.CONFIG_KEY_REMOTE, remote); - repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, - branch, ConfigConstants.CONFIG_KEY_MERGE, branch); - repo.getConfig().save(); // run the fetch command @@ -160,16 +155,23 @@ private void checkout(Repository repo, FetchResult result) throws JGitInternalException, MissingObjectException, IncorrectObjectTypeException, IOException { - if (branch.startsWith(Constants.R_HEADS)) { - final RefUpdate head = repo.updateRef(Constants.HEAD); - head.disableRefLog(); - head.link(branch); + Ref head = result.getAdvertisedRef(branch); + if (branch.equals(Constants.HEAD)) { + Ref foundBranch = findBranchToCheckout(result); + if (foundBranch != null) + head = foundBranch; } - final Ref head = result.getAdvertisedRef(branch); if (head == null || head.getObjectId() == null) return; // throw exception? + if (head.getName().startsWith(Constants.R_HEADS)) { + final RefUpdate newHead = repo.updateRef(Constants.HEAD); + newHead.disableRefLog(); + newHead.link(head.getName()); + addMergeConfig(repo, head); + } + final RevCommit commit = parseCommit(repo, head); boolean detached = !head.getName().startsWith(Constants.R_HEADS); @@ -185,6 +187,32 @@ private void checkout(Repository repo, FetchResult result) } } + private Ref findBranchToCheckout(FetchResult result) { + Ref foundBranch = null; + final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD); + for (final Ref r : result.getAdvertisedRefs()) { + final String n = r.getName(); + if (!n.startsWith(Constants.R_HEADS)) + continue; + if (idHEAD == null) + continue; + if (r.getObjectId().equals(idHEAD.getObjectId())) { + foundBranch = r; + break; + } + } + return foundBranch; + } + + private void addMergeConfig(Repository repo, Ref head) throws IOException { + String branchName = Repository.shortenRefName(head.getName()); + repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, + branchName, ConfigConstants.CONFIG_KEY_REMOTE, remote); + repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION, + branchName, ConfigConstants.CONFIG_KEY_MERGE, head.getName()); + repo.getConfig().save(); + } + private RevCommit parseCommit(final Repository repo, final Ref ref) throws MissingObjectException, IncorrectObjectTypeException, IOException {