Libgit2 (fetch & merge & commit)
I am trying to pull from a repo with libgit2.
My steps:
- git_remote_connect - OK
- git_remote_download or use git_remote_fetch?
- git_remote_ls to get HEAD to go to git_annotated_commit_from_fetchhead (is this a coorect?). But there is more than 1 chapter, can I pass a name named "HEAD"?
- git_merge.
This results in a MERGE_HEAD in the .git folder and then I can merge with the existing commit.
The question is, is the sequence above correct? Why does git create FETCH_HEAD while libgit2 has MERGE_HEAD?
source to share
You probably want to use git_remote_fetch
, not git_remote_download
. fetch
is a convenience feature that downloads and updates things like remote tracking branches to point to the data that is on the server and - more importantly to you - the file FETCH_HEAD
.
You can determine which branch should be pushed to git_merge
by examining the lines FETCH_HEAD
and identifying the branch that is not marked as not-for-merge
. The easiest way to do this is to iterate over them with git_repository_fetchhead_foreach
. The callback will be called for every line and you just need to mark the one that is for_merge
set to true
. The branch marked as for-merge
will be the remote branch that matches the line merge
in your config.
That is, if yours HEAD
points to master
, and your config is:
[branch "master"]
remote = origin
merge = refs/heads/master
Then, when you checked out from origin
, the corresponding remote branch master
will be marked as for-merge
. Once you have defined this record FETCH_HEAD
, you can create an annotated commit and call git_merge
.
Note that both git core and libgit2 will build FETCH_HEAD
on checkout and MERGE_HEAD
merge. You will only see the last one when the merge is in progress, so if you run merge --no-commit
on the command line or if you have a conflict. You can use this to compare the data that git core produces to ensure that you are creating the same data with libgit2.
source to share