0

Does LibGit2Sharp have an equivalent of the command:
git diff-tree --patch-with-raw --cc <commit>

I could see which files were merged with and without changes

igoryok.zp
  • 63
  • 6

2 Answers2

3

As of 2014-05-27, libgit2 doesn't provide that, I'm afraid.

https://github.com/libgit2/libgit2/pull/1965 has basic logic for determining which files were changed during the merge from the original parents (i.e. changes made to resolve merge conflicts), but it is incomplete and probably needs to be redesigned. Actually generating a patch with the diffs for the merge is further off.

arrbee
  • 241
  • 1
  • 2
0

As of now still, there is still no built-in support. But you can manually achieve it by getting the diff between the merged commit and its parents.

The following function returns the changes between two commits. You have to pass the commit's Tree.

private IEnumerable<TreeEntryChanges> GetDiffOfTrees(LibGit2Sharp.Repository repo, Tree oldTree,Tree newTree, CompareOptions compareOptions)
{
    foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(oldTree, newTree, compareOptions))
    {
        var changeStatus = change.Status;

        if (changeStatus == ChangeKind.Unmodified)
        {
            continue;
        }

        yield return change;

    }
}

And the following function computes the diff between a commit and each of the parents and it returns those changes that are common between the two diffs. These changes are those that are introduced during the merge.

private IEnumerable<TreeEntryChanges> GetDiffOfMergedTrees(Repository gitRepo, IEnumerable<LibGit2Sharp.Commit> parents, Tree tree, CompareOptions compareOptions)
{
        var firstParent = parents.ElementAt(0);
        var secondParent = parents.ElementAt(1);

        var firstChanges = GetDiffOfTrees(gitRepo, firstParent.Tree, tree, compareOptions);
        var secondChanges = GetDiffOfTrees(gitRepo, secondParent.Tree, tree, compareOptions);

        var changes = firstChanges.Where(c1 => secondChanges.Any(c2 => c2.Oid == c1.Oid));

        return changes;
}

So, to get the diff you need just make a call to the GetDiffOfMergedTrees method.

Ehsan Mirsaeedi
  • 6,924
  • 1
  • 41
  • 46