Using output of `git diff --name-only` for file names with non-ASCII characters

Viewed 308

The output of git diff --name-only is not very useful for file names that are not ASCII. Example:

git init
echo Germany > Düsseldorf.txt
echo Mexico > Cancún.txt
git add *.txt

while read f
    do cat "$f"
done < <(git diff --cached --name-only)

This results in the following output:

cat: '"Canc303272n.txt"': No such file or directory
cat: '"D303274sseldorf.txt"': No such file or directory

How can I obtain a listing of the staged files that's useful for further processing?

1 Answers

How can I obtain a listing of the staged files that's useful for further processing?

Use a zero separated stream.

git diff -z --cached --name-only |
while IFS= read -r -d '' f; do
   cat "$f"
done

See https://mywiki.wooledge.org/BashFAQ/020

Anyway, with just cat, then: git diff -z --cached --name-only | xargs -0 cat.

Related