List of all files that have a git attribute set

git check-attr allows you to check whether an attribute is set in .gitattributes for a specific set of files. eg:

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

Is there an easy way to list all files with myAttr , including all wildcards?

+6
source share
3 answers

You can specify as an argument a list with all the files in your repository using git ls-files , for example:

 git check-attr myAttr `git ls-files` 

If there are too many files in your repository, you can make the following message:

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

which you can overcome with xargs :

 git ls-files | xargs git check-attr myAttr 

Finally, if you have too many files, you probably want to filter out those where you did not specify an argument to make the output more readable:

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

With grep, you can apply more filters to this result to match only the files you want.

+3
source

If you want to get only a list of files and use the NUL character to be resistant to file names or attributes that contain \n or : you can do:

For a list of files with the attribute "merge = union":

 git ls-files -z | git check-attr --stdin -z merge | sed -z -n -f script.sed 

With 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 

Same thing with sed script inlined (i.e. using -e instead of -f , ignoring comments and replacing newline characters with a semicolon):

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

PS: the result separates the file names using the NUL character, use | xargs --null printf "%s\n" | xargs --null printf "%s\n" to print them using a human readable file.

+1
source

Other posts did not work well for me, but I got there:

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

"Check each file in git and print all filters" one insert.

+1
source

Source: https://habr.com/ru/post/985331/


All Articles