git 版本差异 同步 目录文件操作

如下需求:
一个git工作仓库需要每天提交推送到远端,而且需要每天同步当天或最近几天的修改的代码到服务器部署目录,增量!
一般是采用:1.手动选择分支,2.比对分支差异,3.复制覆盖或者删除差异文件 这种方式,每次做这种重复的事情就很难受啊,所以便考虑使用shll脚本实现以上功能岂不美哉(⊙﹏⊙)
于是进入仓库路径,使用以下几个简单命令

#展示某时间段提交的版本信息 包含提交者、提交备注的显示
git log --pretty=format:"%h %an %cr %s " --after=2018-04-01 --before=2018-04-22
#只显示版本号 以便接下来的操作
git log --pretty=format:%H --after=$time_from --before=$time_to
#比对分支之间的差异文件列表
git diff $1 $2 --stat --name-only

所以结合shell编写如下:
help_git.sh

#!/bin/bash
###########################################
#git版本差异化 期间类补丁 文件覆盖制作
#
#
##-------------------------------- 

#app 下面复制到desktop下面 而git路径在echat_desktop下面所以需要路径转换映射
dirs_from=('/mnt/e/workspace/echat_desktop'     '/mnt/e/workspace/OBCP-Server'            )   #git目录
dirs_diff=('/mnt/e/workspace/echat_desktop/app' '/mnt/e/workspace/OBCP-Server'        )   #源路径截取 把源文件app下面的东西按照原有路径层次同步到目标路径
dirs_to=('/mnt/e/workspace/obcpweb/pro/desktop' '/mnt/e/workspace/obcpweb'            )   #覆盖同步路径
dir_open=''


# //git 日志格式化
# git log --pretty=format:"%H %an %cd %cr"
# git log --pretty=format:"%H %an %cd %cr" --after="2018-4-09 17:37:42" --before="2022-11-06 17:45:42"
# aa6492c71ea38371d95f26fc705ebc9be1edfd19 walker Wed Apr 11 10:41:03 2018 +0800 36 minutes ago
# e4514488d2772ea2acb8e62442eaea6e3331dbec walker Tue Apr 10 15:34:20 2018 +0800 20 hours ago
# e68d8075414572e8097e312dd02e2dfefc45a358 walker Mon Apr 9 18:42:27 2018 +0800 2 days ago
# //使用diff导出差异文件列表
# git diff aa6492c71ea38371d95f26fc705ebc9be1edfd19 e4514488d2772ea2acb8e62442eaea6e3331dbec --stat --name-only

