Skip to content

Instantly share code, notes, and snippets.

@hovissimo
Last active February 5, 2019 16:05
Show Gist options
  • Save hovissimo/cdc0db8abe2f25bcd5276a33cb66a9cc to your computer and use it in GitHub Desktop.
Save hovissimo/cdc0db8abe2f25bcd5276a33cb66a9cc to your computer and use it in GitHub Desktop.
.git/hooks/post-merge
#! /usr/bin/env zsh
# .git/hooks/post-merge
# Author: Hovis
# Adapted from various online resources
# Uncomment this if you use ctags. If you're uncertain that means you don't use ctags.
#.git/hooks/ctags >/dev/null 2>&1 &
# These files were updated in the merge
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
#
check_run() {
# Display a notification and offer to run a command.
response=""
echo "$changed_files" | grep "$1" --quiet && # was our first argument a changed file?
echo && # newline
vared -cp " [ $1 ] was modified in the last merge. Shall we handle it automatically? [Y/n]: " response && # input
[[ -z $response || $response =~ [yY] || $response =~ [yY][eE][sS] ]] && # test response from input
eval "$2" # if everything works out, run the second argument as a command
}
announce_change() {
# Display a notification that a file was changed
echo "$changed_files" | grep --quiet "$1" && echo " ===> $1 has been modified!"
}
# Instead of running the docker-compose commands directly with check_run, we set flags. This allows
# the user to decide which tasks to run before starting them, and then the runs are also batched.
#announce_change package.json
check_run package.json "run_yarn=1"
#announce_change Gemfile
check_run Gemfile "run_bundle=1"
#announce_change structure.sql
check_run structure.sql "run_migrate=1"
[[ -n $run_yarn ]] && docker-compose run assets yarn install
[[ -n $run_bundle ]] && docker-compose run rails bundle install
[[ -n $run_migrate ]] && docker-compose run web rake db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment