Skip to content

Instantly share code, notes, and snippets.

@j13k
Forked from lyrixx/post-checkout
Last active February 28, 2017 05:34
Show Gist options
  • Save j13k/c38654ca6e55f8c48c8ec0da599e162d to your computer and use it in GitHub Desktop.
Save j13k/c38654ca6e55f8c48c8ec0da599e162d to your computer and use it in GitHub Desktop.
A post-checkout hook script to check Composer package status and warn of changes after switching branches
#!/bin/bash
#
# A post-checkout hook script to check Composer package status
# and warn of changes after switching branches.
#
# Put this file at: .git/hooks/post-checkout
# and make it executable
# You can install it system wide too, see http://stackoverflow.com/a/2293578/685587
PREV_COMMIT=$1
POST_COMMIT=$2
NO_COLOR='\x1B[0m'
RED_COLOR='\x1B[37;41m'
if [[ -f composer.lock ]]; then
DIFF=`git diff --shortstat ${PREV_COMMIT}..${POST_COMMIT} composer.lock`
if [[ ${DIFF} != "" ]]; then
PACKAGE_COUNT=`composer install --dry-run --no-ansi 2>&1 | egrep " - (Uninstalling|Installing|Updating)" | wc -l | tr -d ' '`
if [[ ${PACKAGE_COUNT} != "0" ]]; then
echo -e "$RED_COLOR*** composer.lock has changed and $PACKAGE_COUNT package(s) are out of sync. You should run composer install. ***$NO_COLOR"
fi
fi
fi
@j13k
Copy link
Author

j13k commented Jul 21, 2016

Fork includes:

  • additional test to check for >0 package changes before issuing warning,
  • ANSI codes suitable for Mac OS X, and
  • additional comment description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment