Frage

I am planning to replace the usage of git.exe from windows path by libgit2sharp for my plugin GitDiffMargin, A Visual Studio 2012 extension to display Git Diff on the margin of the current file. - https://github.com/laurentkempe/GitDiffMargin

I would like to know if there is an equivalent in libgit2sharp to start the external difftool using git difftool -y filename ?

War es hilfreich?

Lösung

I don't think it should be the responsibility of LibGit2Sharp to launch an external process. LibGit2Sharp goal is to provide a way to manipulate a git repository easily.

It means you can use it to:

  • Get the diff between files in the workdir and the previous version (in the index). To do that you can use the Repository.Diff.Compare(IEnumerable<string> paths, bool includeUntracked, ExplicitPathsOptions explicitPathsOptions) overload, which returns a TreeChanges object. From there, you can get a TreeEntryChanges object through the indexer of the treeChanges, which corresponds to the changes of a given file (use the .Patch property to get the actual content of the patch).

  • Get the configured diff tool by using the Repository.Config namespace (e.g.: repo.Config.Get<string>("diff.tool").Value, although you should also check if the value returned by the Get() method is null in case no diff tool has been configured by the user). That way, you can launch the diff tool by yourself.

Additional resources (v0.11.0):


Note: it seems that at some point, you will need to know if a line has changed or not. I don't think there is an easy way to do that right now (apart from parsing the patch content manually). However, opening an issue on the LibGit2Sharp issue tracker might trigger some discussion around that (feel free to weigh in what kind of API you would like to have to do that!).


Edit: Before launching the external diff tool, you will need to copy the content of the file which is in the index in a temporary folder. You can lookup the blob of a file in the index by doing something like:

  • var indexEntry = repo.Index[fileName];
  • var blob = repo.Lookup(indexEntry.Id);

However... no filters are currently applied when you get the blob content, so the comparison is likely to produce false positives due to crlf differences. There is currently an issue opened on libgit2 in order to propose an API to allow applying filters.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top