Skip to content

Instantly share code, notes, and snippets.

@Sitebase
Created June 22, 2013 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sitebase/5840540 to your computer and use it in GitHub Desktop.
Save Sitebase/5840540 to your computer and use it in GitHub Desktop.
Bash function to get list of files that will be committed. Very handy when writing Git hooks.
# Function to get a list of files that will be committed by extension
# you can for example do "$(commit_files js css)" to get a list of js and css files that wil lbe commited
function commit_files() {
if [ $# -eq 0 ] ; then
echo $(git diff-index --name-only --diff-filter=ACM --cached HEAD --)
exit 0
fi
extensions=''
for extension in "$@"
do
extensions="${extensions}(${extension})|"
done
regex="\.(${extensions%?})$"
echo $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- | grep -P "$regex")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment