Get changes between a commit and its parent with libgit2sharp

試著忘記壹切 提交于 2019-12-03 12:47:21

What you're after, a tree diffing feature, already exists in libgit2 as defined in tree.h header.

The git_tree_diff() function compares two Trees and invokes a callback for every difference (addition, updation and deletion). The callback function is being passed a git_tree_diff_data structure with the filepath of the considered blob, its status, the former and current filemodes and the former and current SHAs.

From LibGit2Sharp perspective, it would make more sense to leverage existing libgit2 feature rather than re-implementing them in C#. However, even if you can get some inspiration from existing Interop definitions, things tend to become quickly tricky when trying to tame .Net/native interop layer.

From your perspective (as contributing to LibGit2Sharp may not your primary goal ;)), another option would be to port the C code to C#, relying on LibGit2Sharp existing features to walk down the trees. git_tree_diff() (and its satellite functions) is a very clean piece of code, and although it does quite a complex job, the comments are very clear and helpful.

References:

  • The git_tree_diff() function is implemented in src/tree.c
  • Tests exercising this feature are available here

Note: In order to bind git_tree_diff(), an issue should be opened in libgit2 tracker requesting that the method definition should be updated in order to be GIT_EXTERN'd. Otherwise it won't be accessible from .Net.

UPDATE

Release v0.9.0 of LibGit2Sharp eventually brought the Tree to Tree diffing feature.

TreeChanges changes = repo.Diff.Compare(fromTree, newTree);

Exposed properties are:

  • Added/Modified lines
  • Collections of TreeEntry changes per kind of change (eg. Added, Modified, ...)
  • The diff Patch

You can find more about this feature and how to leverage the TreeChanges by taking a look at the unit tests in DiffTreeToTreeFixture.cs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!