Git: Post-merge hook to detect when certain files have changed and display a notification

0 Comments

When certain files in a project change, the project needs to be rebuilt/restarted/recompiled. Using the following git hook, you can keep an eye out for changes in certain files then display a reminder message to perform an update or just make it run the command automatically.

It's just a simple bash script that's executed by git after a successful merge.

Use check_run() to run the script automatically, or check_warn() if you want it to only be noisy about it.

Store this in your_project/.git/hooks/ as "post-merge".

#!/usr/bin/env bash
#
# Save this to yourproject/.git/hooks/post-merge
#
# After a successful merge, check if bower.json, package.json
# or requirements has changed and notify the user to take
# appropriate actions.
#
# https://github.com/git/git/blob/master/Documentation/githooks.txt#L167
# This hook cannot affect the outcome of 'git merge' and is not executed,
# if the merge failed due to conflicts.
#
# Based off https://gist.github.com/sindresorhus/7996717
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
# This method checks and notifies
check_warn() {
echo "$changed_files" | grep --quiet "$1" && echo -e "\e[1;31m$1 changed: Please run:\e[39m $2\e[0m"
}
# This method checks and runs automatically
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
check_run package.json "npm install"
check_run bower.json "bower update"
check_warn requirements.txt "pip install -r requirements.txt"
view raw post-merge hosted with ❤ by GitHub

14QJ3Mv
Automate things. Less room for errors and mistakes.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog