Skip to content

Instantly share code, notes, and snippets.

@M1nhNV
Last active January 24, 2024 02:04
Show Gist options
  • Save M1nhNV/8799a492e569e56f76ecc9e24ffc55cc to your computer and use it in GitHub Desktop.
Save M1nhNV/8799a492e569e56f76ecc9e24ffc55cc to your computer and use it in GitHub Desktop.
commit code shell script
function CommitCode {
git add -A
git commit -m "$1"
echo -e "$1 ====> \033[32m commit succeed \033[m"
CURRENT_BRANCH=$2
TARGET_BRANCH=$3
DEVELOP_BRANCH='develop'
if [ "$CURRENT_BRANCH" == "" ]; then
CURRENT_BRANCH=$(git branch --show-current)
fi
if [ "$TARGET_BRANCH" != "" ]; then
mergeBranch "origin" $TARGET_BRANCH $CURRENT_BRANCH
echo -e "\033[32m merge branch $CURRENT_BRANCH to $3 succeed \033[m"
else
TARGET_BRANCH=$DEVELOP_BRANCH
mergeBranch "origin" $TARGET_BRANCH $CURRENT_BRANCH
echo -e "\033[32m merge branch $CURRENT_BRANCH to develop succeed \033[m"
fi
git push origin $CURRENT_BRANCH
openSiteWithDefaultBrowser "[YOR_URL]?source=$2&dest=$TARGET_BRANCH"
}
function mergeBranch {
git checkout $2
isInRemote $1 $2
HAVE_BRANCH=$?
if [ "$HAVE_BRANCH" -eq "1" ]; then
git pull $1 $2 --ff-only
git checkout $3
git merge $2
checkConflictMergeBranch
elif [ "$HAVE_BRANCH" -eq "0" ]; then
echo "not have branch"
fi
}
function checkConflictMergeBranch {
CONFLICTS=$(git ls-files -u | wc -l)
if [ "$CONFLICTS" -gt 0 ] ; then
echo "There is a merge conflict. Aborting"
git merge --abort
exit 1
fi
}
function isInRemote () {
local existed_in_remote=$(git ls-remote --heads $1 $2)
if [[ -z ${existed_in_remote} ]]; then
return 0
else
return 1
fi
}
function openSiteWithDefaultBrowser {
open $1
echo -e "$1 ====> \033[32m open succeed \033[m"
}
CommitCode "$1" $2 $3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment