Last active
July 18, 2022 17:10
-
-
Save Wind4/ba21fd3005f47a46b49ce7abc748434a 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/bash | |
BRANCH_CURRENT=$(git rev-parse --abbrev-ref HEAD) | |
BRANCH_TARGET= | |
DELETE_CURRENT=0 | |
DEPLOY_COMMAND= | |
usage() { | |
echo "Usage: $0 target_branch [options]" 1>&2 | |
echo "Options:" 1>&2 | |
echo " -D, --rm Delete current branch after merge." 1>&2 | |
echo " -d, --deploy [command] Execute the deployment command." 1>&2 | |
echo " -h, --help Display help message." 1>&2 | |
exit 1 | |
} | |
if [[ -n "$1" ]]; then | |
BRANCH_TARGET=$1 | |
shift | |
else | |
usage | |
fi | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case $key in | |
-d | --deploy) | |
DEPLOY_COMMAND="$2" | |
shift | |
shift | |
;; | |
-D | --rm) | |
DELETE_CURRENT=1 | |
shift | |
;; | |
-h | --help) | |
usage | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
check() { | |
if [ ! -n "$BRANCH_CURRENT" ]; then | |
echo "Failed to get the current branch." 1>&2 | |
exit 2 | |
fi | |
if [ "$BRANCH_CURRENT" == "$BRANCH_TARGET" ]; then | |
echo "The current branch is similar to the target branch." 1>&2 | |
exit 2 | |
fi | |
if [ -n "$(git status -z -u | tr -d '\0')" ]; then | |
echo "There are uncommitted files in the workspace, please commit first." | |
exit 2 | |
fi | |
} | |
review() { | |
echo "------------------------------------------" | |
echo "Branch current: $BRANCH_CURRENT" | |
echo "Branch target: $BRANCH_TARGET" | |
echo "Deploy command: $DEPLOY_COMMAND" | |
echo "------------------------------------------" | |
} | |
workflow() { | |
git checkout $BRANCH_TARGET | |
if [ $? -ne 0 ]; then | |
echo "CHECKOUT FAILED!!!" 1>&2 | |
exit 2 | |
fi | |
git pull origin $BRANCH_TARGET | |
if [ $? -ne 0 ]; then | |
echo "PULL FAILED!!!" 1>&2 | |
exit 2 | |
fi | |
git merge $BRANCH_CURRENT --no-ff --no-edit --no-verify | |
if [ $? -ne 0 ]; then | |
git merge --abort | |
echo "Merge FAILED!!!" 1>&2 | |
exit 2 | |
fi | |
git push origin $BRANCH_TARGET | |
if [ $? -ne 0 ]; then | |
git merge --abort | |
echo "PUSH FAILED!!!" 1>&2 | |
exit 2 | |
fi | |
} | |
cleaup() { | |
if [ -n "$DEPLOY_COMMAND" ]; then | |
$DEPLOY_COMMAND | |
fi | |
if [ "$DELETE_CURRENT" -eq "1" ]; then | |
read -p "Deleting branch '$BRANCH_CURRENT'...[Y/n]: " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
git push origin :$BRANCH_CURRENT | |
git branch -D $BRANCH_CURRENT | |
else | |
echo 'Undelete the branch, please delete it manually.' | |
echo " git push origin :$BRANCH_CURRENT" | |
echo " git branch -D $BRANCH_CURRENT" | |
fi | |
else | |
git checkout $BRANCH_CURRENT | |
fi | |
} | |
check | |
review | |
workflow | |
cleaup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
拷贝脚本到
~/merge.sh
目录参数说明:
示例:
~/merge.sh uat
: 合并当前分支代码到uat
~/merge.sh uat -d ./develop.sh
: 合并当前分支代码到uat
,并执行./deploy.sh
发布脚本