Skip to content

Instantly share code, notes, and snippets.

@hannesa2
Created September 23, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannesa2/047781c7b552ce1177ea70e03afa6e87 to your computer and use it in GitHub Desktop.
Save hannesa2/047781c7b552ce1177ea70e03afa6e87 to your computer and use it in GitHub Desktop.
pre-push.sh git-hook to auto format with Android Studio code format
#!/bin/sh
# $1 -- Name of the remote to which the push is being done (Ex: origin)
# $2 -- URL to which the push is being done (Ex: https://<host>:<port>/<username>/<project_name>.git)
#local_ref = refs/heads/master
#local_sha1 = f8a07ee4f6af8271dc40caae6cc23f283122ed11
#remote_ref = refs/heads/master
#remote_sha1 = ffd4d512f34b11e3cf5c12433bbedd4b1532716f
GREEN='\033[32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
currentBranch=$(git rev-parse --abbrev-ref HEAD)
formatter=$(find /Applications/Andr* -name format.sh -print0 2>/dev/null)
echo ${formatter}
# count existing stashes
countExistingStashes=$(git stash list | wc -l | xargs)
# save remaining work, if neccassary
git stash
countNewStashes=$(git stash list | wc -l | xargs)
# iterate affected files
git diff --name-only ${currentBranch} $(git merge-base ${currentBranch} master) | while read -r file; do
echo $(pwd)/${file};
done | xargs "${formatter}" -s $(pwd)/.idea/codeStyles/Project.xml
violatedFilesCount=$(git status | grep 'modified:' | wc -l | xargs)
violatedFiles=$(git status | grep 'modified:')
git reset --hard
# only preserve stash, if we stashed something
if [ $countExistingStashes -ne $countNewStashes ]
then
git stash pop
fi
if [ $violatedFilesCount -eq 0 ]
then
echo "$GREEN all files are well formated$NC"
exit 0
else
echo "$violatedFilesCount have format issues"
echo $RED$violatedFiles$NC
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment