Git post checkout
| #!/bin/bash | |
| # 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 | |
| NOCOLOR='\e[0m' | |
| REDCOLOR='\e[37;41m' | |
| if [[ -f composer.lock ]]; then | |
| DIFF=`git diff --shortstat $PREV_COMMIT..$POST_COMMIT composer.lock` | |
| if [[ $DIFF != "" ]]; then | |
| echo -e "$REDCOLOR composer.lock has changed. You must run composer install$NOCOLOR" | |
| fi | |
| fi |
|
I use something similar for nodejs/PHP projects. |
|
Brilliant, thanks for that |
|
@jubianchi I'm wondering why I did not do that this before |
|
@lyrixx I asked myself the same question :) Now I'm wondering if it would be possible to automatically run the composer/npm commands... It would be really great if everything was handled without having to do anything ;) |
|
This hook completly automates the process : when a change is detected on #!/bin/bash
# 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
GIT_DIR=$(git rev-parse --git-dir)
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply
GIT_MERGE_REBASE=false
[[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]] && GIT_MERGE_REBASE=true
NOCOLOR='\e[0m'
REDCOLOR='\e[37;41m'
function composer.lock {
echo -e "$REDCOLOR composer.lock has changed: running composer install $NOCOLOR"
COMPOSER=
if [ -f composer.phar ]; then
COMPOSER="php composer.phar"
fi
which composer > /dev/null 2>&1
if [ $? ]; then
COMPOSER="composer"
fi
if [[ $GIT_MERGE_REBASE = false && -n "$COMPOSER" ]]; then
$COMPOSER install
fi
}
function package.json {
echo -e "$REDCOLOR package.json has changed: running npm install $NOCOLOR"
which npm > /dev/null 2>&1
if [[ $GIT_MERGE_REBASE = false && $? ]]; then
npm install
fi
}
FUNCS=$(declare -F -p | cut -d " " -f 3)
for FUNC in $FUNCS
do
DIFF=$(git diff --shortstat $PREV_COMMIT..$POST_COMMIT $FUNC 2>/dev/null)
if [[ $DIFF != "" ]]; then
$FUNC
fi
done
|
|
Using the version by @jubianchi on OS X I get
|
|
Other solution: commit your dependencies. |
|
@fzaninotto: I usually apply the following rule: I only commit the code that I am maintaining. Committing dependencies sometimes lead to a messy or too big repository, depending on what is inside dependencies. @skyzyx here is a potential fix: #!/bin/bash
# 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
GIT_DIR=$(git rev-parse --git-dir)
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply
GIT_MERGE_REBASE=false
[[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]] && GIT_MERGE_REBASE=true
NOCOLOR='\e[0m'
REDCOLOR='\e[37;41m'
function composerlock {
echo -e "$REDCOLOR composer.lock has changed: running composer install $NOCOLOR"
COMPOSER=
if [ -f composer.phar ]; then
COMPOSER="php composer.phar"
fi
which composer > /dev/null 2>&1
if [ $? ]; then
COMPOSER="composer"
fi
if [[ $GIT_MERGE_REBASE = false && -n "$COMPOSER" ]]; then
$COMPOSER install
fi
}
function packagejson {
echo -e "$REDCOLOR package.json has changed: running npm install $NOCOLOR"
which npm > /dev/null 2>&1
if [[ $GIT_MERGE_REBASE = false && $? ]]; then
npm install
fi
}
FUNCS=$(declare -F -p | cut -d " " -f 3)
for FUNC in $FUNCS
do
DIFF=$(git diff --shortstat $PREV_COMMIT..$POST_COMMIT $FUNC 2>/dev/null)
if [[ $DIFF != "" ]]; then
$FUNC
fi
done |
|
@jubianchi - |
comments powered by Disqus