|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# 装载其它库 |
| 4 | +ROOT=`dirname ${BASH_SOURCE[0]}` |
| 5 | +source ${ROOT}/env.sh |
| 6 | + |
| 7 | +# ------------------------------------------------------------------------------ git 操作函数 |
| 8 | + |
| 9 | +# 检查指定的路径是不是一个 git 项目 |
| 10 | +checkGit() { |
| 11 | + local source=$1 |
| 12 | + if [[ -d "${source}" ]]; then |
| 13 | + cd ${source} || return ${NO} |
| 14 | + # (1)删除git状态零时文件 |
| 15 | + if [[ -f "gitstatus.tmp" ]]; then |
| 16 | + rm -rf gitstatus.tmp |
| 17 | + fi |
| 18 | + |
| 19 | + # (2)判断是否存在 .git 目录 |
| 20 | + if [[ -d "./.git" ]]; then |
| 21 | + # (3)判断git是否可用 |
| 22 | + git status &> gitstatus.tmp |
| 23 | + local gitStatus=false |
| 24 | + grep -iwq 'not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true |
| 25 | + rm -rf gitstatus.tmp |
| 26 | + if [[ ${gitStatus} == true ]]; then |
| 27 | + return ${YES} |
| 28 | + else |
| 29 | + return ${NO} |
| 30 | + fi |
| 31 | + fi |
| 32 | + |
| 33 | + return ${NO} |
| 34 | + fi |
| 35 | + |
| 36 | + printf "${C_B_B_YELLOW}${source} is invalid dir.${C_RESET}\n" |
| 37 | + return ${NO} |
| 38 | +} |
| 39 | + |
| 40 | +# clone 或 fetch 操作 |
| 41 | +# 如果本地代码目录已经是 git 仓库,执行 pull;若不是,则执行 clone |
| 42 | +# 依次传入 Git 仓库、Git 项目组、Git 项目名、分支、本地代码目录 |
| 43 | +cloneOrPullGit() { |
| 44 | + |
| 45 | + local repository=$1 |
| 46 | + local group=$2 |
| 47 | + local project=$3 |
| 48 | + local branch=$4 |
| 49 | + local root=$5 |
| 50 | + |
| 51 | + if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then |
| 52 | + printf "${C_B_YELLOW}>>>> Please input root, group, project, branch.${C_RESET}\n" |
| 53 | + return ${FAILED} |
| 54 | + fi |
| 55 | + |
| 56 | + if [[ ! -d "${root}" ]]; then |
| 57 | + printf "${C_B_YELLOW}>>>> ${root} is not directory.${C_RESET}\n" |
| 58 | + return ${FAILED} |
| 59 | + fi |
| 60 | + |
| 61 | + local source=${root}/${group}/${project} |
| 62 | + printf "${C_B_CYAN}>>>> project directory is ${source}.${C_RESET}\n" |
| 63 | + printf "${C_B_CYAN}>>>> git url is ${repository}:${group}/${project}.git.${C_RESET}\n" |
| 64 | + mkdir -p ${root}/${group} |
| 65 | + |
| 66 | + checkGit ${source} |
| 67 | + if [[ "${YES}" == "$?" ]]; then |
| 68 | + # 如果 ${source} 是 git 项目,执行 pull 操作 |
| 69 | + cd ${source} || return ${FAILED} |
| 70 | + |
| 71 | + git checkout -f ${branch} |
| 72 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 73 | + printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n" |
| 74 | + return ${FAILED} |
| 75 | + fi |
| 76 | + printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n" |
| 77 | + |
| 78 | + git reset --hard |
| 79 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 80 | + printf "${C_B_RED}<<<< git reset --hard failed.${C_RESET}\n" |
| 81 | + return ${FAILED} |
| 82 | + fi |
| 83 | + printf "${C_B_GREEN}>>>> git reset --hard succeed.${C_RESET}\n" |
| 84 | + |
| 85 | + git pull |
| 86 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 87 | + printf "${C_B_RED}<<<< git pull failed.${C_RESET}\n" |
| 88 | + return ${FAILED} |
| 89 | + fi |
| 90 | + printf "${C_B_GREEN}>>>> git pull succeed.${C_RESET}\n" |
| 91 | + else |
| 92 | + # 如果 ${source} 不是 git 项目,执行 clone 操作 |
| 93 | + |
| 94 | + git clone "${repository}:${group}/${project}.git" ${source} |
| 95 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 96 | + printf "${C_B_RED}<<<< git clone ${project} failed.${C_RESET}\n" |
| 97 | + return ${FAILED} |
| 98 | + fi |
| 99 | + printf "${C_B_GREEN}>>>> git clone ${project} succeed.${C_RESET}\n" |
| 100 | + |
| 101 | + cd ${source} || return ${FAILED} |
| 102 | + |
| 103 | + git checkout -f ${branch} |
| 104 | + if [[ "${SUCCEED}" != "$?" ]]; then |
| 105 | + printf "${C_B_RED}<<<< git checkout ${branch} failed.${C_RESET}\n" |
| 106 | + return ${FAILED} |
| 107 | + fi |
| 108 | + printf "${C_B_GREEN}>>>> git checkout ${branch} succeed.${C_RESET}\n" |
| 109 | + fi |
| 110 | + |
| 111 | + return ${SUCCEED} |
| 112 | +} |
0 commit comments