公司项目模块比较多,用脚本去拉取代码等比较便捷,这个只是git相关操作的脚本,仅供参考.
#! /bin/bash
###
### git的相关操作
###
### 字体颜色
#黑 1高亮度显示 0正常显示
black_color="\E[1;30m"
#红
red_color='\E[0;31m'
#绿
green_color='\E[1;32m'
#黄
yellow_color='\E[1;33m'
#蓝
bule_color='\E[0;34m'
#紫
purple_color='\E[1;35m'
#深绿
darkgreen_color='\E[1;36m'
#白
white_color='\E[1;37m'
reset='\E[0m'
### 读取git工具编号函数
function readArgsFromConsole(){
# git工具编号
num=$1
# git命令list长度
command_list_len=$2
# 错误信息提示
errMsg=$3
while [[ $num -lt 0 ]] || [[ $num -ge $command_list_len ]]
do
echo -e $red_color$errMsg$reset
read num
done
}
function executeCommand(){
# git 命令
command_name=$1
# 项目名称
projectName=$2
# 分支名称
branchName=$3
# 备注说明
message=$4
if [[ $projectName == "all" ]]
then
for projectItem in ${project[@]:0:(${#project[*]}-1)}
do
executeCommand $command_name $projectItem $branchName $message
done
else
# 如果 projectName为目录,则为真
if [[ ! -d $projectName ]]
then
echo -e $red_color"该项目['$projectName']不存在,请检查!"$reset
exit
fi
echo -e $bule_color"进入项目['$projectName']:"$reset
cd $projectName
git_repository="origin"
if [[ $command_name == "pull" ]]
then
git pull $git_repository $branchName
elif [[ $command_name == "push" ]]
then
git add . && git commit -m $message
git push $git_repository $branchName:$branchName
elif [[ $command_name == "checkout" ]]
then
git checkout $branchName
elif [[ $command_name == "checkout_new" ]]
then
git checkout -b $branchName
elif [[ $command_name == "clean" ]]
then
git checkout .
elif [[ $command_name == "merge" ]]
then
git merge --no-ff $branchName
else
git tag -a 'v_'$branchName -m $message
fi
cd ..
fi
}
project=(a b c d all)
command_list=(pull checkout push tag checkout_new clean merge)
echo "该脚本提供了一些常用的批量操作工具,比如:批量提交、批量更新、批量切换分支、批量打项目tag工具"
echo "当前脚本路径是"$PWD
echo "该脚本提供git的一些工具:"
for(( index=0;index<${#command_list[*]};index++ ))
do
command_name=${command_list[$index]}
echo $index". "$command_name" 项目"
done
read -p "请选择工具编号:" command_index
readArgsFromConsole $command_index ${#command_list[*]} '请重新选择工具编号:'
### 打包状态检测
check_pack_status=`ps -ef | grep package.*.sh | grep -v "grep" | wc -l`
if [[ $check_pack_status -gt 0 ]]
then
echo -e $red_color"正在进行打包请勿进行git操作"$reset
exit
fi
echo "将该命令作用于以下的项目:"
for(( i=0; i<${#project[*]}; i++ ))
do
echo $i"."${project[$i]}
done
read -p "请输入项目的编号[支持多项目,多项目数字以空格分割]:" project_indexs
### 项目索引合法判断
for project_index in ${project_indexs[*]}
do
if [[ $project_index -lt 0 ]] || [[ $project_index -ge ${#project[*]} ]]
then
echo -e $red_color"您输入的数字有误,请重新输入:"$reset
read project_indexs
fi
done
read -p "请输入分支名:" branch_name
# 如果 branch长度为零,则为真
if [[ -z $branch_name ]]
then
echo "没有输入特定的分支名,将使用默认的分支名[master]"
branch_name="master"
fi
### push tag 命令的备注说明
case $command_index in
0|1 )
message=""
;;
2|3 )
echo "请输入备注:"
read message
# 如果 message长度为零,则为真
if [[ -z $message ]]
then
message="update"
fi
;;
esac
for project_index in ${project_indexs[*]}
do
projectName=${project[$project_index]}
command_name=${command_list[$command_index]}
echo -e $bule_color"将该命令["$command_name"]作用于以下的项目:"
if [[ $projectName == "all" ]]
then
for projectItem in ${project[@]:0:(${#project[*]}-1)}
do
echo $projectItem
done
else
echo $projectName
fi
# 字体颜色是成对出现的,不然会一直显示没有结束标志的字体颜色
echo -e $reset
executeCommand $command_name $projectName $branch_name $message
done