2015-04-17 48 views
6

git check-attr允许我检查是否在.gitattributes中为特定文件集设置了属性。 e.g:列出具有git属性集的所有文件

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

有没有一种简单的方法来列出有myAttr集,包括所有的通配符匹配的所有文件?

回答

3

您可以在列表中设置作为参数使用git ls-files在你的仓库中的所有文件,就像这样:

git check-attr myAttr `git ls-files` 

如果你的仓库有太多的文件,则可能出现以下错误:

-bash: /usr/bin/git: Argument list too long

您可以与xargs克服:

git ls-files | xargs git check-attr myAttr 

最后,如果你有太多的文件,你可能会想筛选出的那些,你没有指定参数,使输出更易读:

git ls-files | xargs git check-attr myAttr | grep -v 'unspecified$' 

使用grep,您可以应用多个过滤器这个输出只是为了匹配你想要的文件。

+3

该解决方案对于较小的存储库运行良好且足够快。但我认为对于大型项目'git check-attr'应该增强以列出所有具有属性集的文件。 – StackUnderflow

1

如果你想只得到文件的列表,并使用NULL字符为是弹性的文件名或包含\n:属性,你可以这样做:

对于具有属性“列表文件合并=工会“:

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"以便打印出来以人类可读的方式。

1

其他职位不适合我的工作很好,但我到了那里:

git ls-files | git check-attr -a --stdin 

“检查混帐每个文件和打印所有过滤器”一行。