Delete branch with jgit not working as expected

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]

  • git fetch doesn't fetch all branches
  • Can a ref stored outside refs/heads be considered a branch and checked-out and worked-on as a normal branch?
  • best way to versionize different git branches
  • Can I recover a branch after its deletion in Git?
  • how can I figure out when a branch in git was merged into the trunk
  • How to get git branch of colleague who was fired
  • 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

  • Best way to incorporate external django app into a project and safely make local changes
  • Will git push commits from all branches automatically?
  • Git Development vs Release branch best practices
  • Clean git repo on Heroku
  • User/Repository Hierarchy in GIT
  • Babel not working in git hook
  • One Solution collect form web for “Delete branch with jgit not working as expected”

    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();
    
    Git Baby is a git and github fan, let's start git clone.