0

I want to find all of the files in git that have some extensions. The list of extensions is generated from my .editorconfig file.

Say I have a list of file extensions such as:

.css
.html
.java
.js

(This is generated from .editorconfig)

I would like to find all of the files tracked by git that have these extensions.

At the moment, I have this command:

grep -oE "\.[a-z0-9]+" .editorconfig | xargs -I{} grep {} <(git ls-files)

The output is :

src/main/site/src/App.css
src/main/site/src/index.css

The command currently isn't running grep for each file extension, only the first one. What's wrong with the command? I want this to be a single command, without creating any intermediate files.

edit: The reason I generate the list of files from .editorconfig is because I want to check the code styles of each file generated by this command, and more files may be added to the config in the future, so I would like some future-proofing.

2 Answers 2

0

The line

grep -oE "\.[a-z0-9]+" .editorconfig | xargs -I{} grep {} <(git ls-files)

seems weird : you ask xargs to take as an input a pipe from grep and also from (git ls-files). A little trial : echo a | cat <(echo b) indicates the echo b wins ! In your example, git ls-files seems to win which is consistive.

Unfortunately, xargs has only one standard input and you can only search one regex (excepted with -e and -f options).

The best things seems to create a regexfile from your .editorconfig file and launch `git ls-files | grep -f regexfile

2
  • The part <(git ls-files) is supposed to be piped into the stdin of grep, but I suppose its being sent to xargs Oct 7, 2021 at 19:48
  • cat <(echo a b)|xargs echo works as intended ! But if you use xargs grep, warning, grep use the first argument as a regex, and the following as files to open and grep. As I have written above (last edition), you need -e or -f options to make grep search multiple regex. Oct 7, 2021 at 19:56
0

Thanks to @Frédéric Loyer's answer I've solved my problem, but with the compromise of having to create an intermediate file.

I could either of these commands

grep -oE "\.[a-z0-9]+" .editorconfig > out && git ls-files | grep -f out && rm out
git ls-files > out && grep -oE "\.[a-z0-9]+" .editorconfig | xargs -I{} grep {} out && rm out

and they would have more or less the desired effect, but some unwanted files were slipping through.

I realised that the file extensions that were generated from .editorconfig were being used as patterns for grep again, so the leading period had to be escaped, resulting in the following command:

grep -oE "\.[a-z0-9]+" .editorconfig | xargs -I{} echo "\{}" > out && git ls-files | grep -f out && rm out

Slightly shorter alternative using sed:

grep -oE "\.[a-z0-9]+" .editorconfig | sed 's/^/\\/' > out && git ls-files | grep -f out && rm out

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .