列出具有git属性集的所有文件

10

git check-attr 允许我检查在 .gitattributes 中是否为一组特定的文件设置了属性。 例如:

# git check-attr myAttr -- org/example/file1 org/example/file2
org/example/file1: myAttr: set
org/example/file2: myAttr: unspecified

有没有一种简单的方法可以列出所有具有myAttr设置的文件,包括所有通配符匹配?

3个回答

10
其他帖子对我来说效果不太好,但最终我找到了解决方法:
git ls-files | git check-attr -a --stdin

使用一行命令检查Git中的每个文件并打印所有过滤器。

5

您可以使用git ls-files将包含您存储库中所有文件的列表作为参数进行设置,如下所示:

git check-attr myAttr `git ls-files`

如果您的代码库文件过多,可能会出现以下错误:
-bash: /usr/bin/git: Argument list too long
使用xargs命令可以解决此问题。详情请参考xargs
git ls-files | xargs git check-attr myAttr

最后,如果您有太多的文件,可能希望过滤掉没有指定参数的文件,以使输出更易读:
git ls-files | xargs git check-attr myAttr | grep -v 'unspecified$'

使用grep,您可以对此输出应用更多的过滤器,以匹配您想要的文件。

4
这个解决方案在处理较小的代码库时运行得很好且足够快。但我认为对于大型项目,应该加强git check-attr功能,列出所有已设置属性的文件。 - StackUnderflow

1
如果你只想获取文件列表,并使用NUL字符来保护包含 \n: 的文件名或属性,你可以这样做:
获取具有属性 "merge=union" 的文件列表:
git ls-files -z | git check-attr --stdin -z merge | sed -z -n -f script.sed

使用 script.sed:
             # read filename
x            # save filename in temporary space
n            # read attribute name and discard it
n            # read attribute name
s/^union$//  # check if the value of the attribute match
t print      # in that case goto print
b            # otherwise goto the end
:print
x            # restore filename from temporary space
p            # print filename
             # start again

使用内联的sed脚本(即使用-e而不是-f,忽略注释并将换行符替换为分号)的相同操作:

git ls-files -z | git check-attr --stdin -z merge | sed -zne 'x;n;n;s/^union$//;t print;b;:print;x;p'

PS:该结果使用 NUL 字符分隔文件名,使用 | xargs --null printf "%s\n" 以便以人类可读的方式打印它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接