GradleとMavenのプロジェクトに使えます。
Gradleの場合、gradlewrapperを使います。
# !/bin/bash
echo "prepush called"
DIR=$PWD
ROOT=$PWD/`git rev-parse --show-cdup`
ROOT=${ROOT%/}
result=0
# dryRun
function dryRun(){
local result=0
pushMessage=`git push --dry-run --no-verify 2>&1`
result=$?
if [ $result -eq 0 ]; then
if echo $pushMessage | grep -e "^Everything.\+" > /dev/null 2>&1; then
result=1
fi
else
if echo $pushMessage | grep -e "has no upstream branch" > /dev/null 2>&1; then
result=0
fi
fi
if [ $result -ne 0 ]; then
echo $pushMessage 1>&2
fi
return $result
}
# status check
function isReadyToPush(){
local _test=`git submodule foreach "git status|grep 'nothing to commit, working directory clean'"`
local _expect=`git submodule foreach "git status|echo 'nothing to commit, working directory clean'"`
if [ "$_test" != "$_expect" ] ; then
echo "[error] submodule has uncommitted changes." 1>&2
return 1
fi
_test=`git submodule foreach "git log --remotes --oneline -1|sed -e 's/ .\+//g'"`
_expect=`git submodule foreach "git reflog -1|grep 'HEAD@{0}'|sed -e 's/ HEAD.\+//g'"`
if [ "$_test" != "$_expect" ] ; then
echo "[warn] submodule has unpushed or unpulled changes." 1>&2
return 0
fi
return 0
}
# gradle
function forGradle(){
if [ -f "${ROOT}/gradlew" ]; then
cd $ROOT
echo "${ROOT}/gradlew found. start gradle test"
chmod +x ./gradlew
./gradlew clean test 2>&1 >/dev/null;result=$?
cd $DIR
return 0
fi
return 1
}
# maven
function forMaven(){
if [ -f "${ROOT}/pom.xml" ]; then
cd $ROOT
echo "${ROOT}/pom.xml found. start mvn test"
mvn clean test 2>&1 >/dev/null;result=$?
cd $DIR
fi
}
# -------------------------
# entry point
# -------------------------
dryRun
if [ $? -eq 0 ]; then
isReadyToPush
if [ $? -eq 0 ]; then
forGradle
if [ $? -ne 0 ]; then
forMaven
fi
else
exit 1
fi
else
exit 1
fi
if [ $result -ne 0 ]; then
echo "test error!"
echo "push rejected!"
exit 1
else
echo "Test ends successfully"
fi
echo "push start"
exit 0