4

I am trying to delete a branch in my repo using jgit.

DeleteBranchCommand command = git.branchDelete();
command.setBranchNames("myBranch");
command.setForce(true);
try {
    List<String> deletedBranches = new ArrayList<String>();
deletedBranches = command.call();
System.out.println(deletedBranches.toString());
} catch (GitAPIException e) {
throw new MyInternalErrorException("Error while deleting branch [" + branchName + "]", e);
}

The value of deletedBranches will be [myBranch]

If I check if the branch is still in the repo:

git.getRepository().getRef("myBranch");

I will get true. And this is because cf to jgit javadoc:

getRef(name)

name the name of the ref to lookup. May be a short-hand form, e.g. "master" which is is automatically expanded to "refs/heads/master" if "refs/heads/master" already exists.

It is checking for "refs/heads/myBranch" instead of "myBranch".

Moreover, if I will run the deleteBranch command the second time, the value for deletedBranches will be [refs/heads/myBranch].

Can someone explain why is this happening and how can I solve this issue? Thank you.

Update

After debugging inside the jgit code, I've noticed that

String fullName = currentRef.getName();

https://github.com/eclipse/jgit/blob/a76a4acf87952249b94f4be29614565541eb8c46/org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java#L133

returns "myBranch" instead of "heads/refs/myBranch" thus it is not executing this piece of code:

                if (fullName.startsWith(Constants.R_HEADS)) {
                    String shortenedName = fullName
                            .substring(Constants.R_HEADS.length());
                    // remove upstream configuration if any
                    final StoredConfig cfg = repo.getConfig();
                    cfg.unsetSection(
                            ConfigConstants.CONFIG_BRANCH_SECTION,
                            shortenedName);
                    cfg.save();
                }

https://github.com/eclipse/jgit/blob/a76a4acf87952249b94f4be29614565541eb8c46/org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java#L158-L167

victorsc
  • 714
  • 9
  • 30

1 Answers1

0

It could be similar to git branch -d, which will refuse to delete a branch not fully merged in its upstream branch.

That is why the examples you see in org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest are using setForce(true) (like a git branch -D):

List<String> deleted = git.branchDelete().setBranchNames("ForDelete")
            .setForce(true).call();
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I agree. That's why I am using `command.setForce(true);` before calling the command, so it should be equivalent to `git branch -D` – victorsc Jun 05 '14 at 09:37
  • @victorsc and there is no exception when calling the command? (since you can wrap it in a '`try`' '`catch`', as in https://github.com/eclipse/jgit/blob/64b0531c35674df21ed02ab96d5f5bc6f5af7052/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java#L293-L298) – VonC Jun 05 '14 at 09:47
  • I simplified a bit the code in my question. Actually I have it wrapped in try { deletedBranches = command.call(); System.out.println(deletedBranches.toString()); } catch (GitAPIException e) { throw new MyInternalErrorException("Error while deleting branch [" + branchName + "]", e); } – victorsc Jun 05 '14 at 12:01
  • what I do not understand at all is why the first time the command is deleting `[myBranch]` and the second time `[refs/heads/myBranch]` – victorsc Jun 05 '14 at 12:04
  • @victorsc maybe because it doesn't find mybranch the second time , so it will try the extended name. – VonC Jun 05 '14 at 12:14
  • then how is it possible to pass only "ForDelete" as parameter in the delete command but actually to remove Constants.R_HEADS + "ForDelete" https://github.com/eclipse/jgit/blob/64b0531c35674df21ed02ab96d5f5bc6f5af7052/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java#L299-L302 This is exactly what I am trying to achieve – victorsc Jun 05 '14 at 12:20
  • @victorsc maybe because on the first call, the branch still exists, and its short name is returned? See https://github.com/eclipse/jgit/blob/a76a4acf87952249b94f4be29614565541eb8c46/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java#L876-L881 – VonC Jun 05 '14 at 15:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55149/discussion-between-victorsc-and-vonc). – victorsc Jun 05 '14 at 16:11