|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | +# console color |
| 5 | +BLACK="\033[1;30m" |
| 6 | +RED="\033[1;31m" |
| 7 | +GREEN="\033[1;32m" |
| 8 | +YELLOW="\033[1;33m" |
| 9 | +BLUE="\033[1;34m" |
| 10 | +PURPLE="\033[1;35m" |
| 11 | +CYAN="\033[1;36m" |
| 12 | +RESET="$(tput sgr0)" |
| 13 | + |
| 14 | +# 常量 |
| 15 | +YES=0 |
| 16 | +NO=1 |
| 17 | +SUCCEED=0 |
| 18 | +FAILED=1 |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +printf "$BLUE" |
| 22 | +cat << EOF |
| 23 | +################################################################################ |
| 24 | +# 项目初始化脚本 |
| 25 | +################################################################################ |
| 26 | +EOF |
| 27 | +printf "$RESET" |
| 28 | + |
| 29 | +# 检查指定的路径是不是一个 git 项目 |
| 30 | +checkGit() { |
| 31 | + source=$1 |
| 32 | + if [[ -d "${source}" ]]; then |
| 33 | + cd ${source} || return ${NO} |
| 34 | + # (1)删除git状态零时文件 |
| 35 | + if [[ -f "gitstatus.tmp" ]]; then |
| 36 | + rm -rf gitstatus.tmp |
| 37 | + fi |
| 38 | + |
| 39 | + # (2)判断是否存在 .git 目录 |
| 40 | + if [[ -d "./.git" ]]; then |
| 41 | + # (3)判断git是否可用 |
| 42 | + git status &> gitstatus.tmp |
| 43 | + gitStatus=false |
| 44 | + grep -iwq 'not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true |
| 45 | + rm -rf gitstatus.tmp |
| 46 | + if [[ ${gitStatus} == true ]]; then |
| 47 | + return ${YES} |
| 48 | + else |
| 49 | + return ${NO} |
| 50 | + fi |
| 51 | + fi |
| 52 | + fi |
| 53 | + return ${NO} |
| 54 | +} |
| 55 | + |
| 56 | +cloneOrFetchGit() { |
| 57 | + root=$1 |
| 58 | + group=$2 |
| 59 | + project=$3 |
| 60 | + branch=$4 |
| 61 | + |
| 62 | + if [[ ! -d "${root}" ]]; then |
| 63 | + printf "${YELLOW}>>>> ${root} is not directory.${RESET}\n" |
| 64 | + return ${FAILED} |
| 65 | + fi |
| 66 | + |
| 67 | + if [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]]; then |
| 68 | + printf "${YELLOW}>>>> group, project, branch must not be empty.${RESET}\n" |
| 69 | + return ${FAILED} |
| 70 | + fi |
| 71 | + |
| 72 | + source=${root}/${group}/${project} |
| 73 | + printf "${CYAN}>>>> project directory is ${source}.${RESET}\n" |
| 74 | + printf "${CYAN}>>>> git url is ${REPOSITORY}:${group}/${project}.git.${RESET}\n" |
| 75 | + mkdir -p ${root}/${group} |
| 76 | + |
| 77 | + checkGit ${source} |
| 78 | + if [[ "${YES}" == "$?" ]]; then |
| 79 | + # 如果 ${source} 是 git 项目,执行 pull 操作 |
| 80 | + cd ${source} || return ${FAILED} |
| 81 | + |
| 82 | + git checkout -f ${branch} |
| 83 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 84 | + printf "${RED}>>>> git checkout ${branch} failed.${RESET}\n" |
| 85 | + return ${FAILED} |
| 86 | + fi |
| 87 | + printf "${GREEN}>>>> git checkout ${branch} succeed.${RESET}\n" |
| 88 | + |
| 89 | + git reset --hard |
| 90 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 91 | + printf "${RED}>>>> git reset --hard failed.${RESET}\n" |
| 92 | + return ${FAILED} |
| 93 | + fi |
| 94 | + printf "${GREEN}>>>> git reset --hard succeed.${RESET}\n" |
| 95 | + |
| 96 | + git pull |
| 97 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 98 | + printf "${RED}>>>> git pull failed.${RESET}\n" |
| 99 | + return ${FAILED} |
| 100 | + fi |
| 101 | + printf "${GREEN}>>>> git pull succeed.${RESET}\n" |
| 102 | + else |
| 103 | + # 如果 ${source} 不是 git 项目,执行 clone 操作 |
| 104 | + |
| 105 | + git clone "${REPOSITORY}:${group}/${project}.git" ${source} |
| 106 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 107 | + printf "${RED}>>>> git clone ${project} failed.${RESET}\n" |
| 108 | + return ${FAILED} |
| 109 | + fi |
| 110 | + printf "${GREEN}>>>> git clone ${project} succeed.${RESET}\n" |
| 111 | + |
| 112 | + cd ${source} || return ${FAILED} |
| 113 | + |
| 114 | + git checkout -f ${branch} |
| 115 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 116 | + printf "${RED}>>>> git checkout ${branch} failed.${RESET}\n" |
| 117 | + return ${FAILED} |
| 118 | + fi |
| 119 | + printf "${GREEN}>>>> git checkout ${branch} succeed.${RESET}\n" |
| 120 | + fi |
| 121 | + |
| 122 | + return ${SUCCEED} |
| 123 | +} |
| 124 | + |
| 125 | +doCloneOrFetchGit() { |
| 126 | + cloneOrFetchGit $1 $2 $3 $4 |
| 127 | + if [[ "$?" == "${SUCCEED}" ]]; then |
| 128 | + printf "\n${GREEN}>>>> Update git project [$2/$3] succeed.${RESET}\n" |
| 129 | + return ${SUCCEED} |
| 130 | + else |
| 131 | + printf "\n${RED}>>>> Update git project [$2/$3] failed.${RESET}\n" |
| 132 | + return ${FAILED} |
| 133 | + fi |
| 134 | +} |
| 135 | + |
| 136 | +##################################### MAIN ##################################### |
| 137 | +ROOT=$(pwd) |
| 138 | + |
| 139 | +# 这里需要根据实际情况替换 Git 仓库、项目组、项目、代码分支 |
| 140 | +REPOSITORY="git@gitee.com" |
| 141 | + |
| 142 | +printf "${CYAN}Current path is ${ROOT}.${RESET}\n" |
| 143 | + |
| 144 | +doCloneOrFetchGit ${ROOT} turnon linux-tutorial master |
| 145 | +r1=$? |
| 146 | +doCloneOrFetchGit ${ROOT} turnon nginx-tutorial master |
| 147 | +r2=$? |
| 148 | + |
| 149 | +if [[ "${r1}" == "${SUCCEED}" && "${r2}" == "${SUCCEED}" ]]; then |
| 150 | + printf "\n${GREEN}Succeed.${RESET}\n" |
| 151 | + exit ${SUCCEED} |
| 152 | +else |
| 153 | + printf "\n${RED}Failed.${RESET}\n" |
| 154 | + exit ${FAILED} |
| 155 | +fi |
| 156 | + |
0 commit comments