I'm working on an ANT task that calls some java that uses JGit to create a new branch on a git repository and push it to remote. I'm using JGit 2.1.0.
Here's the code:
CreateBranchCommand bcc;
CheckoutCommand checkout;
Git git;
try {
Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir(src).build();
git = new Git(repo);
bcc = git.branchCreate();
checkout = git.checkout();
} catch (IOException e) {
throw new BuildException("Could not access repository " + src, e);
}
try {
bcc.setName(branch)
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint("origin/" + branch)
.setForce(force)
.call();
checkout.setName(branch);
checkout.call();
}
} catch (Exception e) {
throw new BuildException("Could not checkout repository " + src, e);
}
The src variable is set to the path of the git repo (which has already been cloned). The branch variable is set to: release_2_0_2 The force variable is set to: true or false (with both I have this issue).
There is a separate ANT task afterward to do the push.
When running the above code an exception is caught in the 2nd catch:
org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/release_2_0_2 can not be resolved
The issue seems to be with setStartPoint("origin/" + branch)
If I hard code this to "origin/master" it works. The new branch is created. I'm simply trying to create a new branch locally and then push it to the remote. I was using https://stackoverflow.com/a/12928374/1860867 as an example.
Maybe I'm misunderstanding how the CreateBranchCommand should be used, all the examples I've seen are setting the startpoint to "origin/" + branch
.
Any suggestions/clarification would be helpful.