git-code-check
v1.0.1
Published
在git commit提交代码的时候,检测提交文件的注释率、重复率
Downloads
305
Readme
git-code-check
在 git commit 提交代码时,检测提交文件的注释率与重复率。
- 拦截对象是 git commit 改动的文件(git 暂存区),不做全量扫描
- 路径参数仅用于限定检测范围:只检测暂存文件中位于这些路径下的改动
- 注释率:JS 代码中注释行占比(默认要求 ≥ 15%)
- 重复率:基于 token 级后缀数组 + LCP 算法的代码重复检测(默认 ≤ 10%)
- 支持
.js / .jsx / .ts / .tsx / .vue;JSX 元素行不计入代码,vue 只统计<script>块
安装
# 方式一:本地项目依赖
npm install git-code-check
# 方式二:全局安装(可在任意目录使用 git-code 命令)
npm install -g git-code-check
# 本地开发时全局链接
npm link使用
git-code check-comment [路径...] [选项] # 检测注释率
git-code check-duplication [路径...] [选项] # 检测重复率| 子命令 | 路径(限定范围) | 选项 |
| --- | --- | --- |
| check-comment | 文件/目录,只检测其中 git 改动的文件;不传则检测全部改动文件 | -r, --rate <百分比>(默认 15) |
| check-duplication | 同上 | -r, --max-rate <百分比>(默认 10)、-t, --min-token <数量>(默认 50)、-l, --min-lines <数量>(默认 5) |
各子命令详细用法:git-code check-comment --help、git-code check-duplication --help
# 示例(均在 git 仓库内运行,检测的是暂存区改动的文件)
git-code check-comment # 检测全部改动文件
git-code check-comment src/ # 只检测 src/ 下的改动文件
git-code check-comment src/App.jsx -r 20 # 只检测该文件的改动
git-code check-duplication src/ -r 5 -t 30 -l 3注意:非 git 仓库目录下运行会报错;未暂存(未
git add)的文件不会被检测。
不达标时命令以退出码 1 结束,可在 git hooks 中直接使用:
# .git/hooks/pre-commit
#!/bin/sh
git-code check-comment && git-code check-duplication本地脚本
npm run check-comment -- src/ # 只检测 src/ 下 git 改动的文件
npm run check-duplication -- src/ -r 5
npm test # 功能测试(临时 git 仓库 + example/ 示例)