Git全量打包和增量打包

需求

  • 项目使用 git 作为版本管理工具,使用的 GitLab 在内网搭建的
  • 外网上线时,需要将项目代码打包成.zip文件发送给运维人员
  • 每次手动压缩文件有点麻烦,改成使用批处理文件处理
  • 只有在master分支才可以打包
  • post-push-full.bat用于全量打包,双击执行即可
  • post-push.bat用于增量打包,双击执行。需要输入SHA-1。打包该SHA-1之后变动的文件。默认打包最后一条记录的修改文件
  • 打包生成的zip文件命名为project_20191216.zip。其中20191216是打包时的日期
  • 每次打包生成一个gitlog.html文件,显示最近的git提交记录
  • 配置.gitignore,不要将这几个文件提交到git上面

思路

  • 压缩成zip可以使用7-Zip
  • 使用git log命令可以查看最近的提交记录
  • 可以使用git symbolic-ref --short -q HEAD或者git rev-parse --abbrev-ref HEAD获取分支名称

备注

  • bat文件可以使用记事本新建,新建完成后修改扩展名即可
  • bat文件的编码格式是GB2312。如果使用其他编码格式,可能会造成中文等显示乱码

实现

安装7-Zip

比如安装位置:C:\Program Files\7-Zip

  • 点击我的电脑右键属性高级环境变量xx的用户变量 → 找到Path双击编辑 → 点击新建 → 输入C:\Program Files\7-Zip
  • 设置完成后需要重启电脑
  • 打开cmd,输入7z可以显示相关命令

全量打包

  • 在项目目录下新建文件post-push-full.bat
echo on echo "开始导出本地提交变动..." @echo ^<html^>^<head^> ^<meta name="viewport" content="width=device-width,initial-scale=1"^> ^</head^>^<body^> ^<h2^>Recent Change log^</h2^> > gitlog.html git log -n 100 --pretty=format:"<br/><h2 style='margin-bottom: 5px;'><span style='font-size: 14px;'>%%cd</span></h2><div style='font-size: 12px;'>%%an &nbsp; %%ae</div><div style='font-size: 14px;'>%%s</div>" --date=iso >> gitlog.html @echo ^<body^>^</html^> >> gitlog.html set "hour=%time:~0,2%" if "%hour:~0,1%" == " " set hour=0%hour:~1,1% set "min=%time:~3,2%" if "%min:~0,1%" == " " set min=0%min:~1,1% set "t=%date:~0,4%%date:~5,2%%date:~8,2%%hour%%min%" echo %t% set "output_prepend=./project_" set "output_append=.zip" set "output=%output_prepend%%t%%output_append%" echo %output% del "%output_prepend%*" for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set "current_branch=%%i" IF "%current_branch%"=="master" ( git archive --format zip --output "%output%" master -0 )

增量打包

  • 在项目目录下新建文件post-push.bat
echo on echo "开始导出本地提交变动..." @echo ^<html^>^<head^> ^<meta name="viewport" content="width=device-width,initial-scale=1"^> ^</head^>^<body^> ^<h2^>Recent Change log^</h2^> > gitlog.html git log -n 100 --pretty=format:"<br/><h2 style='margin-bottom: 5px;'><span style='font-size: 14px;'>%%cd</span></h2><div style='font-size: 12px;'>%%an &nbsp; %%ae</div><div style='font-size: 14px;'>%%s</div>" --date=iso >> gitlog.html @echo ^<body^>^</html^> >> gitlog.html setlocal enabledelayedexpansion set "hour=%time:~0,2%" if "%hour:~0,1%" == " " set "hour=0%hour:~1,1%" set "min=%time:~3,2%" if "%min:~0,1%" == " " set "min=0%min:~1,1%" set "t=%date:~0,4%%date:~5,2%%date:~8,2%%hour%%min%" echo %t% set "output_prepend=./project_" set "output_append=.zip" set "output=%output_prepend%%t%%output_append%" echo %output% del "%output_prepend%*" for /f %%i in ('git rev-parse --abbrev-ref HEAD') do set "current_branch=%%i" IF "%current_branch%"=="master" ( set /p commit_id=请输入版本SHA-1值: if "!commit_id!"=="" ( set "commit_id=--no-commit-id" ) echo !commit_id! git diff-tree -r !commit_id! --name-only --diff-filter=ACMRT HEAD > diff.log for /f "delims=" %%j in (diff.log) do 7z a "%output%" %%j del diff.log pause )

.gitignore

  • 修改项目的.gitignore文件,添加*.batgitlog.htmlproject_*.zip
# ... # zip project_*.zip # bat *.bat # html gitlog.html

创作不易,若本文对你有帮助,欢迎打赏支持作者!

 分享给好友: