Skip to content

Instantly share code, notes, and snippets.

@sergeycherepanov
Last active April 6, 2020 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergeycherepanov/07126ad57e760b4294c9e30cb33c382b to your computer and use it in GitHub Desktop.
Save sergeycherepanov/07126ad57e760b4294c9e30cb33c382b to your computer and use it in GitHub Desktop.
makerelease
#!/bin/bash
#
# Install dependencies:
# pip install markdown-to-json
#
#
# Usage: makerelease "branch1 branch2 ..."
#
set -e
#set -x
[[ -d ./.git ]] || {
echo "Warning .git directory not found!"
exit 1
}
if [[ -z $1 ]]; then
read -p "Branch(es):" branches
else
_branches=$1
fi
[[ -z $_branches ]] || [[ "" == $_branches ]] && {
echo "Branch wasn't specified"
exit 1
}
git stash push -m "$(date +'%F %T')"
git fetch origin
git fetch --tags --force origin
isMinor=0;
branches=""
for branch in $_branches; do
if (echo $branch | egrep '^us/'); then
isMinor=1
fi
branches="$branches origin/$branch"
done
if [[ 1 -eq $isMinor ]]; then
tag=$(git tag --format='%(creatordate:short)%09%(refname:strip=2)' | awk '{print $2}' | tail -1 | awk -F. '{ print $1 "." ++$2 "." 0 }')
else
tag=$(git tag --format='%(creatordate:short)%09%(refname:strip=2)' | awk '{print $2}' | tail -1 | awk -F. '{ print $1 "." $2 "." ++$3 }')
fi
[[ "" == $tag ]] && {
read -p "No any tag found, please specify:" tag
}
echo "The tag will be: $tag"
read -n 1 -s -r -p "Press any key to continue"
echo ""
git checkout -b "release/$tag" origin/master
#git branch --set-upstream-to "$tag" "origin/release/$tag"
git pull origin master
git merge --no-ff --no-commit $branches
if ! [[ -f CHANGELOG.md ]]; then
read -r -p "CHANGELOG.md not found, create it? [Y/n]" response
response=$( echo $response | awk '{print tolower($0)}' )
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
touch CHANGELOG.md
fi
fi
if [[ -f CHANGELOG.md ]]; then
cat CHANGELOG.md | grep -i '^# Changelog' || {
echo "Invalid changelog format! normilizing..."
sleep 1
printf "# Changelog\n$(cat CHANGELOG.md | egrep -v '=+' | sed 's/^[[:blank:]]*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)/## \1/')" > CHANGELOG.md
}
php -r '$changelog = json_decode(file_get_contents("php://stdin"), true); exit((int)!isset($changelog["Changelog"]["'$tag'"]));' < <(md_to_json CHANGELOG.md) || {
echo "Release not found in change log, adding..."
sleep 1
echo $(tail -n1 CHANGELOG.md) | grep -v '^$' && echo "" >> CHANGELOG.md
features=""
bugs=""
for branch in $_branches; do
changelog=$(echo "$(git log --no-merges origin/$branch ^master | egrep '^\s*#[0-9]+')" | sed 's/^[[:blank:]]*#\([0-9][0-9]*\)/ * [#\1](https:\/\/tp.ewave.com\/\1) --/' | sort -u)
if (echo $branch | egrep '^bug/'); then
bugs="${bugs}${changelog}"
else
features="${features}${changelog}"
fi
done
echo "## ${tag}" >> CHANGELOG.md
if [[ "" != $features ]]; then
echo "* New features:" >> CHANGELOG.md
echo "$features" >> CHANGELOG.md
fi
if [[ "" != $bugs ]]; then
echo "* Bug fixing:" >> CHANGELOG.md
echo "$bugs" >> CHANGELOG.md
fi
echo "" >> CHANGELOG.md
echo "CHANGELOG.md:"
echo "$(cat CHANGELOG.md)"
}
read -r -p "Do you want to edit CHANGELOG.md? [Y/n]" response
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
vim CHANGELOG.md
fi
git add CHANGELOG.md
fi
php -r '$composer = json_decode(file_get_contents("composer.json"), true); $composer["version"] = "'$tag'"; file_put_contents("composer.json", json_encode($composer, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) . PHP_EOL);'
git add "composer.json"
read -r -p "Do you want to commit changes? [Y/n]" response
response=$( echo $response | awk '{print tolower($0)}' )
if ! [[ $response =~ ^(yes|y| ) ]] && ! [[ -z $response ]]; then
exit 1
fi
git commit -m "Release: $tag; Merged: $_branches"
git tag "$tag"
read -r -p "Do you want to push changes? [Y/n]" response
response=$( echo $response | awk '{print tolower($0)}' )
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
git push origin "release/$tag"
git push origin "release/$tag:master"
git push origin --tag "$tag"
fi
read -r -p "Do you want to merge release/$tag into develop? [Y/n]" response
response=$( echo $response | awk '{print tolower($0)}' )
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
git branch | awk -F ' +' '! /\(no branch\)/ {print $2}' | grep develop | while read b; do git branch -D $b; done
git checkout develop
git merge "release/$tag"
fi
read -r -p "Do you want to push develop branch? [Y/n]" response
response=$( echo $response | awk '{print tolower($0)}' )
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
git push origin "develop:develop"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment