Using git diff with xargs -0, spaces in file names

1

What I'm attempting to do is extract a list of 'projects' which have changed since a given commit. For this I'm approximating a project to a folder containing a changed file.

The problem I'm having is getting this to work with files which have spaces in their names:

$ git diff --name-only TREEISH.. | xargs -n 1 dirname

I've looked at the -0 option for xargs, and the -z option for diff, but this gives me:

$ git diff --name-only -z TREEISH.. | xargs -0 dirname
dirname: too many arguments

What am I missing?

Benjol

Posted 2012-12-12T09:38:19.613

Reputation: 1 648

Answers

1

Try this:

git diff --name-only -z TREEISH.. | while IFS= read -r n; do dirname "$n"; done 
  • IFS is the input field separator, needed to avoid trimming leading/trailing whitespace
  • -r is needed to avoid backslash processing.
  • The quotes around "$n" make it deal with spaces.

See here for more information.

terdon

Posted 2012-12-12T09:38:19.613

Reputation: 45 216