# ./help_git.sh time_from time_to
function make(){
    dir_open=`pwd`  #记录使用脚本的路径

    time_from='2018-4-10'
    time_to='2099-11-06'

    if [ "$#" = "2" ]   #2018-4-10 2018-11-1
    then
        time_from=$1
        time_to=$2
    else
        if [ "$#" = "1" ] #2018-4-10
        then
            time_from=$1
            time_to=`date "+%Y-%m-%d" `
        else
            time_from=`date "+%Y-%m-%d" -d yesterday`
            time_to=`date "+%Y-%m-%d" `
        fi
    fi
    echo '时间 '$time_from' -> '$time_to

    # echo ${#dirs_from[@]}
    for ((i=0; i<${#dirs_from[@]}; i++))
    do
        local dir_from=${dirs_from[$i]}
        local dir_diff=${dirs_diff[$i]}
        local dir_to=${dirs_to[$i]}   
        line
        echo '同步开始 '$dir_diff' -> '$dir_to 
        line
        log $dir_from $dir_diff $dir_to $time_from $time_to &   #异步同步等待 
        wait

        echo '同步完成 '$dir_diff' -> '$dir_to
        line
        echo
    done
    cd $dir_open    #回到当初位置
}

function log(){
    # echo $@
    local nowdir=$1
    local diffdir=$2
    local todir=$3
    local timefrom=$4
    local timeto=$5
    cd $nowdir  #进入源目录
    local cmd="git log --pretty=format:%H --after=$time_from --before=$time_to"
    echo $cmd
    line
    local branchs=`$cmd`    #期间分支集合
    # echo $branchs
    git log --pretty=format:"%h %an %cr %s " --after=$time_from --before=$time_to

    local arr=(` echo $branchs | tr ' ' "\n" `)
    if (( ${#arr[@]} < 2 ))
    then
        echo '期间未提交新的分支'
        line
    else
        local branch_from=${arr[-1]}
        local branch_to=${arr[0]}
        diff $branch_from $branch_to $nowdir $diffdir $todir   #比对分支差异文件并复制移动
    fi    



}
function diff(){
    if [ "$#" = "5" ] 
    then
        local nowdir=$3
        local diffdir=$4
        local todir=$5

        local diffdirLen=${#diffdir}+1  #/斜杠占位
        line
        local cmd="git diff $1 $2 --stat --name-only"
        echo $cmd
        local files=( `$cmd` )
        line
        echo '共计差异文件 '${#files[@]}' 个. '
        line
        #echo ${files[@]}
        echo '路径 '$diffdir' -> '$todir' '
        local delFileCount=0    # 差异删除文件数
        local cpFileCount=0     # 差异覆盖/添加文件数
        local delFileCountRel=0 # 实际删除文件数
        for ((i=0; i<${#files[@]}; i++))
        do
            local itemdiff=${files[$i]}                    #app/modules/chat/services/chatService.js
            local fileFromPath=$nowdir'/'$itemdiff         #/mnt/e/workspace/echat_desktop                 /app    /modules/chat/services/chatService.js 源文件真实路径
            local newItemDiff=${fileFromPath:$diffdirLen}  #                                                       /modules/chat/services/chatService.js 目标相对路径
            local fileToPath=$todir'/'$newItemDiff         #/mnt/e/workspace/obcpweb/pro/desktop                   /modules/chat/services/chatService.js 目标真实路径
            # echo $i' '$fileFromPath' -> '$fileToPath
            mkFileDir $fileToPath   #确保文件所在目录存在 否则cp失败
            if [ -f $fileFromPath ] # 源文件存在 
            then
                echo "$i cp $newItemDiff"
                cpFileCount=$[cpFileCount+1]
                # echo "cp $fileFromPath $fileToPath "
                cp $fileFromPath $fileToPath #-v    修改/添加文件 则覆盖文件
            else
                echo "$i rm $newItemDiff"
                delFileCount=$[delFileCount+1]
                # echo "rm $fileToPath"
                if [ -f $fileToPath ] # 目标文件存在 
                then
                    delFileCountRel=$[delFileCountRel+1]
                    rm $fileToPath  # 删除文件 源不存在了 目标存在的情况 就删除目标文件
                fi
            fi    
        done
        line
        echo "差异添加/修改: $cpFileCount  差异删除: $delFileCount  实际删除: $delFileCountRel"
    else
        echo '比对分支并移动需要 版本号from to 和 源路径 目标路径 args:'$@
    fi
}



function mkFileDir(){
    myPath=$1
    local fileDir=${myPath%/*}
    if [ ! -d $fileDir ]; then
        mkdir $fileDir -p
    fi
}

function line(){
    echo "---------------------------------"
}

function help(){
    echo "-------make --------"
}

function git_main(){
    echo
    ##########################do something yourself
    git_init $@
    echo
}

function git_init(){
    method=$1
    if [[ "$method" != "" ]]
    then
        rootParams=($@)
        params=(${rootParams[@]:1})
        $method ${params[@]}
    else
        help 
    fi
} 


# 单独执行文件时操作如下 引入时提示
_temp='help_git.sh'
if [[ $0 =~ $_temp ]]
then
    # git_main $@
    make $@
else
    echo 'source '$_temp
fi 

使用方式:
./help_git.sh (from_time) (to_time)
即可把配置的多个git项目源路径 最近几天的 提交的差异文件 增量覆盖或删除到目标项目路径(补丁包或者目标git项目或甚至结合ssh推送到远端服务器) 就可以比较方便的结合版本提交来实现增量打补丁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值