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
设置的文件,包括所有通配符匹配?
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
设置的文件,包括所有通配符匹配?
其他帖子对我来说效果不佳,但我到了那里:
git ls-files | git check-attr -a --stdin
“检查 git 中的每个文件并打印所有过滤器”一行。
您可以使用 将包含存储库中所有文件的列表设置为参数git ls-files
,如下所示:
git check-attr myAttr `git ls-files`
如果您的存储库有太多文件,您可能会出现以下错误:
-bash: /usr/bin/git: 参数列表太长
你可以用xargs克服:
git ls-files | xargs git check-attr myAttr
最后,如果您有太多文件,您可能希望过滤掉未指定参数的文件,以使输出更具可读性:
git ls-files | xargs git check-attr myAttr | grep -v 'unspecified$'
使用 grep,您可以对此输出应用更多过滤器,以便仅匹配您想要的文件。
如果您只想获取文件列表,并使用 NUL 字符来恢复包含\n
or的文件名或属性:
,您可以执行以下操作:
对于具有属性“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-tree -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"
以便以人类可读的方式打印它们。