gitattributes:匹配两个名字几乎相同的文件,不同的只是多了一个固定的字符串

gitattributes: Match two files with almost identical names, differing only in an additional fixed string

背景

使用 smarty 模板时,您可以通过创建文件副本并向其添加字符串 -USERMOD 来覆盖文件,如下所示:

original_file.html
original_file-USERMOD.html

有没有办法在我的 .git 属性文件中定位 两个 文件?

只定位第二种类型的文件很容易:

*-USERMOD* export-ignore

从字面上看,规则是这样的:
"If there is a copy of any file that ends in -USERMOD.original_extension, ignore both, the copy and the original"

当然,我总是可以手动添加这些文件,但自动化解决方案会更好。

gitignore(5) 的手册页(由 gitattributes(5) 引用以解释模式匹配)说:

Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag

据我所知,shell glob 不能满足我的需要。然而,最终目标是告诉 git 导出忽略 USERMOD 文件及其原始文件,因此任何解决此问题的解决方案都将被接受。

您将不得不以某种方式对此编写脚本,我强烈建议您将其作为存档创建的一部分。最简单的可能是

USERMOD-aware-archive() {
        git ls-files '*-USERMOD*' \
        | sed 's/$/ export-ignore/;p;s/-USERMOD//' >.git/info/attributes
        git archive "$@"
}

根据我的经验:

original_file*.html

可以解决问题,如果您不介意被忽略的文件,例如:

"original_file[withABunchOfStuffAroundHere].html"

因此,

*-USERMOD* export-ignore

可能是您的存储库的更具体模式