Skip to content

Instantly share code, notes, and snippets.

@austintaylor
Created June 25, 2009 20:18
Show Gist options
  • Save austintaylor/136133 to your computer and use it in GitHub Desktop.
Save austintaylor/136133 to your computer and use it in GitHub Desktop.
.git/hooks
#!/bin/bash
# Print out what changed in a nice format.
git log @{1}.. --pretty=format:"%Cblue%an%Creset %ar: %Cred\"%s%b\"%Creset (%h)" --reverse --no-merges
# Print out any new migrations
diff <(ls -l db/migrate/ | grep "[0-9]\{14\}" | sed -e 's/.*\([0-9]\{14\}\).*/\1/g' | sort) <((test -f db/development.sqlite3 && sqlite3 db/development.sqlite3 "select version from schema_migrations" || mysql -uroot `pwd | xargs basename`_development -e "select version from schema_migrations") | grep [0-9] | sort) | grep "< [0-9]\{14\}" | cut -d' ' -f2 | tr '\n' '|' | sed -e 's/\(.*\)|$/"\\\(\1\\\)"/' -e 's/|/\\\|/g' | xargs -J % grep % <(ls -l db/migrate | tail -100) | sed -e 's/.*\([0-9]\{14\}_.*\.rb\)/Pending migration: \1/g'
# Yes, I know about rake db:about_if_pending_migrations, but it's way too slow.
# On the other hand, this makes all kinds of assumptions about your database configuration.
# Looks first for db/development.sqlite3, then for a mysql database called [dirname]_development
# I'm not sure why, but the | tail -100 keeps it from hanging when there are no pending migrations.
# Output any changes to the Gemfile
OUTPUT=$(git diff -w -U0 @{1}.. -- Gemfile | sed -e "1,4d" -e "/@@/d" -e "/[+-] *$/d" -e "s/^\+ *\(.*\)$/ \+ \1/" -e "s/^- *\(.*\)$/ \- \1/")
if [ -n "$OUTPUT" ]
then
echo "** Gemfile changed **"
echo "$OUTPUT"
fi
#!/bin/sh
# Clean up whitespace in any modified files.
if git-rev-parse --verify HEAD > /dev/null
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
old_IFS=$IFS
IFS=$'\n'
for FILE in `git-diff-index --check --cached $against 2>&1 | sed '/^[\*\+]/d' | sed 's/:.*//' | uniq` ; do
sed -ie 's/[[:space:]]*$//' $FILE
rm $FILE"e"
git add $FILE
done
IFS=$old_IFS
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment