为什么 github/gitlab 错误地应用了 gitattributes?

问题描述

这是我发现的类似问题:Make github use .gitattributes "binary" attribute 如果它不打算在 Github 中工作,那么我的问题仅适用于 Gitlab。


问题是:想象一下创建一个带有单个文本 Dockerfile 并跟随 .gitattributes 的分支:

[core]
    whitespace=trailing-space,space-before-tab
[apply]
    whitespace=fix
* binary
Dockerfile text=auto diff merge

你推送这段代码,你可以在界面中查看提交,它工作得很好。 然后您决定 Dockerfile 也应该是二进制的(无论出于何种原因)并删除最后一行。您提交它,然后更改 Dockerfile 以检查它是否有效。您调用 git show,它显示确实应用了更改:

pzixe@ZPC MINGW64 ~/Documents/Repos/gitlab-test (all-binary)
$ git show HEAD
commit 141451116e292ae30a515920e6efb906f84b4142 (HEAD -> all-binary,origin/all-binary,github/all-binary)
Author: Psilon <pzixel@gmail.com>
Date:   Wed Apr 14 19:25:04 2021 +0300

    test changes

diff --git a/Dockerfile b/Dockerfile
index 11f0f66..fef4bc1 100644
Binary files a/Dockerfile and b/Dockerfile differ

但是现在,如果您检查 github 界面,您会发现它实际上显示的是文本差异而不是所询问的二进制差异:

enter image description here

然后您尝试查看 GitLab 的工作方式是否有所不同,然后发现不是:

enter image description here

所以问题是:有什么办法可以使它起作用吗?我希望看到初始提交 6c0745e 的文本差异和最新提交 1414511

的二进制折叠差异

这是一个带有复制案例的仓库:https://github.com/Pzixel/test-gitlab

解决方法

您的 .gitattributes 文件的语法无效,因此可能会被完全忽略。前四行(转载如下)是配置语法,不能包含在 .gitattributes 文件中。配置不能作为存储库的一部分提供,需要存储在 .git/config 或您的个人 .gitconfig 中。

[core]
    whitespace=trailing-space,space-before-tab
[apply]
    whitespace=fix

除此之外,作为 outlined in this answer,GitHub 不使用 binary 中的 .gitattributes 属性来确定应该比较哪些文件。它将区分那些看起来是纯文本的文件(并且,默认情况下,不是自动生成的),并且不会区分看起来是二进制的文件。不过,您可以将文件标记为 linguist-generated 以表明它是生成的文件。

我不知道 GitLab 是否支持此功能,但鉴于您的 .gitattributes 文件包含无效语法,这可能无济于事。