admin管理员组文章数量:1357121
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!
本文标签: jgitHow to determine detached head stateStack Overflow
版权声明:本文标题:jgit - How to determine detached head state - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744073441a2586309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论