Subversion代码合并通用Shell脚本

该脚本仅适用于Subversion代码仓库,主要功能是将分支代码合并至目标仓库中,合并时自动生成并导出diff文件,用户确认差异文件无误后方可继续。

#!/bin/bash

# 代码合并通用脚本 (适合Subversion版本库)
# 服务器必须安装lrzsz服务
# 脚本参数说明:
# -a 项目名称(代码在当前服务器的存放目录)
# -c 代码完整路径
# -v 分支完整路径(末端位置与代码路径匹配)
# -u 仓库用户名称(留空为默认)
# -p 仓库用户密码(留空为默认)
# ./mergeCode.sh -a ${prjName} -c ${codeUrl} -v ${versUrl} -u ${svnUser} -p ${svnPass}

# 分支代码存放目录
versDir="vers"
# 差异文件存放目录
diffDir="diff"
# 代码合并日志文件
workLog="${PWD}/mergeCode.log"

while getopts 'a:c:v:u:p:' OPT
do
  case ${OPT} in
    a) prjName="$OPTARG";;
    c) codeUrl="$OPTARG";;
    v) versUrl="$OPTARG";;
    u) svnUser="$OPTARG";;
    p) svnPass="$OPTARG";;
  esac
done
[ -z "${svnUser}" ] && svnUser="myUserame"
[ -z "${svnPass}" ] && svnPass="myPassword"
flag=0
[ -z "${prjName}" ] && echo "缺少项目名称" && flag=1
[ -z "${codeUrl}" ] && echo "缺少代码路径" && flag=1
[ -z "${versUrl}" ] && echo "缺少分支路径" && flag=1
[[ "${codeUrl}" =~ ^svn.* ]] || [[ "${codeUrl}" =~ ^http.* ]] || flag=1
[[ "${versUrl}" =~ ^svn.* ]] || [[ "${versUrl}" =~ ^http.* ]] || flag=1

getTime (){
  nowTime=`date +'%Y%m%d%H%M%S'`
}

getResult (){
  getTime
  [ ${getStatus} -ne 0 ] && echo -e "${nowTime}\t上一条指令执行失败\n"  >> ${workLog} && exit 1
}

getTime
echo ">>>>>>>>>>>>>>> ${prjName}-${nowTime:0:8} <<<<<<<<<<<<<<<" >> ${workLog}

# 检查必要参数
[ ${flag} -eq 1 ] && echo -e "${nowTime}\t缺少必要参数或者参数错误导致无法合并代码\n" >> ${workLog}

# 检查工作空间
workDir="${PWD}/MERGE/${prjName}"
[ ! -d "${workDir}" ] && mkdir -p ${workDir} && getTime && echo -e "${nowTime}\t成功创建本地工作空间" >> ${workLog}

# 开始合并代码
cd ${workDir}
getTime
verInfo=`svn info ${codeUrl##*/} | grep -Ew "Revision|版本" | awk '{print $2}'`
if [ ${verInfo} -gt 1 ]
then
  echo -e "${nowTime}\t更新代码${codeUrl}" >> ${workLog}
  svn update ${codeUrl##*/} --username ${svnUser} --password ${svnPass}
  getStatus=$?
else
  [ -d "${codeUrl##*/}" ] rm -rf ${codeUrl##*/}
  echo -e "${nowTime}\t检出代码${codeUrl}" >> ${workLog}
  svn co ${codeUrl} --username ${svnUser} --password ${svnPass}
  getStatus=$?
fi
getResult
sleep 1
getTime
echo -e "${nowTime}\t检出分支${versUrl}" >> ${workLog}
[ -d "${versDir}/${versUrl##*/}" ] && rm -rf ${versDir}/${versUrl##*/}
svn co ${versUrl} ${versDir}/${versUrl##*/} --username ${svnUser} --password ${svnPass}
getStatus=$?
getResult
getTime
if [ "${codeUrl##*/}" != "${versUrl##*/}" ]
then
  echo -e "复制分支代码至版本库时发现末端目录不匹配\n返回仓库核对代码分支的末端目录是否正常"
  while true
  do
    read -p "确定复制代码输入'Y'继续,输入'E'退出 - " toNEXT
    [ "${toNEXT^^}" = "Y" ] && break
    [ "${toNEXT^^}" = "E" ] && exit 8
  done
fi
echo -e "${nowTime}\t复制本地分支代码至本地版本库" >> ${workLog}
cp -rf ${versDir}/${versUrl##*/}/* ${codeUrl##*/}/
getStatus=$?
getResult
sleep 1
if [ -n "`svn st ${codeUrl##*/} | awk '{if ( $1 == "?" ) {print $2}}'`" ]
then
  getTime
  echo -e "${nowTime}\t向仓库中预添加新增的文件" >> ${workLog}
  svn st ${codeUrl##*/} | awk '{if ( $1 == "?" ) {print $2}}' | xargs svn add
  getStatus=$?
  getResult
fi
sleep 1
getTime
if [ -n "`svn st ${codeUrl##*/}`" ]
then
  echo -e "${nowTime}\t生成当前版本diff文件" >> ${workLog}
  [ ! -d "${diffDir}" ] && mkdir -p ${diffDir}
  diffFile="${versUrl##*/}-${nowTime}.diff"
  svn diff ${codeUrl##*/} > ${diffDir}/${diffFile}
  getStatus=$?
  getResult
  getTime
  echo -e "${nowTime}\t差异文件位置${workDir}/${diffDir}/${diffFile}" >> ${workLog}
  sz -r -y ${diffDir}/${diffFile}
  sleep 1
  while true
  do
    read -p "核对diff无差异输入'Y'继续,输入'E'退出 - " toNEXT
    [ "${toNEXT^^}" = "Y" ] && break
    [ "${toNEXT^^}" = "E" ] && exit 9
  done
  getTime  
  echo -e "${nowTime}\t向仓库中提交代码" >> ${workLog}
  svn ci -m "合并代码${versUrl}" ${codeUrl##*/}/* --username ${svnUser} --password ${svnPass}
  getStatus=$?
  getResult
else
  echo -e "${nowTime}\t没有需要提交的代码" >> ${workLog}
fi
  
echo -e ">>>>>>>>>>>>>>>>>>>> 完成代码合并 <<<<<<<<<<<<<<<<<<<<\n" >> ${workLog}  

原创文章禁止转载:技术学堂 » Subversion代码合并通用Shell脚本

精彩评论

6+9=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