11#! /usr/bin/env bash
22
33# -----------------------------------------------------------------------------------------------------
4- # git operation utils
4+ # Shell Utils
55# @author Zhang Peng
66# -----------------------------------------------------------------------------------------------------
77
8- # ------------------------------------------------------------------------------ load libs
8+ # ------------------------------------------------------------------------------ env
9+
10+ # Regular Color
11+ export ENV_COLOR_BLACK=" \033[0;30m"
12+ export ENV_COLOR_RED=" \033[0;31m"
13+ export ENV_COLOR_GREEN=" \033[0;32m"
14+ export ENV_COLOR_YELLOW=" \033[0;33m"
15+ export ENV_COLOR_BLUE=" \033[0;34m"
16+ export ENV_COLOR_MAGENTA=" \033[0;35m"
17+ export ENV_COLOR_CYAN=" \033[0;36m"
18+ export ENV_COLOR_WHITE=" \033[0;37m"
19+ # Bold Color
20+ export ENV_COLOR_B_BLACK=" \033[1;30m"
21+ export ENV_COLOR_B_RED=" \033[1;31m"
22+ export ENV_COLOR_B_GREEN=" \033[1;32m"
23+ export ENV_COLOR_B_YELLOW=" \033[1;33m"
24+ export ENV_COLOR_B_BLUE=" \033[1;34m"
25+ export ENV_COLOR_B_MAGENTA=" \033[1;35m"
26+ export ENV_COLOR_B_CYAN=" \033[1;36m"
27+ export ENV_COLOR_B_WHITE=" \033[1;37m"
28+ # Underline Color
29+ export ENV_COLOR_U_BLACK=" \033[4;30m"
30+ export ENV_COLOR_U_RED=" \033[4;31m"
31+ export ENV_COLOR_U_GREEN=" \033[4;32m"
32+ export ENV_COLOR_U_YELLOW=" \033[4;33m"
33+ export ENV_COLOR_U_BLUE=" \033[4;34m"
34+ export ENV_COLOR_U_MAGENTA=" \033[4;35m"
35+ export ENV_COLOR_U_CYAN=" \033[4;36m"
36+ export ENV_COLOR_U_WHITE=" \033[4;37m"
37+ # Background Color
38+ export ENV_COLOR_BG_BLACK=" \033[40m"
39+ export ENV_COLOR_BG_RED=" \033[41m"
40+ export ENV_COLOR_BG_GREEN=" \033[42m"
41+ export ENV_COLOR_BG_YELLOW=" \033[43m"
42+ export ENV_COLOR_BG_BLUE=" \033[44m"
43+ export ENV_COLOR_BG_MAGENTA=" \033[45m"
44+ export ENV_COLOR_BG_CYAN=" \033[46m"
45+ export ENV_COLOR_BG_WHITE=" \033[47m"
46+ # Reset Color
47+ export ENV_COLOR_RESET=" $( tput sgr0) "
48+
49+ # status
50+ export ENV_YES=0
51+ export ENV_NO=1
52+ export ENV_SUCCEED=0
53+ export ENV_FAILED=1
54+
55+
56+ # ------------------------------------------------------------------------------ util functions
57+
58+ # 显示打印日志的时间
59+ SHELL_LOG_TIMESTAMP=$( date " +%Y-%m-%d %H:%M:%S" )
60+ # 那个用户在操作
61+ USER=$( whoami)
62+ # 日志路径
63+ LOG_PATH=${ENV_LOG_PATH:-/ var/ log/ shell.log}
64+ # 日志目录
65+ LOG_DIR=${LOG_PATH%/* }
66+
67+ createLogFileIfNotExists () {
68+ if [[ ! -x " ${LOG_PATH} " ]]; then
69+ mkdir -p " ${LOG_DIR} "
70+ touch " ${LOG_PATH} "
71+ fi
72+ }
973
10- LINUX_SCRIPTS_LIB_DIR=` dirname ${BASH_SOURCE[0]} `
74+ redOutput () {
75+ echo -e " ${ENV_COLOR_RED} $@ ${ENV_COLOR_RESET} "
76+ }
77+ greenOutput () {
78+ echo -e " ${ENV_COLOR_B_GREEN} $@ ${ENV_COLOR_RESET} "
79+ }
80+ yellowOutput () {
81+ echo -e " ${ENV_COLOR_YELLOW} $@ ${ENV_COLOR_RESET} "
82+ }
83+ blueOutput () {
84+ echo -e " ${ENV_COLOR_BLUE} $@ ${ENV_COLOR_RESET} "
85+ }
86+ magentaOutput () {
87+ echo -e " ${ENV_COLOR_MAGENTA} $@ ${ENV_COLOR_RESET} "
88+ }
89+ cyanOutput () {
90+ echo -e " ${ENV_COLOR_CYAN} $@ ${ENV_COLOR_RESET} "
91+ }
92+ whiteOutput () {
93+ echo -e " ${ENV_COLOR_WHITE} $@ ${ENV_COLOR_RESET} "
94+ }
1195
12- if [[ ! -x ${LINUX_SCRIPTS_LIB_DIR} /utils.sh ]]; then
13- logError " ${LINUX_SCRIPTS_LIB_DIR} /utils.sh not exists!"
14- exit 1
15- fi
96+ logInfo () {
97+ echo -e " ${ENV_COLOR_B_GREEN} [INFO] $@ ${ENV_COLOR_RESET} "
98+ createLogFileIfNotExists
99+ echo " [${SHELL_LOG_TIMESTAMP} ] [${USER} ] [INFO] [$0 ] $@ " >> " ${LOG_PATH} "
100+ }
101+ logWarn () {
102+ echo -e " ${ENV_COLOR_B_YELLOW} [WARN] $@ ${ENV_COLOR_RESET} "
103+ createLogFileIfNotExists
104+ echo " [${SHELL_LOG_TIMESTAMP} ] [${USER} ] [WARN] [$0 ] $@ " >> " ${LOG_PATH} "
105+ }
106+ logError () {
107+ echo -e " ${ENV_COLOR_B_RED} [ERROR] $@ ${ENV_COLOR_RESET} "
108+ createLogFileIfNotExists
109+ echo " [${SHELL_LOG_TIMESTAMP} ] [${USER} ] [ERROR] [$0 ] $@ " >> " ${LOG_PATH} "
110+ }
16111
17- source ${LINUX_SCRIPTS_LIB_DIR} /utils.sh
112+ printInfo () {
113+ echo -e " ${ENV_COLOR_B_GREEN} [INFO] $@ ${ENV_COLOR_RESET} "
114+ }
115+ printWarn () {
116+ echo -e " ${ENV_COLOR_B_YELLOW} [WARN] $@ ${ENV_COLOR_RESET} "
117+ }
118+ printError () {
119+ echo -e " ${ENV_COLOR_B_RED} [ERROR] $@ ${ENV_COLOR_RESET} "
120+ }
18121
122+ callAndLog () {
123+ $*
124+ if [[ $? -eq ${ENV_SUCCEED} ]]; then
125+ logInfo " $@ "
126+ return ${ENV_SUCCEED}
127+ else
128+ logError " $@ EXECUTE FAILED"
129+ return ${ENV_FAILED}
130+ fi
131+ }
19132
20- # ------------------------------------------------------------------------------ functions
133+ # ------------------------------------------------------------------------------ git functions
21134
22- GIT_LOCAL_BRANCH=
23135getGitLocalBranch () {
24- GIT_LOCAL_BRANCH=$( git symbolic-ref -q --short HEAD)
136+ export GIT_LOCAL_BRANCH=$( git symbolic-ref -q --short HEAD)
25137}
26138
27- GIT_ORIGIN_BRANCH=
28139getGitOriginBranch () {
29- GIT_ORIGIN_BRANCH=$( git rev-parse --abbrev-ref --symbolic-full-name " @{u}" )
140+ export GIT_ORIGIN_BRANCH=$( git rev-parse --abbrev-ref --symbolic-full-name " @{u}" )
30141}
31142
32143# check specified path is git project or not
144+ IS_GIT=false
33145checkGit () {
34146 local source=$1
35147 if [[ -d " ${source} " ]]; then
36- cd ${source} || return ${ENV_NO}
148+ cd ${source}
37149 # (1) delete gitstatus.tmp
38150 if [[ -f " gitstatus.tmp" ]]; then
39- rm -rf gitstatus.tmp
151+ rm -rf gitstatus.tmp
40152 fi
41153
42154 # (2) check git status
@@ -45,16 +157,13 @@ checkGit() {
45157 grep -iwq ' not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true
46158 rm -rf gitstatus.tmp
47159 if [[ ${gitStatus} == true ]]; then
48- return ${ENV_YES}
49- else
50- return ${ENV_NO}
160+ export IS_GIT=true
161+ return
51162 fi
52-
53- return ${ENV_NO}
54163 fi
55164
56165 logWarn " ${source} is not exists."
57- return ${ENV_NO}
166+ export IS_GIT=false
58167}
59168
60169# execute git clone or fetch
@@ -67,7 +176,7 @@ cloneOrPullGit() {
67176 local branch=$4
68177 local root=$5
69178
70- if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then
179+ if [[ ! ${repository} || ! ${group} || ! ${project} || ! ${branch} || ! ${root} ]]; then
71180 logError " Please input root, group, project, branch."
72181 return ${ENV_FAILED}
73182 fi
@@ -80,52 +189,31 @@ cloneOrPullGit() {
80189 local source=${root} /${group} /${project}
81190 logInfo " project directory is ${source} ."
82191 logInfo " git url is ${repository} :${group} /${project} .git."
83- mkdir -p ${root} /${group}
84192
85193 checkGit ${source}
86- if [[ " ${ENV_YES } " == " $? " ]]; then
194+ if [[ " ${IS_GIT } " == " true " ]]; then
87195 cd ${source} || return ${ENV_FAILED}
88196
89- git fetch --all
90- git checkout -f ${branch}
91- if [[ " ${ENV_SUCCEED} " != " $? " ]]; then
92- logError " <<<< git checkout ${branch} failed."
93- return ${ENV_FAILED}
94- fi
197+ git checkout ${branch}
95198 logInfo " git checkout ${branch} succeed."
96199
97- getGitOriginBranch
200+ git fetch --all
98201 git reset --hard ${GIT_ORIGIN_BRANCH}
99- if [[ " ${ENV_SUCCEED} " != " $? " ]]; then
100- logError " <<<< git reset --hard ${GIT_ORIGIN_BRANCH} failed."
101- return ${ENV_FAILED}
102- fi
103202 logInfo " git reset --hard ${GIT_ORIGIN_BRANCH} succeed."
104203
105204 git pull
106- if [[ " ${ENV_SUCCEED} " != " $? " ]]; then
107- logError " <<<< git pull failed."
108- return ${ENV_FAILED}
109- fi
110205 logInfo " git pull succeed."
111206 else
112207 git clone " ${repository} :${group} /${project} .git" ${source}
113- if [[ " ${ENV_SUCCEED} " != " $? " ]]; then
114- logError " <<<< git clone ${project} failed."
115- return ${ENV_FAILED}
116- fi
117208 logInfo " git clone ${project} succeed."
118209
119210 cd ${source} || return ${ENV_FAILED}
120211
121- git checkout -f ${branch}
122- if [[ " ${ENV_SUCCEED} " != " $? " ]]; then
123- logError " <<<< git checkout ${branch} failed."
124- return ${ENV_FAILED}
125- fi
212+ git checkout ${branch}
126213 logInfo " git checkout ${branch} succeed."
127214 fi
128215
129216 logInfo " Clone or pull git project [$2 /$3 :$4 ] succeed."
217+ cd ${SOURCE_DIR}
130218 return ${ENV_SUCCEED}
131219}
0 commit comments