.gitattributes使用' * text = auto'带过滤器

时间:2018-02-14 14:34:13

标签: git line-endings gitattributes git-filter

在跨平台项目中,为所有文本文件配置 CRLF规范化的正确方法是什么?* text=auto'连同'涂抹'和'清洁'过滤某些文件' *.h filter=myfilter'?

示例:

# my .gitattributes
* text=auto           # in case people don't have core.autocrlf set
*.h filter=myfilter   # filter for header files

添加*.h filter=myfilter后,我收到警告:

warning: LF will be replaced by CRLF in foo.h.
The file will have its original line endings in your working directory.

如果我删除*.h filter=myfilter,警告就会消失。为什么?该行是否禁用* .h文件的 CRLF规范化

更新

关于警告,过滤器使用sed替换文件标题注释中的标记。在Windows sed上运行时,正在将CRLF转换为LF。这是警告的来源。解决方案是以二进制模式(sed)运行--binary

1 个答案:

答案 0 :(得分:3)

一些实验会产生相当混乱的结果。

The gitattributes documentation预先说明了这一点:

  

当多个模式与路径匹配时,后一行会覆盖前一行。 此覆盖是按属性完成的。

(强调我的)这清楚地表明,虽然*.h filter=myfilter会覆盖filter设置的任何早期匹配,但会影响任何早期的* text=...设置

实际的登记和退房流程似乎表现良好:遵守先前的* text=<whatever>

然而,其他命令似乎将filter视为覆盖text。 (这似乎是警告的来源。)

在这种情况下,您可以通过编写:

来解决问题
*.h text=auto filter=myfilter

但总的来说,这似乎违反了记录在案的行为。