forked from dunwu/java-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-clone.sh
More file actions
112 lines (98 loc) · 2.9 KB
/
git-clone.sh
File metadata and controls
112 lines (98 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
#
# 检查脚本参数,如必要参数未传入,退出脚本。
#
checkInput() {
if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then
echo "请输入脚本参数:repository branch [source] [target]"
echo " repository: git 仓储(必填)。"
echo " branch: git 分支(必填)。如 master/develop"
echo " source: 代码存放目录。默认为/home/zp/source。"
echo " target: 代码存放目录。默认为脚本所在目录。"
exit 0
fi
}
#
# 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。
#
gitok=false
isGitExist() {
cd ${SOURCE_PATH}
if [ -d "${SOURCE_PATH}/${repository}/${target}" ]; then
cd ${SOURCE_PATH}/${repository}/${target}
#(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
#返回到主目录
cd ${SOURCE_PATH}
fi
}
#
# 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。
#
doFetchOrClone() {
if [ ! -d "${SOURCE_PATH}" ]; then
mkdir -p ${SOURCE_PATH}
fi
if ${gitok}; then
cd ${SOURCE_PATH}/${repository}/${target}
git reset --hard
git clean -ffdx
git fetch
echo "git fetch ${repository} remote repository 到本地成功"
else
#删除所有内容,便于重新进行git clone
rm -rf ${repository}
git clone --no-checkout [email protected]:${ACCOUNT}/${repository}.git ${SOURCE_PATH}/${repository}/${target}
echo "git clone ${repository} remote repository 到本地成功"
cd ${SOURCE_PATH}/${repository}/${target}
fi
}
#
# 切换到 ${branch} 分支
#
doCheckout() {
echo "检出 ${repository} ${branch} 分支代码"
isRemoteBranch=false
gitRemoteBranch=`git branch -r`
echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false
if ${isRemoteBranch}; then
echo "找到 ${branch} 对应的远端分支"
git checkout -f 'origin/'${branch}
else
git checkout -f ${branch}
fi
echo "更新子模块代码"
git submodule update --init --recursive --force
}
##############################__MAIN__########################################
export LANG="zh_CN.UTF-8"
# Github 账户
ACCOUNT=dunwu
# 源码默认根目录
SOURCE_PATH=/home/zp/source
# 必填输入参数
repository=`echo $1`
branch=`echo $2`
# 可选输入参数
source=`echo $3`
target=`echo $4`
if [ "${source}" != "" ]; then
SOURCE_PATH=${source}
fi
# 0. 检查传入的参数
checkInput
# 1. 判断本地是否已存在 Git 仓库
isGitExist
# 2. 如果本地已有代码,执行 fetch;反之,从远程 clone
doFetchOrClone
# 3. 切换到指定分支
doCheckout
echo "代码检出完成!"