10

I am working on a project using JGit. I managed to delete a branch, but I also want to check if that branch is checked out or not. I found a variable in CheckoutCommand but it is private:

private boolean isCheckoutIndex() {
    return startCommit == null && startPoint == null;
}

No public method returns what I want. When I use the command below on a checked out branch it returns an error that the branch cannot be deleted, so I want to check first if is checked out or not.

git.branchDelete().setForce(true).setBranchNames(branchName).call();
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
marius9228
  • 113
  • 1
  • 7

1 Answers1

17

Repository::getFullBranch() returns the full name (e.g. refs/heads/main) of the currently checked out branch, if any. Otherwise the ID of the HEAD commit is returned or null, if there is no commit.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Works fine! I have one more question. How get the commit message when ammend button is pressed? – marius9228 Jul 28 '14 at 13:23
  • See [this question](http://stackoverflow.com/questions/12342152/jgit-and-finding-the-head) for how to use `Repository#resolve()` and `RevWalk` to obtain the commit id of a ref. Note that you should dispose of the `RevWalk`after using it with `revWalk.dispose()`. – Rüdiger Herrmann Jul 28 '14 at 18:37
  • A sample about how to get the commit message for a given commit is now available in the jgit-cookbook at https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/api/GetCommitMessage.java – centic Jul 28 '14 at 18:55