Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remote HEAD with libgit2?

I can't seem to figure out how to get a git_reference * to a particular remote's HEAD.

I've got:

git_repository * repo = NULL;
git_reference *   ref = NULL;

/* ...load repository... */

if(git_reference_lookup(&ref, repo, "remotes/origin/HEAD"))
    printf("Error obtaining reference to remote HEAD!");

The error message is displayed when the application is run, indicating that the reference wasn't found. However, the remote HEAD does indeed exist:

$: find .git/refs/*
.git/refs/heads
.git/refs/heads/master
.git/refs/remotes
.git/refs/remotes/origin
.git/refs/remotes/origin/HEAD
.git/refs/tags

Why can't I obtain a git_reference * to the remote HEAD?

like image 896
Nathan Osman Avatar asked Feb 01 '26 05:02

Nathan Osman


1 Answers

You need to specify a full path to the reference. For example:

int error = git_reference_lookup(&ref, repo, "refs/remotes/origin/HEAD");

libgit2 could not prefix refs/ for you, or else you would be unable to open references in other places, most notably the HEAD reference.

like image 162
Edward Thomson Avatar answered Feb 04 '26 02:02

Edward Thomson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!