11#! /usr/bin/env bash
22
3- # ------------------------------------------------------------------------------
4- # Git 基本操作脚本
3+ # -----------------------------------------------------------------------------------------------------
4+ # git operation utils
55# @author Zhang Peng
6- # ------------------------------------------------------------------------------
6+ # -----------------------------------------------------------------------------------------------------
7+
8+ # ------------------------------------------------------------------------------ load libs
79
8- # 装载其它库
910LINUX_SCRIPTS_LIB_DIR=` dirname ${BASH_SOURCE[0]} `
1011
1112if [[ ! -x ${LINUX_SCRIPTS_LIB_DIR} /utils.sh ]]; then
12- logError " 必要脚本库 ${LINUX_SCRIPTS_LIB_DIR} /utils.sh 不存在! "
13+ logError " ${LINUX_SCRIPTS_LIB_DIR} /utils.sh not exists! "
1314 exit 1
1415fi
1516
1617source ${LINUX_SCRIPTS_LIB_DIR} /utils.sh
1718
18- # ------------------------------------------------------------------------------ git 操作函数
19+
20+ # ------------------------------------------------------------------------------ functions
1921
2022GIT_LOCAL_BRANCH=
2123getGitLocalBranch () {
@@ -27,37 +29,36 @@ getGitOriginBranch() {
2729 GIT_ORIGIN_BRANCH=$( git rev-parse --abbrev-ref --symbolic-full-name " @{u}" )
2830}
2931
30- # 检查指定的路径是不是一个 git 项目
32+ # check specified path is git project or not
3133checkGit () {
3234 local source=$1
3335 if [[ -d " ${source} " ]]; then
34- cd ${source} || return ${NO }
35- # (1)删除git状态零时文件
36+ cd ${source} || return ${ENV_NO }
37+ # (1) delete gitstatus.tmp
3638 if [[ -f " gitstatus.tmp" ]]; then
3739 rm -rf gitstatus.tmp
3840 fi
3941
40- # (2)判断git是否可用
42+ # (2) check git status
4143 git status & > gitstatus.tmp
4244 local gitStatus=false
4345 grep -iwq ' not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true
4446 rm -rf gitstatus.tmp
4547 if [[ ${gitStatus} == true ]]; then
46- return ${YES }
48+ return ${ENV_YES }
4749 else
48- return ${NO }
50+ return ${ENV_NO }
4951 fi
5052
51- return ${NO }
53+ return ${ENV_NO }
5254 fi
5355
54- logError " ${source} is invalid dir ."
55- return ${NO }
56+ logWarn " ${source} is not exists ."
57+ return ${ENV_NO }
5658}
5759
58- # clone 或 fetch 操作
59- # 如果本地代码目录已经是 git 仓库,执行 pull;若不是,则执行 clone
60- # 依次传入 Git 仓库、Git 项目组、Git 项目名、分支、本地代码目录
60+ # execute git clone or fetch
61+ # params: Git repository, git group, git project, git branch, local path
6162cloneOrPullGit () {
6263
6364 local repository=$1
@@ -68,12 +69,12 @@ cloneOrPullGit() {
6869
6970 if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then
7071 logError " Please input root, group, project, branch."
71- return ${FAILED }
72+ return ${ENV_FAILED }
7273 fi
7374
7475 if [[ ! -d " ${root} " ]]; then
7576 logError " ${root} is not directory."
76- return ${FAILED }
77+ return ${ENV_FAILED }
7778 fi
7879
7980 local source=${root} /${group} /${project}
@@ -82,52 +83,49 @@ cloneOrPullGit() {
8283 mkdir -p ${root} /${group}
8384
8485 checkGit ${source}
85- if [[ " ${YES} " == " $? " ]]; then
86- # 如果 ${source} 是 git 项目,执行 pull 操作
87- cd ${source} || return ${FAILED}
86+ if [[ " ${ENV_YES} " == " $? " ]]; then
87+ cd ${source} || return ${ENV_FAILED}
8888
89+ git fetch --all
8990 git checkout -f ${branch}
90- if [[ " ${SUCCEED } " != " $? " ]]; then
91+ if [[ " ${ENV_SUCCEED } " != " $? " ]]; then
9192 logError " <<<< git checkout ${branch} failed."
92- return ${FAILED }
93+ return ${ENV_FAILED }
9394 fi
9495 logInfo " git checkout ${branch} succeed."
9596
9697 getGitOriginBranch
97- git fetch --all
9898 git reset --hard ${GIT_ORIGIN_BRANCH}
99- if [[ " ${SUCCEED } " != " $? " ]]; then
99+ if [[ " ${ENV_SUCCEED } " != " $? " ]]; then
100100 logError " <<<< git reset --hard ${GIT_ORIGIN_BRANCH} failed."
101- return ${FAILED }
101+ return ${ENV_FAILED }
102102 fi
103103 logInfo " git reset --hard ${GIT_ORIGIN_BRANCH} succeed."
104104
105105 git pull
106- if [[ " ${SUCCEED } " != " $? " ]]; then
106+ if [[ " ${ENV_SUCCEED } " != " $? " ]]; then
107107 logError " <<<< git pull failed."
108- return ${FAILED }
108+ return ${ENV_FAILED }
109109 fi
110110 logInfo " git pull succeed."
111111 else
112- # 如果 ${source} 不是 git 项目,执行 clone 操作
113-
114112 git clone " ${repository} :${group} /${project} .git" ${source}
115- if [[ " ${SUCCEED } " != " $? " ]]; then
113+ if [[ " ${ENV_SUCCEED } " != " $? " ]]; then
116114 logError " <<<< git clone ${project} failed."
117- return ${FAILED }
115+ return ${ENV_FAILED }
118116 fi
119117 logInfo " git clone ${project} succeed."
120118
121- cd ${source} || return ${FAILED }
119+ cd ${source} || return ${ENV_FAILED }
122120
123121 git checkout -f ${branch}
124- if [[ " ${SUCCEED } " != " $? " ]]; then
122+ if [[ " ${ENV_SUCCEED } " != " $? " ]]; then
125123 logError " <<<< git checkout ${branch} failed."
126- return ${FAILED }
124+ return ${ENV_FAILED }
127125 fi
128126 logInfo " git checkout ${branch} succeed."
129127 fi
130128
131129 logInfo " Clone or pull git project [$2 /$3 :$4 ] succeed."
132- return ${SUCCEED }
130+ return ${ENV_SUCCEED }
133131}
0 commit comments