关闭

指定git路径,拉取远程所有的分支到本地,脚本

682人阅读 评论(0) 收藏 举报
分类:

不知道为什么,感觉整理做过的事情真的是一件特别辛苦的事情,要从头撸一遍;

先贴一下脚本吧,后面再慢慢介绍调试过程,可以不看,我写给自己看的; 

#!/bin/bash

############################################################################
#使用环境:ubuntu 12.04英文版,只要是英文版 就行,因为一些字符串的判断是是以英文来的,试过ubuntu14.04的中文版不可以,因为命令提示是中文的
#调试:s905x android6.0 SDK, s905 android 5.1 SDK,
#功能说明:拉取指定路径的git的所有远程分支到本地
#使用方法:
#1. repo 新拉取的sdk,执行该脚本;
#2. 已经在使用的sdk,可直接执行该脚本;不管当前git的状态如何,都处理为清除未提交内容,本地分支和远程分支不一致的使用远程分支;
#3. 报错文件error_path,里面的路径需自行处理,处理方法为,删除问题目录,重新repo sync,然后再执行一次脚本即可;
#4. 脚本多次执行没有问题
#5. author: 罗云  qq:741790551
#############################################################################


###################################
# 脚本使用方法: ./ly_repoladaima [first branch] [remote]
# eg: ./ly_repoladaima m-amlogic openlinux
# 如果不对则退出,并打印使用方法
# 
if [ $# -lt 2 ];then
	echo "input error, please use script as follows"
    echo "usage: ./ly_repoladaima [first branch] [remote]"
	echo "eg: ./ly_repoladaima m-amlogic openlinux"
    exit
else
    echo "first_branch=$1 , remote=$2"
	first_branch="$1"
	remote="$2"
fi
####################################

####################################################
#所有push了远程分支的仓库的 路径保存在文件ly_branch里面
#如果不存在该文件,那么退出并提示,文件内容是否符合要求这里不做判断
if [ -f ly_branch ];then
	echo "ok , has ly_branch file"
else
	echo "you need create ly_branch file,list all  git path into file ly_branch"
	echo "eg:  "
	echo "common/"
	echo "device/amlogic/"
	exit
fi
#####################################################

echo "ok,  i will start"

##############################
#执行出错的git,将路径保存在error_path中
tmp9=`pwd`
rm -rf error_path
if [ -f path_tmp ];then
	echo "has path_tmp,del it"
	rm -rf path_tmp
else
	echo "no path_tmp"
fi
#rm -rf path_tmp
touch error_path
touch path_tmp
chmod 0755 error_path
chmod 0755 path_tmp
##############################

##############################
#遍历文件ly_branch中每个path
for each_dir in `cat ly_branch`
do
	cd $each_dir
	pwd
	####################################
	#判断是否存在no branch
	git branch > ../f_tmp
	tmp=`grep -rn "no branch" ../f_tmp`
	
	if [ -n "$tmp" ];then
		#存在"no branch",	1.1  ###### 
		echo "$each_dir has \"no branch\""
		#判断是否存在first_branch                          ###重写
		#tmp1=`grep -rn "$first_branch" ../f_tmp`
		tmp1=`sed -n "/${first_branch}$/p" ../f_tmp`
		#存在first_branch,	1.1.2
		if [ -n "${tmp1}" ];then
			echo "has $first_branch"
			git branch -D $first_branch
		else
			echo "no $first_branch"
		fi

		######################################
		#删除需要git add的文件
		git status > ../f_tmp3
		tmp6=`grep -rn "use \"git add <file>" ../f_tmp3`
		if [ -n "$tmp6" ];then
			echo "$tmp6" > ../f_tmp4
			tmp7=`awk -F ':' '{print $1}' ../f_tmp4`
			echo "$tmp7"
			rm ../f_tmp4
			let "tmp8 = $tmp7 + 1"
			#echo "$tmp8"
			sed -i "1,${tmp8}d" ../f_tmp3

			tmp6=`grep -rn "use \"git add\" to track" ../f_tmp3`
			echo "$tmp6" > ../f_tmp4
			tmp7=`awk -F ':' '{print $1}' ../f_tmp4`
			#echo "$tmp7"
			rm ../f_tmp4
			sed -i "${tmp7}"',$d' ../f_tmp3
			sed -i 's/#//g' ../f_tmp3
			for each_f in `cat ../f_tmp3`
			do
				echo "$each_f"
				echo "#"
				rm -rf $each_f
			done
			#如果误删除了文件下面语句恢复
			git checkout .
		else
			echo "no git add"
		fi
		rm -rf ../f_tmp3
		######################################

		#不存在first_branch,	1.1.1
		git checkout -b $first_branch
		git remote update --prune

		#保存所有远程分支
		git branch -r > ../f_tmp1
		echo "#########"
		cat ../f_tmp1
		echo "#########"
		#删除"m/"开头的行
		sed -i "/m\//d" ../f_tmp1
		#删除"${remote}/${first_branch}"结尾的行
		sed -i "/${remote}\/${first_branch}$/d" ../f_tmp1
		echo "#########"
		cat ../f_tmp1
		echo "#########"
		#删除不包含"[remote]"的行,即删除不包含openlinux的行,但是找不到相关sed命令,所以
		#对于不匹配openlinux的行,不做处理
		for each_rbranch in `cat ../f_tmp1`
        do  
            echo $each_rbranch > ../f_tmp2
            #tmp2=`grep -rn "$remote" ../f_tmp2`
			#查找包含openlinux/的行
			tmp2=`sed -n "/${remote}\//p" ../f_tmp2`
            if [ -n "$tmp2" ];then
				#包含$remote (openlinux)
                echo "$tmp2"
                sed -i "s/${remote}\///g" ../f_tmp2
                tmp3=`cat ../f_tmp2`
				echo "$tmp3"
				#查看远程分支是否有对应的本地分支
				tmp4=`sed -n "/${tmp3}$/p" ../f_tmp`
				if [ -n "$tmp4" ];then
					#本地有远程对应的分支
					echo "remote branch $tmp3 has local branch"
					tmp5=`git diff $tmp3 ${remote}/$tmp3`
					#本地分支和远程分支是否一样
					if [ -n "$tmp5" ];then
						#不一样,删除本地分支,重新拉
						echo "git diff different"
						git branch -D $tmp3
						git fetch ${remote} $tmp3:$tmp3
					else
						echo "git diff same"
					fi
				else
					#本地没有远程对应的分支,直接拉
					echo "remote branch $tmp3 has no local branch"
					git fetch ${remote} $tmp3:$tmp3
				fi
            else
				#不包含$remote (openlinux)
                #cat ../f_tmp2
				echo "no fetch $each_rbranch "
            fi  
            rm ../f_tmp2
        done
		rm ../f_tmp1
		echo "has no branch end"
	
	else
		#不存在no branch,	1.2
		echo "not has no branch"
		#检测是否有分支${first_branch}即m-amlogic
		tmp5=`sed -n "/${first_branch}$/p" ../f_tmp`
		if [ -n "$tmp5" ];then
			#有公版分支,切换到${first_branch}即m-amlogic
			git reset --hard HEAD~0
			git checkout $first_branch
			#清一下git status的内容
			git reset --hard HEAD~0

			###############################
			#删除需要git add的文件
			git status > ../f_tmp3
			tmp6=`grep -rn "use \"git add <file>" ../f_tmp3`
			if [ -n "$tmp6" ];then
				echo "$tmp6" > ../f_tmp4
				tmp7=`awk -F ':' '{print $1}' ../f_tmp4`
				echo "$tmp7"
				rm ../f_tmp4
				let "tmp8 = $tmp7 + 1"
				#echo "$tmp8"
				sed -i "1,${tmp8}d" ../f_tmp3

				tmp6=`grep -rn "use \"git add\" to track" ../f_tmp3`
				echo "$tmp6" > ../f_tmp4
				tmp7=`awk -F ':' '{print $1}' ../f_tmp4`
				#echo "$tmp7"
				rm ../f_tmp4
				sed -i "${tmp7}"',$d' ../f_tmp3
				sed -i 's/#//g' ../f_tmp3
				for each_f in `cat ../f_tmp3`
				do
					echo "$each_f"
					echo "#"
					rm -rf $each_f
				done
				#如果误删除了文件下面语句恢复
				git checkout .
			else
				echo "no git add"
			fi
			rm -rf ../f_tmp3
			
			###############################

			git remote update --prune

			#保存所有远程分支
			git branch -r > ../f_tmp1
			#删除"m/"开头的行
			sed -i "/m\//d" ../f_tmp1
			#删除"${remote}/${first_branch}"结尾的行
			sed -i "/${remote}\/${first_branch}$/d" ../f_tmp1
			#删除不包含"[remote]"的行,即删除不包含openlinux的行,但是找不到相关sed命令,所以
			#对于不匹配openlinux的行,不做处理
			for each_rbranch in `cat ../f_tmp1`
			do  
				echo $each_rbranch > ../f_tmp2
				tmp2=`grep -rn "$remote" ../f_tmp2`
				if [ -n "$tmp2" ];then
					#包含$remote (openlinux)
					#echo "$tmp2"
					#去掉openlinux/,目的保留需要的branch名字
					sed -i "s/${remote}\///g" ../f_tmp2
					tmp3=`cat ../f_tmp2`
					#查看远程分支是否有对应的本地分支
					tmp4=`sed -n "/ ${tmp3}$/p" ../f_tmp`
					if [ -n "$tmp4" ];then
						#本地有远程对应的分支
						tmp5=`git diff $tmp3 ${remote}/$tmp3`
						#本地分支和远程分支是否一样
						if [ -n "$tmp5" ];then
							#不一样,删除本地分支,重新拉
							git branch -D $tmp3
							git fetch ${remote} $tmp3:$tmp3
						fi
					else
						#本地没有远程对应的分支,直接拉
						echo -e "#########\n"
						echo -e "${tmp3} \n"
						git fetch ${remote} ${tmp3}:${tmp3}
						echo -e "#########\n"
					fi
				else
					#不包含$remote (openlinux)
					cat ../f_tmp2
				fi  
				rm ../f_tmp2
			done
			rm ../f_tmp1

		else
			echo "no $first_branch"
			tmp10=`pwd`
			#其中$tmp10为问题path,如果需要可用$tmp10另外生成一个文件
			echo "$tmp10" >> ${tmp9}/path_tmp
			echo "$tmp10 , no $first_branch branch" >> ${tmp9}/error_path
	
		fi	 
	fi
	rm ../f_tmp
	cd -
	
done
echo -e "\n" >> ${tmp9}/error_path
cat ${tmp9}/path_tmp >>  ${tmp9}/error_path
rm -rf ${tmp9}/path_tmp
echo -e "\n########## cat error_path start ##############\n"
cat ${tmp9}/error_path
echo -e "\n########## cat error_path end ##############\n"






























好了,牢骚也发了


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4281次
    • 积分:95
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论