linux-tutorial/codes/linux/soft/lib/git.sh

132 lines
3.8 KiB
Bash
Raw Normal View History

2020-01-03 22:41:37 +08:00
#!/usr/bin/env bash
2020-03-10 22:54:15 +08:00
# -----------------------------------------------------------------------------------------------------
# git operation utils
2020-02-02 17:56:28 +08:00
# @author Zhang Peng
2020-03-10 22:54:15 +08:00
# -----------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------ load libs
2020-02-02 17:56:28 +08:00
2020-02-12 22:46:17 +08:00
LINUX_SCRIPTS_LIB_DIR=`dirname ${BASH_SOURCE[0]}`
if [[ ! -x ${LINUX_SCRIPTS_LIB_DIR}/utils.sh ]]; then
2020-03-10 22:54:15 +08:00
logError "${LINUX_SCRIPTS_LIB_DIR}/utils.sh not exists!"
2020-02-12 22:46:17 +08:00
exit 1
fi
source ${LINUX_SCRIPTS_LIB_DIR}/utils.sh
2020-01-03 22:41:37 +08:00
2020-03-10 22:54:15 +08:00
# ------------------------------------------------------------------------------ functions
2020-01-03 22:41:37 +08:00
2020-02-12 22:46:17 +08:00
GIT_LOCAL_BRANCH=
getGitLocalBranch() {
GIT_LOCAL_BRANCH=$(git symbolic-ref -q --short HEAD)
}
GIT_ORIGIN_BRANCH=
getGitOriginBranch() {
GIT_ORIGIN_BRANCH=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}")
}
2020-03-10 22:54:15 +08:00
# check specified path is git project or not
2020-01-03 22:41:37 +08:00
checkGit() {
2020-02-12 22:46:17 +08:00
local source=$1
if [[ -d "${source}" ]]; then
2020-03-10 22:54:15 +08:00
cd ${source} || return ${ENV_NO}
# (1) delete gitstatus.tmp
2020-02-12 22:46:17 +08:00
if [[ -f "gitstatus.tmp" ]]; then
2020-01-03 22:41:37 +08:00
rm -rf gitstatus.tmp
2020-02-12 22:46:17 +08:00
fi
2020-01-03 22:41:37 +08:00
2020-03-10 22:54:15 +08:00
# (2) check git status
2020-01-03 22:41:37 +08:00
git status &> gitstatus.tmp
local gitStatus=false
grep -iwq 'not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true
rm -rf gitstatus.tmp
if [[ ${gitStatus} == true ]]; then
2020-03-10 22:54:15 +08:00
return ${ENV_YES}
2020-01-03 22:41:37 +08:00
else
2020-03-10 22:54:15 +08:00
return ${ENV_NO}
2020-01-03 22:41:37 +08:00
fi
2020-03-10 22:54:15 +08:00
return ${ENV_NO}
2020-02-12 22:46:17 +08:00
fi
2020-01-03 22:41:37 +08:00
2020-03-10 22:54:15 +08:00
logWarn "${source} is not exists."
return ${ENV_NO}
2020-01-03 22:41:37 +08:00
}
2020-03-10 22:54:15 +08:00
# execute git clone or fetch
# params: Git repository, git group, git project, git branch, local path
2020-01-03 22:41:37 +08:00
cloneOrPullGit() {
local repository=$1
local group=$2
local project=$3
local branch=$4
local root=$5
if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then
2020-02-12 22:46:17 +08:00
logError "Please input root, group, project, branch."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
if [[ ! -d "${root}" ]]; then
2020-02-12 22:46:17 +08:00
logError "${root} is not directory."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
local source=${root}/${group}/${project}
2020-02-12 22:46:17 +08:00
logInfo "project directory is ${source}."
logInfo "git url is ${repository}:${group}/${project}.git."
2020-01-03 22:41:37 +08:00
mkdir -p ${root}/${group}
checkGit ${source}
2020-03-10 22:54:15 +08:00
if [[ "${ENV_YES}" == "$?" ]]; then
cd ${source} || return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
2020-03-10 22:54:15 +08:00
git fetch --all
2020-01-03 22:41:37 +08:00
git checkout -f ${branch}
2020-03-10 22:54:15 +08:00
if [[ "${ENV_SUCCEED}" != "$?" ]]; then
2020-02-12 22:46:17 +08:00
logError "<<<< git checkout ${branch} failed."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "git checkout ${branch} succeed."
2020-01-03 22:41:37 +08:00
2020-02-12 22:46:17 +08:00
getGitOriginBranch
git reset --hard ${GIT_ORIGIN_BRANCH}
2020-03-10 22:54:15 +08:00
if [[ "${ENV_SUCCEED}" != "$?" ]]; then
2020-02-12 22:46:17 +08:00
logError "<<<< git reset --hard ${GIT_ORIGIN_BRANCH} failed."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "git reset --hard ${GIT_ORIGIN_BRANCH} succeed."
2020-01-03 22:41:37 +08:00
git pull
2020-03-10 22:54:15 +08:00
if [[ "${ENV_SUCCEED}" != "$?" ]]; then
2020-02-12 22:46:17 +08:00
logError "<<<< git pull failed."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "git pull succeed."
2020-01-03 22:41:37 +08:00
else
git clone "${repository}:${group}/${project}.git" ${source}
2020-03-10 22:54:15 +08:00
if [[ "${ENV_SUCCEED}" != "$?" ]]; then
2020-02-12 22:46:17 +08:00
logError "<<<< git clone ${project} failed."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "git clone ${project} succeed."
2020-01-03 22:41:37 +08:00
2020-03-10 22:54:15 +08:00
cd ${source} || return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
git checkout -f ${branch}
2020-03-10 22:54:15 +08:00
if [[ "${ENV_SUCCEED}" != "$?" ]]; then
2020-02-12 22:46:17 +08:00
logError "<<<< git checkout ${branch} failed."
2020-03-10 22:54:15 +08:00
return ${ENV_FAILED}
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "git checkout ${branch} succeed."
2020-01-03 22:41:37 +08:00
fi
2020-02-12 22:46:17 +08:00
logInfo "Clone or pull git project [$2/$3:$4] succeed."
2020-03-10 22:54:15 +08:00
return ${ENV_SUCCEED}
2020-01-03 22:41:37 +08:00
}