2018-02-07 17:14:35 +08:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
###################################################################################
|
|
|
|
|
# 更新代码脚本
|
|
|
|
|
# 只有退出码(exit code)为 0 ,表示脚本执行成功。
|
2019-05-07 14:29:16 +08:00
|
|
|
|
# @author: Zhang Peng
|
2018-02-07 17:14:35 +08:00
|
|
|
|
###################################################################################
|
2017-12-14 20:21:15 +08:00
|
|
|
|
|
|
|
|
|
# 检查脚本参数,如必要参数未传入,退出脚本。
|
|
|
|
|
checkInput() {
|
2018-01-26 17:14:34 +08:00
|
|
|
|
if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then
|
|
|
|
|
echo "请输入脚本参数:repository branch [source] [target]"
|
|
|
|
|
echo " repository: git 仓储(必填)。"
|
|
|
|
|
echo " branch: git 分支(必填)。如 master/develop"
|
|
|
|
|
echo " source: 代码存放目录。默认为/home/zp/source。"
|
2017-12-14 20:21:15 +08:00
|
|
|
|
echo " target: 代码存放目录。默认为脚本所在目录。"
|
2018-02-07 17:14:35 +08:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 检查文件夹是否存在,不存在则创建
|
|
|
|
|
function createFolderIfNotExist() {
|
|
|
|
|
if [ ! -d "$1" ];then
|
|
|
|
|
mkdir -p "$1"
|
2017-12-14 20:21:15 +08:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。
|
|
|
|
|
gitok=false
|
2018-02-07 17:14:35 +08:00
|
|
|
|
function isGitExist() {
|
|
|
|
|
cd ${SOURCE_DIR}
|
|
|
|
|
if [ -d "${SOURCE_DIR}/${repository}/${target}" ]; then
|
|
|
|
|
cd ${SOURCE_DIR}/${repository}/${target}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
#(1)删除git状态零时文件
|
|
|
|
|
if [ -f "gitstatus.tmp" ]; then
|
|
|
|
|
rm -rf gitstatus.tmp
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#(2) 判断是否存在.git目录
|
|
|
|
|
if [ -d "./.git" ]; then
|
|
|
|
|
#(3) 判断git是否可用
|
|
|
|
|
git status &> gitstatus.tmp
|
|
|
|
|
grep -iwq 'not a git repository' gitstatus.tmp && gitok=false || gitok=true
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#返回到主目录
|
2018-02-07 17:14:35 +08:00
|
|
|
|
cd ${SOURCE_DIR}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。
|
2018-02-07 17:14:35 +08:00
|
|
|
|
function doFetchOrClone() {
|
2017-12-14 20:21:15 +08:00
|
|
|
|
if ${gitok}; then
|
2018-02-07 17:14:35 +08:00
|
|
|
|
cd ${SOURCE_DIR}/${repository}/${target}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
git reset --hard
|
|
|
|
|
git clean -ffdx
|
|
|
|
|
git fetch
|
|
|
|
|
echo "git fetch ${repository} remote repository 到本地成功"
|
|
|
|
|
else
|
|
|
|
|
#删除所有内容,便于重新进行git clone
|
|
|
|
|
rm -rf ${repository}
|
2018-02-07 17:14:35 +08:00
|
|
|
|
git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
echo "git clone ${repository} remote repository 到本地成功"
|
2018-02-07 17:14:35 +08:00
|
|
|
|
cd ${SOURCE_DIR}/${repository}/${target}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 切换到 ${branch} 分支
|
2018-02-07 17:14:35 +08:00
|
|
|
|
function doCheckout() {
|
2017-12-14 20:21:15 +08:00
|
|
|
|
echo "检出 ${repository} ${branch} 分支代码"
|
|
|
|
|
isRemoteBranch=false
|
|
|
|
|
gitRemoteBranch=`git branch -r`
|
|
|
|
|
echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false
|
|
|
|
|
if ${isRemoteBranch}; then
|
2018-02-07 17:14:35 +08:00
|
|
|
|
echo "找到 ${branch} 分支。"
|
2017-12-14 20:21:15 +08:00
|
|
|
|
git checkout -f 'origin/'${branch}
|
|
|
|
|
else
|
2018-02-07 17:14:35 +08:00
|
|
|
|
echo "未找到 ${branch} 分支!"
|
|
|
|
|
exit 2
|
2017-12-14 20:21:15 +08:00
|
|
|
|
fi
|
|
|
|
|
echo "更新子模块代码"
|
|
|
|
|
git submodule update --init --recursive --force
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 17:14:35 +08:00
|
|
|
|
######################################## MAIN ########################################
|
2017-12-14 20:21:15 +08:00
|
|
|
|
export LANG="zh_CN.UTF-8"
|
2018-01-26 17:14:34 +08:00
|
|
|
|
|
2018-02-07 17:14:35 +08:00
|
|
|
|
# 0. 检查传入的参数
|
2017-12-14 20:21:15 +08:00
|
|
|
|
repository=`echo $1`
|
|
|
|
|
branch=`echo $2`
|
2018-01-26 17:14:34 +08:00
|
|
|
|
source=`echo $3`
|
|
|
|
|
target=`echo $4`
|
2018-02-07 17:14:35 +08:00
|
|
|
|
checkInput
|
|
|
|
|
|
|
|
|
|
# 设置全局常量
|
|
|
|
|
GITHUB_ACCOUNT=dunwu
|
|
|
|
|
SOURCE_DIR=/home/xyz/source
|
2018-01-26 17:14:34 +08:00
|
|
|
|
if [ "${source}" != "" ]; then
|
2018-02-07 17:14:35 +08:00
|
|
|
|
SOURCE_DIR=${source}
|
2018-01-26 17:14:34 +08:00
|
|
|
|
fi
|
2018-02-07 17:14:35 +08:00
|
|
|
|
createFolderIfNotExist ${SOURCE_DIR}
|
2017-12-14 20:21:15 +08:00
|
|
|
|
|
|
|
|
|
# 1. 判断本地是否已存在 Git 仓库
|
|
|
|
|
isGitExist
|
|
|
|
|
|
|
|
|
|
# 2. 如果本地已有代码,执行 fetch;反之,从远程 clone
|
|
|
|
|
doFetchOrClone
|
|
|
|
|
|
|
|
|
|
# 3. 切换到指定分支
|
|
|
|
|
doCheckout
|
|
|
|
|
|
2018-02-07 17:14:35 +08:00
|
|
|
|
# 4. 分配权限
|
|
|
|
|
chmod -R 777 ${SOURCE_DIR}
|
|
|
|
|
|
|
|
|
|
exit 0
|