6

I'm trying to setup a linter for my code and I only want to lint the coffeescript files that have changed in the current branch. So, I generate the list of files using git:

git diff --name-only develop | grep coffee$

That gives me a nice list of the files I'd like to process but I can't remember how to pipe this to the linting program to actually do the work. Basically I'd like something similar to find's -exec:

find . -name \*.coffee -exec ./node_modules/.bin/coffeelint '{}' \;

Thanks!

  • You're already piping to another program when you git diff | grep. Do you mean passing the previous command's output as arguments, as with xargs or $(...)? – Anko Jul 22 '14 at 17:22
2

Just pipe through a while loop:

git diff --name-only develop | grep coffee$ | while IFS= read -r file; do
    ./node_modules/.bin/coffeelint "$file"
done
  • 1
    Thanks. I actually just got this to work using xargs. git diff --diff-filter=M --name-only develop | grep coffee$ | xargs ./node_modules/.bin/coffeelint – spinlock Jul 22 '14 at 17:34
  • 1
    @spinlock that will work as long as your file names don't contain newlines which is a safe enough assumption. It would be great if you could post that as an answer and accept it. – terdon Jul 22 '14 at 17:39
  • I don't have the karma to answer my own question yet. If I remember I'll come back in a day or two when I can. – spinlock Jul 22 '14 at 17:41
  • @spinlock ah yes, new users need to wait a while. Well, thanks in advance if you do actually remember :) – terdon Jul 22 '14 at 17:43
1

xargs is the unix utility I was looking for. From the man page:

The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments.

Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from the standard input
 of xargs.  The utility is repeatedly executed until standard input is exhausted.

So, the solution to my original question is:

git diff --diff-filter=M --name-only develop | grep coffee$ | xargs ./node_modules/.bin/coffeelint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.