git 检查更新文件_Git通过差异比对快速打包待更新文件(SQL)

019e88ba98f56ebb5428ca7c41dea8b3.png

本文实现主要用到的命令:

  1. GIT命令: git pull
  2. GIT命令: git log
  3. GIT命令: git diff
  4. 文件读取: head
  5. 文件写入: sed

我是在X度上搜不到完整的方案(单个命令的用法自行X度),结合多方面杂乱的搜索结果、自己Windows7环境实验得出的结论。

因此直接上我本地可执行的代码,后续如果有人感兴趣,可以再展开详细:

#!/bin/bash
#reset

sqlDir=`pwd`
LOG_FILE=log_git_latest.txt

if [[ ! -f $LOG_FILE ]]; then
	echo 更新日志文件不存在!进行首次记录……
	echo "-------------`date +%Y-%m-%d` This is the beginning of the file.-------------" > $LOG_FILE
	first_time_latest=`git log --pretty=format:'%h %cd %cn %s 明细:%b' --date=format:'%Y-%m-%d_%H%M%S' -1`
	echo $first_time_latest
	# 往首行插入记录
	sed -i '1i'"$first_time_latest"'' $LOG_FILE
fi

echo $sqlDir
echo 上次获取到最新的commit、时间:
# 读取首行记录
last_time_latest=`head -1 log_git_latest.txt`
echo $last_time_latest

echo 开始获新...
git pull 1> /dev/null

echo 本次获取到最新的commit、时间:
this_time_latest=`git log --pretty=format:'%h %cd %cn %s 明细:%b' --date=format:'%Y-%m-%d_%H%M%S' -1`
echo $this_time_latest

SQL_PACK_FILE=`date +%Y-%m-%d_%H%M%S`.tar.gz
echo 本次更新将打包生成 [ $SQL_PACK_FILE ],包含以下文件:
#git diff ${last_time_latest:0:7}..${this_time_latest:0:7} --name-only 
git diff ${last_time_latest:0:7}..${this_time_latest:0:7} --name-only --diff-filter=ACMR | grep .sql  | xargs tar -zcvf $SQL_PACK_FILE


# 往首行插入记录
sed -i '1i'"$this_time_latest"'  生成的SQL更新包:'"$SQL_PACK_FILE"'' $LOG_FILE

GIT的log、diff命令可能出现“中文文件名乱码”的问题,需要进行如下配置:

git config --global core.quotepath false          # 显示 status 编码
git config --global gui.encoding utf-8            # 图形界面编码
git config --global i18n.commit.encoding utf-8    # 提交信息编码
git config --global i18n.logoutputencoding utf-8  # 输出 log 编码

如果是Linux,因为 git log 默认使用 less 分页,所以需要对 less 命令进行 utf-8 编码:

export LESSCHARSET=utf-

ca19d4598076a754a331ba02a25e40f8.png
效果图,加示意箭头
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值