How to apply diff rules of the languages in gitattributes

旧街凉风 提交于 2019-12-24 01:09:23

问题


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:

  1. Make an entry in your .git/config file (or your $HOME/.gitconfig) that implements diff.csharp.xfuncname with a pattern to search, or
  2. 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 in libgit2).

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

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