|
Re: How to clone or checkout a specific branch ? [message #941269 is a reply to message #934097] |
Fri, 12 October 2012 10:50 |
Christian Halstrick Messages: 274 Registered: July 2009 |
Senior Member |
|
|
It's like in native git. When you clone a repo (with jgit or native git) then you get in your local repository
- a remote tracking branch refs/remote/origin/xxx for every branch refs/heads/xxx which exists on the remote repo
- a branch refs/heads/yyy for that branch yyy to which the remote repo's HEAD pointed to. If the remote repo HEAD pointed to a branch called test then after the clone you will have on one local (non-tracking) branch test (and no main branch).
In your case I guess that the remote repo's HEAD pointed to master. If you clone that you'll end up locally with the branches refs/remotes/origin/master, refs/remotes/origin/kalees and refs/heads/master. Therefore it's clear you can checkout "master" but you cannot checkout "kalees". In git (and jgit) when you specify the name of the branch to checkout git will try to find it by prefixing the name with "refs", refs/heads". So master is found (because refs/heads/master exists) and kalees is not found. But origin/kalees can be checked out (because refs/heads/origin/kalees is there).
Here is some example code which clones and checks out every branch:
public static void main(String args[]) throws IOException, GitAPIException,
JGitInternalException {
File tmpDir = new File(System.getProperty("java.io.tmpdir"), "tmp"
+ System.currentTimeMillis());
tmpDir.mkdirs();
try {
Git r = Git.cloneRepository().setDirectory(tmpDir)
.setURI("http://github.com/chalstrick/dondalfi.git")
.setProgressMonitor(new TextProgressMonitor()).call();
r.checkout().setName("origin/test").call();
for (Ref f : r.branchList().setListMode(ListMode.ALL).call()) {
r.checkout().setName(f.getName()).call();
System.out.println("checked out branch " + f.getName()
+ ". HEAD: " + r.getRepository().getRef("HEAD"));
}
// try to checkout branches by specifying abbreviated names
r.checkout().setName("master").call();
r.checkout().setName("origin/test").call();
try {
r.checkout().setName("test").call();
} catch (RefNotFoundException e) {
System.err.println("couldn't checkout 'test'. Got exception: "
+ e.toString() + ". HEAD: "
+ r.getRepository().getRef("HEAD"));
}
} finally {
rm(tmpDir);
}
}
static void rm(File f) {
if (f.isDirectory())
for (File c : f.listFiles())
rm(c);
f.delete();
}
Ciao
Chris
|
|
|
Powered by
FUDForum. Page generated in 0.03138 seconds