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

来源:互联网 发布:女生用电动牙刷知乎 编辑:程序博客网 时间:2024/05/22 07:55

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

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

#!/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 ];thenecho "input error, please use script as follows"    echo "usage: ./ly_repoladaima [first branch] [remote]"echo "eg: ./ly_repoladaima m-amlogic openlinux"    exitelse    echo "first_branch=$1 , remote=$2"first_branch="$1"remote="$2"fi#########################################################################################所有push了远程分支的仓库的 路径保存在文件ly_branch里面#如果不存在该文件,那么退出并提示,文件内容是否符合要求这里不做判断if [ -f ly_branch ];thenecho "ok , has ly_branch file"elseecho "you need create ly_branch file,list all  git path into file ly_branch"echo "eg:  "echo "common/"echo "device/amlogic/"exitfi#####################################################echo "ok,  i will start"###############################执行出错的git,将路径保存在error_path中tmp9=`pwd`rm -rf error_pathif [ -f path_tmp ];thenecho "has path_tmp,del it"rm -rf path_tmpelseecho "no path_tmp"fi#rm -rf path_tmptouch error_pathtouch path_tmpchmod 0755 error_pathchmod 0755 path_tmp#############################################################遍历文件ly_branch中每个pathfor each_dir in `cat ly_branch`docd $each_dirpwd#####################################判断是否存在no branchgit branch > ../f_tmptmp=`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.2if [ -n "${tmp1}" ];thenecho "has $first_branch"git branch -D $first_branchelseecho "no $first_branch"fi#######################################删除需要git add的文件git status > ../f_tmp3tmp6=`grep -rn "use \"git add <file>" ../f_tmp3`if [ -n "$tmp6" ];thenecho "$tmp6" > ../f_tmp4tmp7=`awk -F ':' '{print $1}' ../f_tmp4`echo "$tmp7"rm ../f_tmp4let "tmp8 = $tmp7 + 1"#echo "$tmp8"sed -i "1,${tmp8}d" ../f_tmp3tmp6=`grep -rn "use \"git add\" to track" ../f_tmp3`echo "$tmp6" > ../f_tmp4tmp7=`awk -F ':' '{print $1}' ../f_tmp4`#echo "$tmp7"rm ../f_tmp4sed -i "${tmp7}"',$d' ../f_tmp3sed -i 's/#//g' ../f_tmp3for each_f in `cat ../f_tmp3`doecho "$each_f"echo "#"rm -rf $each_fdone#如果误删除了文件下面语句恢复git checkout .elseecho "no git add"firm -rf ../f_tmp3#######################################不存在first_branch,1.1.1git checkout -b $first_branchgit remote update --prune#保存所有远程分支git branch -r > ../f_tmp1echo "#########"cat ../f_tmp1echo "#########"#删除"m/"开头的行sed -i "/m\//d" ../f_tmp1#删除"${remote}/${first_branch}"结尾的行sed -i "/${remote}\/${first_branch}$/d" ../f_tmp1echo "#########"cat ../f_tmp1echo "#########"#删除不包含"[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 $tmp3git fetch ${remote} $tmp3:$tmp3elseecho "git diff same"fielse#本地没有远程对应的分支,直接拉echo "remote branch $tmp3 has no local branch"git fetch ${remote} $tmp3:$tmp3fi            else#不包含$remote (openlinux)                #cat ../f_tmp2echo "no fetch $each_rbranch "            fi              rm ../f_tmp2        donerm ../f_tmp1echo "has no branch end"else#不存在no branch,1.2echo "not has no branch"#检测是否有分支${first_branch}即m-amlogictmp5=`sed -n "/${first_branch}$/p" ../f_tmp`if [ -n "$tmp5" ];then#有公版分支,切换到${first_branch}即m-amlogicgit reset --hard HEAD~0git checkout $first_branch#清一下git status的内容git reset --hard HEAD~0################################删除需要git add的文件git status > ../f_tmp3tmp6=`grep -rn "use \"git add <file>" ../f_tmp3`if [ -n "$tmp6" ];thenecho "$tmp6" > ../f_tmp4tmp7=`awk -F ':' '{print $1}' ../f_tmp4`echo "$tmp7"rm ../f_tmp4let "tmp8 = $tmp7 + 1"#echo "$tmp8"sed -i "1,${tmp8}d" ../f_tmp3tmp6=`grep -rn "use \"git add\" to track" ../f_tmp3`echo "$tmp6" > ../f_tmp4tmp7=`awk -F ':' '{print $1}' ../f_tmp4`#echo "$tmp7"rm ../f_tmp4sed -i "${tmp7}"',$d' ../f_tmp3sed -i 's/#//g' ../f_tmp3for each_f in `cat ../f_tmp3`doecho "$each_f"echo "#"rm -rf $each_fdone#如果误删除了文件下面语句恢复git checkout .elseecho "no git add"firm -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_tmp2tmp2=`grep -rn "$remote" ../f_tmp2`if [ -n "$tmp2" ];then#包含$remote (openlinux)#echo "$tmp2"#去掉openlinux/,目的保留需要的branch名字sed -i "s/${remote}\///g" ../f_tmp2tmp3=`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 $tmp3git fetch ${remote} $tmp3:$tmp3fielse#本地没有远程对应的分支,直接拉echo -e "#########\n"echo -e "${tmp3} \n"git fetch ${remote} ${tmp3}:${tmp3}echo -e "#########\n"fielse#不包含$remote (openlinux)cat ../f_tmp2fi  rm ../f_tmp2donerm ../f_tmp1elseecho "no $first_branch"tmp10=`pwd`#其中$tmp10为问题path,如果需要可用$tmp10另外生成一个文件echo "$tmp10" >> ${tmp9}/path_tmpecho "$tmp10 , no $first_branch branch" >> ${tmp9}/error_pathfi firm ../f_tmpcd -doneecho -e "\n" >> ${tmp9}/error_pathcat ${tmp9}/path_tmp >>  ${tmp9}/error_pathrm -rf ${tmp9}/path_tmpecho -e "\n########## cat error_path start ##############\n"cat ${tmp9}/error_pathecho -e "\n########## cat error_path end ##############\n"






























好了,牢骚也发了


0 0
原创粉丝点击