本文实现主要用到的命令:
- GIT命令: git pull
- GIT命令: git log
- GIT命令: git diff
- 文件读取: head
- 文件写入: 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-