Search code examples
mergepulllibgit2

How to determine if need to git_commit_create() after the merge()?


When I use libgit2 to update data, pull seem to create unnecessary short-term branches and merges which are a headache to resolve. and each merge produced a new oid, so there should be a function to determine if a merge commit is needed before the merge commit. wondering if it is a good practice, then what is the best way to do it?


Solution

  • If you want to analyze whether a merge is needed between two branches, or if instead it is fast-forwardable, use the git_merge_analysis function:

    git_merge_analysis_t analysis;
    git_merge_preference_t preference;
    int error = git_merge_analysis(&analysis, &preference, repo, merge_heads, merge_head_len);
    
    if (analysis == GIT_MERGE_ANALYSIS_FASTFORWARD) {
        /* You can simply do a fast-forward */
    } else {
        /* You need to do a true merge */
    }
    

    You may wish to examine the preference to know whether the user has asked to never fast-forward.