Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Created November 25, 2016 12:17
Show Gist options
  • Save matthieuprat/b579ed88106d894b19097efc188e1dba to your computer and use it in GitHub Desktop.
Save matthieuprat/b579ed88106d894b19097efc188e1dba to your computer and use it in GitHub Desktop.
#! /bin/bash
PREV_HEAD=$1
HEAD=$2
BRANCH_CHECKOUT=$3
# Skip the hook if the checkout is not a branch checkout.
[ "$BRANCH_CHECKOUT" = 1 ] || exit 0
# Tells wether a file has been modified between 'PREV_HEAD' and 'HEAD'.
was_modified() {
! git diff-tree -r --quiet --diff-filter=M $PREV_HEAD $HEAD -- $1
}
# Runs a command in the background.
# If the command fails, its output is written to the provided path.
run_in_background() {
command="$1"
log_path="$2"
(
if ! output=$(eval "$command" 2>&1); then
[ -z "$log_path" ] || echo "$output" > $log_path
fi &
)
}
if was_modified Gemfile.lock && was_modified db/structure.sql; then
echo 'Running `bundle && rake:db migrate` in the background...'
run_in_background 'bundle && rake db:migrate && git checkout db/structure.sql' db-migrate-error.log
elif was_modified Gemfile.lock; then
echo 'Running `bundle` in the background...'
run_in_background 'bundle' bundle-error.log
elif was_modified db/structure.sql; then
echo 'Running `rake:db migrate` in the background...'
run_in_background 'rake db:migrate && git checkout db/structure.sql' db-migrate-error.log
fi
if was_modified yarn.lock; then
echo 'Running `yarn` in the background...'
# TODO: use --mutex
run_in_background 'yarn --no-emoji'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment