Last active
May 7, 2020 12:45
-
-
Save wicksome/1ae5dfcd42dbbce69daff80823951492 to your computer and use it in GitHub Desktop.
The 'git-finish' is 'git-flow finish' helper command using git-flow and git commands.
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
#!/usr/bin/env bash | |
# Set current branch to variable | |
WORKING_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
# Check branch name | |
if [ -z "$1" ]; then | |
echo "usage: git-finish <refspec>" | |
exit 1 | |
fi | |
# Check if working tree is clean | |
if [[ -n $(git status -s) ]]; then | |
echo "Please cleanup working tree" | |
exit 1 | |
fi | |
# Update git brnaches | |
git fetch origin | |
echo | |
git checkout master | |
git pull --ff-only origin master | |
[[ $? == "0" ]] || exit 1 | |
echo | |
git checkout develop | |
git pull --ff-only origin develop | |
[[ $? == "0" ]] || exit 1 | |
echo | |
# Check release branch | |
BRANCH_COUNT=$(git branch -ar | grep -E "origin\/.*$1" | wc -l | tr -d ' ') | |
if [ "$BRANCH_COUNT" != "1" ]; then | |
echo "$(tput setaf 1)* '$(tput bold)$1$(tput sgr0;tput setaf 1)' branch is not one($BRANCH_COUNT).$(tput sgr0)" | |
git branch -ar | grep origin/$1 | tr -d ' ' | |
exit 1 | |
fi | |
# Check release branch | |
RELEASE_BRANCH=$(git branch -ar | grep -E "origin\/.*$1" | tr -d ' ') | |
while true; do | |
read -p "$(tput setaf 11)* Do you want a finish $(tput smul)$RELEASE_BRANCH$(tput rmul)?$(tput sgr0) " yn | |
case $yn in | |
[Yy]* ) | |
echo "$(tput setaf 0) $ git checkout -t $RELEASE_BRANCH$(tput sgr0)" | |
git checkout -t $RELEASE_BRANCH | |
[[ $? == "0" ]] || exit 1 | |
break;; | |
[Nn]* ) exit;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
echo | |
# Make a release or hotfix | |
echo "$(tput setaf 2)* Merge branches and Make a release(or hotfix)$(tput sgr0)" | |
if [[ $RELEASE_BRANCH == *"release"* ]]; then | |
echo "$(tput setaf 0) $ git flow release finish ${RELEASE_BRANCH#"origin/$(git config gitflow.prefix.release)"}$(tput sgr0)" | |
git flow release finish ${RELEASE_BRANCH#"origin/$(git config gitflow.prefix.release)"} | |
else | |
echo "$(tput setaf 0) $ git flow hotfix finish ${RELEASE_BRANCH#"origin/$(git config gitflow.prefix.hotfix)"}$(tput sgr0)" | |
git flow hotfix finish ${RELEASE_BRANCH#"origin/$(git config gitflow.prefix.hotfix)"} | |
fi | |
[[ $? == "0" ]] || exit 1 | |
echo | |
# Update remote branch and tags | |
while true; do | |
read -p "* $(tput setaf 11)Do you want to update remote branch and tags?$(tput sgr0) " yn | |
case $yn in | |
[Yy]* ) break;; | |
[Nn]* ) exit;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
echo | |
echo "$(tput setaf 2)* Push master, develop branch to origin$(tput sgr0)" | |
echo "$(tput setaf 0) $ git push origin master develop$(tput sgr0)" | |
git push origin master develop | |
echo | |
echo "$(tput setaf 2)* Push tags$(tput sgr0)" | |
echo "$(tput setaf 0) $ git push --tags$(tput sgr0)" | |
git push --tags | |
echo | |
echo "$(tput setaf 2)* Delete the branch:$(tput sgr0) $RELEASE_BRANCH" | |
echo "$(tput setaf 0) $ git push origin --delete ${RELEASE_BRANCH#"origin/"}$(tput sgr0)" | |
git push origin --delete ${RELEASE_BRANCH#"origin/"} | |
echo | |
# Show releases link | |
REMOTE_URL=$(git ls-remote --get-url origin) | |
echo "$(tput setaf 2)* Create new GitHub release:$(tput sgr0)" | |
echo " $REMOTE_URL/releases" | |
echo | |
# Checkout to working branch | |
# git checkout $WORKING_BRANCH | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirement
install git, git-flow
Usage
git-finish
/usr/local/bin
git finish <brnach>
Preview