diff options
author | Stefan Lay <stefan.lay@sap.com> | 2011-04-06 14:58:48 +0200 |
---|---|---|
committer | Stefan Lay <stefan.lay@sap.com> | 2011-04-06 14:58:48 +0200 |
commit | 792b93bc62bb3714eb04ccf6e0c800d3b5b85f5b (patch) | |
tree | 255e588995626e8c0bad4d407fe6e2e9dd817a6b /org.eclipse.jgit | |
parent | e24005de2d28bf6c246e2ce4e5413ae136920877 (diff) | |
download | jgit-792b93bc62bb3714eb04ccf6e0c800d3b5b85f5b.tar.gz jgit-792b93bc62bb3714eb04ccf6e0c800d3b5b85f5b.zip |
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 <stefan.lay@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java | 48 |
1 files changed, 38 insertions, 10 deletions
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 b41a44a0e3..52d9e21b09 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 @@ public class CloneCommand implements Callable<Git> { 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 @@ public class CloneCommand implements Callable<Git> { 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 @@ public class CloneCommand implements Callable<Git> { } } + 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 { |