1. 程式人生 > >linux shell git相關操作

linux shell git相關操作

公司專案模組比較多,用指令碼去拉取程式碼等比較便捷,這個只是git相關操作的指令碼,僅供參考.
#! /bin/bash

### 字型顏色
#黑 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

		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

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

		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