Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgit2 (fetch & merge & commit)

Tags:

libgit2

I 'm trying to pull from a repo with libgit2.

My steps are these:

  • git_remote_connect - OK
  • git_remote_download or should I use git_remote_fetch ?
  • git_remote_ls to get the of the HEAD to pass to git_annotated_commit_from_fetchhead (is this coorect?). But there are more than 1 heads, do I pass the one with "HEAD" name?
  • git_merge.

This results in MERGE_HEAD in .git folder and then I am able to merge with an existing commit.

The question is, is the above sequence correct? Why git creates FETCH_HEAD whereas libgit2 has MERGE_HEAD?

like image 473
Michael Chourdakis Avatar asked Jan 03 '15 21:01

Michael Chourdakis


1 Answers

You probably want to use git_remote_fetch, not git_remote_download. fetch is a convenience function that performs the download and updates things like the remote tracking branches to point to the data that is on the server and - more importantly for you - the FETCH_HEAD file.

You can determine which branch to feed to git_merge by examining the FETCH_HEAD lines and identifying the branch that is not marked as not-for-merge. The simplest way to do this is to iterate over them using git_repository_fetchhead_foreach. Your callback will be invoked for each line, and you simply need to make note of the one that has for_merge set to true. The branch marked as for-merge will be the remote branch corresponding to the merge line in your config.

That is to say that if your HEAD points to master, and your configuration is:

[branch "master"]
    remote = origin
    merge = refs/heads/master

Then when you fetched from origin, the corresponding remote master branch would be marked as for-merge. Once you have identified this FETCH_HEAD entry, then you can create an annotated commit from it and call git_merge.

Note that both git core and libgit2 will create FETCH_HEADs when fetching and MERGE_HEADs when merging. You will only see the latter when the merge is ongoing - 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 producing the same data using libgit2.

like image 128
Edward Thomson Avatar answered Sep 27 '22 20:09

Edward Thomson