Alex Rivera | Logout

How to remove remote branch with JGit

Asked 2012-08-09T22:15:13.153
8

I'm not able to figure out how to remove a remote branch.

I was trying to mimic the following GIT command: git push origin :branchToDelete

The following code and it's variations with the empty source:

RefSpec refSpec = new RefSpec();
refSpec = refSpec.setSource("");
// remove branch from origin:
git.push().setRefSpecs(refSpec).add(branchToDelete).call();

throws and exception like:

org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of push command
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:175)
    at org.gitscripts.DeleteBranchOperation.execute(DeleteBranchOperation.java:27)
    at org.gitscripts.Main.main(Main.java:27)
Caused by: java.io.IOException: Source ref  doesnt resolve to any object.
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:285)
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:189)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:612)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:1150)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:149)
    ... 2 more

Thanks in advance for your ideas and solutions.

Edit
Report

1 Answer

2

I never did it, but did you simply try a DeleteBranchCommand by specifying origin/branchToDelete?

EDIT : I particularly mean Git/JGit references remote branches via the structure <remote name>/<branch name> (and using the ListBranchCommand will help you make sure you got the correct spelling).

To know the exact spelling of the branch name, you can use a ListBranchCommand (don't forget to call setListMode(REMOTE)).

Note: Git allows more weird behaviours than JGit, so unless it is written somewhere, don't expect them.

EDIT : I mean that a refspec is supposed to have the following syntax: <remote branch>:<local branch> (or probably the other way around), but don't expect it works in JGit if you miss one end, even if it works in Git.

answered 2012-08-10T07:16:09.040

Your Answer