I am using JGit to do some manipulation of a repo.
Is there an easy way for me to be sure that a repo is/isn't in a detached head state?
The best I've come up with so far is to use Repository.getBranch()
, which will return the proper branch name if we're not detached, and an SHA-1 if we're detached.
I can test the return for 40 hex characters, and I admit that the odds of a legitimate branch name being 40 hex characters long are vanishingly small. But I wonder if there's some dead-simple way to do this that I just can't find in the docs.
I am using JGit to do some manipulation of a repo.
Is there an easy way for me to be sure that a repo is/isn't in a detached head state?
The best I've come up with so far is to use Repository.getBranch()
, which will return the proper branch name if we're not detached, and an SHA-1 if we're detached.
I can test the return for 40 hex characters, and I admit that the odds of a legitimate branch name being 40 hex characters long are vanishingly small. But I wonder if there's some dead-simple way to do this that I just can't find in the docs.
Share Improve this question edited Mar 27 at 18:31 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 27 at 18:29 GeePawHillGeePawHill 231 silver badge7 bronze badges 2- I think this is answered here: stackoverflow/questions/24992843/… (if the returned string doesn't start with 'refs/', HEAD points to a commit or nowhere. – Rüdiger Herrmann Commented Mar 27 at 22:21
- Thanks, Rudiger, that was just the clue I needed. – GeePawHill Commented Mar 28 at 17:37
1 Answer
Reset to default 0As Rudiger suggested, the answer is straightforward.
Instead of using repo.getBranch()
to get the branch name, use repo.getFullBranch()
to get it.
The full-branch version returns a string "refs/heads/[branchname]" if we're on a branch, or a raw SHA-1 if we are not.
Confirmed, tested, and pushed.
Thanks!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744073442a4553961.html
评论列表(0条)