How to get the name of the current branch in libgit2?

I am trying to use libgit2 to read the name of the current branch. Do I have to make some kind of decision?

I tried to use

git_branch_lookup 

to search for git_reference for HEAD , but this leads to

 Unable to find local branch 'HEAD' 

Thanks!

+4
source share
1 answer

Running git branch -a does not display HEAD . In libgit2, HEAD not considered a valid branch. This is just a link .

If you want to know which link is the current branch, then you should

  • Download the current HEAD link (try the git_repository_head() method)
  • Define its type (using git_reference_type() )
  • Depending on its type ( GIT_REF_SYMBOLIC or GIT_REF_OID ), select one of the following
    • Branch name (using git_reference_symbolic_target() )
    • The git_reference_target() pointed to (using git_reference_target() )
+6
source

All Articles