问题
For example, the .gitattributes
file of lg2s
has the line *.cs diff=csharp
. the output of the codes
using (var repo = new Repository(@"path\to\lg2s"))
{
var tree1 = repo.Lookup<Commit>("a845db9").Tree;
var tree2 = repo.Lookup<Commit>("d677741").Tree;
var patches = repo.Diff.Compare<Patch>(tree1, tree2);
foreach (var patch in patches)
{
Console.WriteLine(patch.Patch);
}
}
is (shortly)
diff --git a/LibGit2Sharp/RepositoryStatus.cs b/LibGit2Sharp/RepositoryStatus.cs
@@ -59,8 +59,8 @@ namespace LibGit2Sharp
but the output of git bash
is (shortly)
diff --git a/LibGit2Sharp/RepositoryStatus.cs b/LibGit2Sharp/RepositoryStatus.cs
@@ -59,8 +59,8 @@ internal RepositoryStatus(Repository repo, StatusOptions optio
the second line not same.
Looks like there are an in-depth discussion Add .gitattributes support
libgit2/#508, but the PR Add APIs for git attributes
libgit2/#516 is merged or not?
How many languages were supported now?
Can we specify the rules without gitattributes
file?
回答1:
This is actually an issue of diff drivers and custom function context rules for diffs, not one of attributes.
Those files do have the diff=csharp
attribute; the problem is that the attribute doesn't mean anything to libgit2
.
When git
and libgit2
generate diffs, they use regular expressions to find the name of the function in which a diff hunk occurs. They also have a fallback rule if no regular expression is available to find the first line that did not start with whitespace.
git
contains a table of built in diff drivers that includes a definition of the csharp
driver.
libgit2
does not contain that table (as discussed in the PR that merged diff driver support) and hence has no built in notion of what pattern to use for diff=csharp
.
Your alternatives for solving this are either:
- Make an entry in your
.git/config
file (or your$HOME/.gitconfig
) that implementsdiff.csharp.xfuncname
with a pattern to search, or - Start a Pull Request to
libgit2
that imports the driver definitions. The issue with importing (and the reason I didn't take it on) is that it will involve getting permission from the original authors of the code to relicense it for use inlibgit2
).
If you decide to start a Pull Request and you get the author of the CSharp xfuncname patterns to agree to relicense the code for libgit2
, let me know and I'll be happy to collaborate on the actual implementation!
来源:https://stackoverflow.com/questions/21096188/how-to-apply-diff-rules-of-the-languages-in-gitattributes