Last active
August 29, 2015 14:17
-
-
Save rubeniskov/ea28cda6089e0118cdec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Called by "git push" after it has checked the remote status, | |
# but before anything has been pushed. | |
# | |
# If this script exits with a non-zero status nothing will be pushed. | |
# | |
# Steps to install, from the root directory of your repo... | |
# 1. Copy the file into your repo at `.git/hooks/pre-push` | |
# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` | |
# 3. Or, use `rake hooks:pre_push` to install | |
# | |
# Requires git 1.8.2 or newer | |
# | |
# Git 1.8.2 release notes cover the new pre-push hook: | |
# <https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt> | |
# | |
# See Sample pre-push script: | |
# <https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample> | |
git_protected_branches[0]='master' | |
git_protected_branches[1]='develop' | |
git_user=$(git config user.name) | |
git_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p') | |
git_push_command=$(ps -ocommand= -p $PPID) | |
git_command_is_forced='force|delete|\-f' | |
git_type_branch=$(echo $git_branch | tr '#' ' ' | awk '{print $1}') | |
git_type_branch_id=$(echo $git_branch | tr '#' ' ' | awk '{print $2}') | |
git_root_dir=$(git rev-parse --show-toplevel); | |
git_changelog_file="$git_root_dir/CHANGELOG.md" | |
git_changelog_tmp="$git_root_dir/CHANGELOG.tmp" | |
color_red='\033[0;31m' | |
color_green='\033[0;32m' | |
color_orange='\033[0;33m' | |
color_end='\033[0m' | |
show_policy(){ | |
echo "${color_red}Stop now $git_user! before you make a crime fucking bastard. The [ ${git_protected_branches[*]} ] branches are protected of noobs like you${color_end}" | |
exit 1 | |
} | |
if [[ " ${git_protected_branches[*]} " == *" $git_branch "* ]]; then | |
show_policy | |
fi | |
if [[ $(hash npm 2>/dev/null || false) != false ]]; then | |
echo "\n${color_green}Update package control version${color_end}\n" | |
echo "\t ${color_green}Checking project${color_end}\n" | |
npm run test &>/dev/null | |
if [ "$?" = "1" ]; then | |
echo "\t${color_red}Error at this version!! Aborting${color_end}\n" | |
exit 1 | |
fi | |
npm_package="$git_root_dir/package.json" | |
npm_package_version=$(cat $npm_package | grep '.*version' | sed 's/[^0-9.]*\([0-9.]*\).*/\1/' ) | |
npm_package_git_url_commit=$(cat $npm_package | grep '.*url_commit' | cut -d '"' -f 4 | sed s/'http:\/\/'/''/g) | |
npm_package_git_url_tag=$(cat $npm_package | grep '.*url_tag' | cut -d '"' -f 4 | sed s/'http:\/\/'/''/g) | |
npm_suggested_version=$npm_package_version; | |
npm_package_version_major=$( echo $npm_package_version | tr '.' ' ' | awk '{print $1}' ) | |
npm_package_version_minor=$( echo $npm_package_version | tr '.' ' ' | awk '{print $2}' ) | |
npm_package_version_patch=$( echo $npm_package_version | tr '.' ' ' | awk '{print $3}' ) | |
echo "\t Current version: [${color_orange}$npm_package_version${color_end}]\n" | |
case "$git_type_branch" in | |
"release/") | |
echo "\t ${color_orange}The branch release can't be pushed by client git${color_end}\n" | |
exit 1; | |
;; | |
"feature/") | |
npm_package_version_minor=$git_type_branch_id; | |
npm_package_version_patch="0"; | |
echo "\t Checking ${color_green}feature${color_end}#${color_orange}$git_type_branch_id${color_end}\n" | |
;; | |
"hotfix/") | |
npm_package_version_patch=$git_type_branch_id; | |
echo "\t Checking ${color_red}hotfix#$git_type_branch_id${color_end}\n" | |
;; | |
esac | |
npm_suggested_version=$npm_package_version_major'.'$npm_package_version_minor'.'$npm_package_version_patch | |
exec < /dev/tty | |
read -p "Enter a version number or leave empty to update by [$npm_suggested_version]: " input_version | |
if [ "$input_version" != "" ]; then | |
npm_suggested_version=$input_version | |
fi | |
if [[ $npm_package_version != $npm_suggested_version ]]; then | |
echo "\n\t Will set new version to be: [${color_orange}$npm_suggested_version${color_end}]\n" | |
echo "\t Setting package version ${color_green}$npm_package${color_end}\n" | |
sed -i .bak 's/\("version": "\)[^"]*\("\)/\1'$npm_suggested_version'\2/g' $npm_package | |
if [ "$?" = "0" ]; then | |
if [ ! -f $git_changelog_file ]; then | |
echo "\t $git_changelog_file file created!\n" | |
echo "# Changelog\n\n" > $git_changelog_file | |
fi | |
echo "\t ${color_green}Git Ouput${color_end}\n\n" | |
echo "\n## version : [\`${npm_suggested_version}\`](${npm_package_git_url_tag}/v${npm_suggested_version})\n" > $git_changelog_tmp | |
git log --pretty=format:" - [%s]($npm_package_git_url_commit/%H)" "v$npm_package_version"...HEAD | grep '\(add\|update\|remove\|fix\)\:' >> $git_changelog_tmp | |
sed -i .bak "1r $git_changelog_tmp" $git_changelog_file | |
git add $git_changelog_file $npm_package | |
git commit -m "version: Bump to v$npm_suggested_version" | |
echo "\t Created a new tag, v$npm_suggested_version" | |
rm -rf "$npm_package.bak" | |
rm -rf "$git_changelog_file.bak" | |
rm -rf "$git_changelog_tmp" | |
else | |
echo "\t Can not change version in package.json!" 1>&2 | |
fi | |
else | |
echo "\n\t${color_red}No version changes detected${color_end}\n" | |
fi | |
git tag -d "v$npm_suggested_version" &>/dev/null | |
git push --no-verify origin ":refs/tags/v$npm_suggested_version" | |
git tag -a "v$npm_suggested_version" -m "Tagging version v$npm_suggested_version" | |
git push --no-verify origin "v$npm_suggested_version" | |
git push --no-verify origin $git_branch | |
else | |
echo 'NPM is not installed to increase control version' | |
fi | |
unset show_policy | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment