Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass the stdout of `git diff –name-only` to an executable file as parameters of the file

So, there’s this executable file called pint (it’s part of the Laravel 9 framework). It formats PHP files according to a configurable standard like PSR12.
Anyway, you can pass a list of the files you want to format as the parameters of pint like this:

pint file1 file2 file3 file4 ...

If you pass no argument, pint formats all files of the project.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, what I’m trying to do is that I want pint to format only the files that have changed since the last commit. In other words, I want the output of git diff --name-only HEAD^ HEAD to be passed as parameters of pint in bash shell.

So, this is what I could come up with, but sadly it doesn’t work:

git diff --name-only HEAD^ HEAD -o /dev/stdout | ./vendor/bin/pint

Which says: fatal: /dev/stdout: '/dev/stdout' is outside repository at '/home/user/directory' and then it proceeds to execute ./vendor/bin/pint normally as if no argument had been passed.

I suppose I should somehow convert new line to space before passing it to pint but I’m not sure.

>Solution :

| is for piping to standard input, but pint expects the filenames to be command-line arguments, not stdin.

Use $(...) to substitute the output of a command into the command line.

./vendor/bin/pint $(git diff --name-only HEAD^ HEAD)

Note that this won’t work if any of the filenames contain whitespace, since the spaces will be treated as filename delimiters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading