如何检测composer install后手动修改并提交的vendor目录文件?
检测./vendor目录手动修改提交的方法
一、单文件提交的快速检测
直接筛选出提交中仅修改了./vendor下单个文件的记录:
git log --all --numstat --grep="vendor" | awk '$1 != "-" && $2 != "-" && $3 ~ /^vendor\// {count[$0]++} END {for (commit in count) if (count[commit] == 1) print commit}'
该命令会遍历所有提交,统计每个提交涉及vendor目录的文件数,仅输出只修改1个vendor文件的提交记录。
二、提交文件数小于vendor总文件数的检测
- 先统计当前
vendor目录的总文件数(自动排除隐藏文件,只统计普通文件):
find ./vendor -type f | wc -l
将得到的数字记为TOTAL。
- 筛选出提交中修改的
vendor文件数小于TOTAL的记录:
git log --all --numstat --grep="vendor" | awk -v total=TOTAL '$1 != "-" && $2 != "-" && $3 ~ /^vendor\// {files[$0]++} END {for (commit in files) if (files[commit] < total) print commit}'
替换命令里的TOTAL为第一步统计出的实际数字即可。
三、自动化检测脚本(一键运行)
可以编写一个shell脚本check-vendor-commits.sh,自动完成统计和检测:
#!/bin/bash # 统计vendor目录总文件数 VENDOR_TOTAL=$(find ./vendor -type f | wc -l) echo "Vendor目录总文件数: $VENDOR_TOTAL" echo "====================================" echo "检测到的不符合规范的提交:" # 遍历所有包含vendor修改的提交,筛选文件数异常的记录 git log --all --pretty=format:"%H %s" --name-only | awk -v total="$VENDOR_TOTAL" ' /^[0-9a-f]{40}/ { if (prev_files > 0) { if (prev_files == 1 || prev_files < total) { print prev_commit } } prev_commit = $0 prev_files = 0 next } /^vendor\// { prev_files++ } END { if (prev_files > 0 && (prev_files == 1 || prev_files < total)) { print prev_commit } }'
使用时给脚本添加执行权限:chmod +x check-vendor-commits.sh,然后运行./check-vendor-commits.sh即可。
四、查看具体修改内容
找到可疑提交后,用以下命令查看该提交的详细改动:
git show <commit-hash>
如果只想看vendor目录的修改:
git show <commit-hash> -- ./vendor
内容的提问来源于stack exchange,提问作者Dimitrios Desyllas
相关产品推荐
相关产品推荐

