bat脚本统计提交代码行数
·
代码行数统计脚本
第一章 快速查询
阅读版本
git log \
--since="2025-07-01" \
--until="2025-07-31" \
--pretty=tformat: \
--numstat | \
awk '{ add += $1; subs += $2; loc += $1 - $2 } END {
printf "统计周期: 2025-07-01 至 2025-07-31\n新增行数: %s\n删除行数: %s\n净增行数: %s\n", add, subs, loc
}'
压缩版本
git log \--since="2025-07-01" \--until="2025-07-31" \--pretty=tformat: \--numstat | \awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "统计周期: 2025-07-01 至 2025-07-31\n新增行数: %s\n删除行数: %s\n净增行数: %s\n", add, subs, loc }'
第二章 核心代码
需要将 bat 脚本 放在 代码所在的仓库中 运行
@echo off
setlocal enabledelayedexpansion
:: 设置控制台编码为 65001 (UTF-8) 936 (GBK)
set UCODE=65001
chcp %UCODE% >nul
:: ==============================================================
:: 定义统计周期(注意: 等号周围不能有空格)
:: 参数 isNotInputTime 表示是否不需要输入时间。
:: 如果是0则需要输入时间, 如果是其他则不用输入时间,采用默认的配置
set isNotInputTime=0
set start_date=2025-08-01
set end_date=2025-08-31
:: ==============================================================
:: 配置参数
if "!isNotInputTime!"=="0" (
set /p "start_date=请输入开始时间(格式 2025-07-01): "
set /p "end_date=请输入结束时间(格式 2025-07-31): "
)
set debug_mode=false
:: 验证Git仓库
git rev-parse --is-inside-work-tree >nul 2>&1
if errorlevel 1 (
echo 错误:当前目录不是Git仓库
pause
exit /b 1
)
:: 获取当前分支名称
for /f "delims=" %%b in ('git branch --show-current') do set "current_branch=%%b"
:: 获取所有作者列表并去重
set author_index=0
for /f "tokens=*" %%A in ('git log --since^="%start_date%" --until^="%end_date%" --pretty^=format^:"%%an"') do (
set "current_author=%%A"
set "author_exists=false"
:: 检查是否已存在该作者
for /l %%I in (1,1,!author_index!) do (
if "!author_%%I!"=="!current_author!" (
set "author_exists=true"
)
)
if "!author_exists!"=="false" (
set /a author_index+=1
set "author_!author_index!=!current_author!"
set "author_added_!author_index!=0"
set "author_removed_!author_index!=0"
set "author_commits_!author_index!=0"
)
)
:: 主统计循环 - 按作者统计
for /l %%I in (1,1,%author_index%) do (
set "current_author=!author_%%I!"
for /f "delims=" %%H in ('git log --since^="%start_date%" --until^="%end_date%" --author^="!current_author!" --pretty^=format^:"%%H"') do (
set /a author_commits_%%I+=1
if "%debug_mode%"=="true" (
echo 正在处理提交: %%H by !current_author!
git show --numstat --oneline %%H | findstr /r "^[0-9]"
)
for /f "tokens=1,2 delims= " %%A in ('git show --numstat --pretty^=format^:"" %%H ^| findstr /r "^[0-9][0-9]*"') do (
if not "%%A"=="-" set /a author_added_%%I+=%%A
if not "%%B"=="-" set /a author_removed_%%I+=%%B
if "%debug_mode%"=="true" (
echo 添加:%%A 删除:%%B
)
)
)
)
:: 计算总行数
set added_total=0
set removed_total=0
set commit_count=0
for /l %%I in (1,1,%author_index%) do (
set /a added_total+=!author_added_%%I!
set /a removed_total+=!author_removed_%%I!
set /a commit_count+=!author_commits_%%I!
)
set /a net_total=added_total - removed_total
echo.
echo.
:: 输出报告
echo ============== 统计报告[作者统计] ==============
:: 按作者输出
for /l %%I in (1,1,%author_index%) do (
set /a author_net=!author_added_%%I! - !author_removed_%%I!
echo.
echo 作者: !author_%%I!
echo 当前分支: %current_branch%
echo 提交次数: !author_commits_%%I!
echo 增加行数: !author_added_%%I!
echo 删除行数: !author_removed_%%I!
echo 净增行数: !author_net!
echo --------------------------------
)
echo.
echo ============== 统计报告[汇总信息] ==============
echo.
echo 统计周期: %start_date% 至 %end_date%
echo 当前分支: %current_branch%
echo.
echo 总提交次数: %commit_count%
echo 总增加行数: %added_total%
echo 总删除行数: %removed_total%
echo 净增行数: %net_total%
echo ================================================
if %commit_count% equ 0 (
echo.
echo 警告:在指定日期范围内未发现任何提交!
echo.
echo 检查建议:
echo.
echo 1. 确认日期格式为YYYY-MM-DD
echo.
echo 2. 运行以下命令测试:git log --since="%start_date%" --until="%end_date%" --oneline
echo.
)
pause
运行结果

更多推荐


所有评论(0)