Get changes between commit and its parent with libgit2sharp

I am working with libgit2sharp (C # shell for libgit2) and have run into problems because it does not have much functionality that I hope for (I hope I can contribute to this soon, it seems like a really useful project)

What I'm trying to do right now is to get a list of files modified from a specific commit and its parent. I will not try to figure out what has changed between the merger and the two parents. I'm more interested in regular commits.

These guys (https://github.com/libgit2/libgit2sharp/issues/89) are working on something similar. I think that their procedure is a good idea, but I'm a little weak in my understanding of the internal elements of GIT (in the user manual for GIT there are many guides for the guide, but not so much in the internal structure).

I'm curious how GIT itself executes the "git diff" command. Presumably GIT does not actually store the delta, but rather the full version of the file (if it has not changed, it will simply point to the existing SHA. This information can be found from various sources, such as http://xentac.net/2012/01/19/ the-real-difference-between-git-and-mercurial.html ). This seems to make it difficult to get changes between the two commits (in my case, the specific commit and its sole parent), since the data is not stored as part of the commit (this is clear if you examine the Commit class in the libgit2sharp Commit.cs file).

What I can get from a commit is a tree. It would be wise to do the following to find this information:

1) Start with the required commit and go through the tree and save all the SHA values ​​in the set.

2) Start with the parent for the desired commit and go through its tree to save all its BLA SHA values ​​in another set.

3) SHA for modified files will be files that are not at the intersection of two sets.

The problem that I see with this approach is that it does not look like there is a way to get the file name from the SHA BLOB value (I do not see anything that can do this in the libbit2sharp Blob.cs file).

I know that there are many aspects of this question, but they are part of this big goal to get a specific piece of data from git.

Thanks.

+6
git libgit2 libgit2sharp
source share
1 answer

What you need, the tree function is different , already exists in libgit2 , as defined in the tree.h header .

The git_tree_diff() function compares two Trees and calls a callback for each difference (add, update, and delete). The callback function is transferred by the git_tree_diff_data structure from the path to the file of the git_tree_diff_data in question, its status, the first and current file models, as well as the previous and current SHA.

From the point of view of LibGit2Sharp, it would be wiser to use the existing libgit2 function rather than repeating their implementation in C #. However, even if you can get inspiration from existing Interop definitions , things tend to quickly become tricky when trying to tame .Net / your own interaction layer.

From your point of view (since the contribution of LibGit2Sharp might not be your main goal;)), another option would be to put the C code in C # , drawing on existing LibGit2Sharp functions to go down the trees. git_tree_diff() (and its satellite functions) is a very clean piece of code, and although it does a fairly complex job, the comments are very clear and helpful.

Literature:

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

Note. . To bind git_tree_diff() , the problem must be opened in libgit2 tracker , requiring the method definition to be updated to be GIT_EXTERN 'd. Otherwise, it will not be accessible from .Net.

UPDATE

Release v0.9.0 LibGit2Sharp eventually brought the Tree to Tree function.

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

Public properties:

  • Added / Modified Rows
  • TreeEntry change collections for each kind of change (e.g., added, changed, ...)
  • Patch diff

You can learn more about this function and how to use TreeChanges by looking at unit tests.

+7
source share

All Articles