I am writing a pre-commit hook. I want to run php -l
against all files with .php extension. However I am stuck.
I need to obtain a list of new/changed files that are staged. deleted files should be excluded.
I have tried using git diff
and git ls-files
, but I think I need a hand here.
git diff --cached --name-status
will show a summary of what's staged, so you can easily exclude removed files, e.g.:
M wt-status.c
D wt-status.h
This indicates that wt-status.c was modified and wt-status.h was removed in the staging area (index). So, to check only files that weren't removed:
steve@arise:~/src/git <master>$ git diff --cached --name-status | awk '$1 != "D" { print $2 }'
wt-status.c
wt-status.h
You will have to jump through extra hoops to deal with filenames with spaces in though (-z option to git diff and some more interesting parsing)