Skip to content

CLI 命令

Git Doctor 命令行工具的完整参考。

安装

bash
# 全局安装
npm install -g git-doctor

# 或使用 npx
npx git-doctor <command>

命令概览

bash
git-doctor <command> [options]

Commands:
  analyze      运行完整诊断
  size         分析仓库体积
  commits      分析提交质量
  branches     分析分支健康度
  security     扫描敏感信息
  hotspots     分析代码热点
  export       导出诊断报告
  ai           AI 智能分析

Options:
  -h, --help     显示帮助
  -V, --version  显示版本
  --json         JSON 格式输出

analyze - 完整诊断

bash
git-doctor analyze [options]

Options:
  --json    JSON 格式输出
  --quiet   静默模式,只输出评分

示例:

bash
# 标准输出
git-doctor analyze

# JSON 输出(CI 使用)
git-doctor analyze --json

# 只输出评分
git-doctor analyze --quiet

size - 体积分析

bash
git-doctor size [options]

Options:
  --json         JSON 格式输出
  --threshold    大文件阈值(字节),默认 1048576

示例:

bash
# 使用默认阈值
git-doctor size

# 自定义阈值(500KB)
git-doctor size --threshold 512000

commits - 提交分析

bash
git-doctor commits [options]

Options:
  --json     JSON 格式输出
  --since    起始日期(如 "2024-01-01")
  --until    结束日期
  --author   只分析指定作者

示例:

bash
# 分析所有提交
git-doctor commits

# 分析最近 30 天
git-doctor commits --since "30 days ago"

# 分析指定作者
git-doctor commits --author "john@example.com"

branches - 分支分析

bash
git-doctor branches [options]

Options:
  --json          JSON 格式输出
  --stale-days    僵尸分支天数,默认 90

示例:

bash
# 使用默认配置
git-doctor branches

# 自定义僵尸分支天数
git-doctor branches --stale-days 60

security - 敏感信息扫描

bash
git-doctor security [options]

Options:
  --json         JSON 格式输出
  --use-gitleaks 强制使用 gitleaks(如果可用)

示例:

bash
# 标准扫描
git-doctor security

# 强制使用 gitleaks
git-doctor security --use-gitleaks

hotspots - 热点分析

bash
git-doctor hotspots [options]

Options:
  --json        JSON 格式输出
  --days        时间范围(天),默认 90
  --threshold   最小修改次数,默认 10
  --exclude     排除模式(可多次使用)

示例:

bash
# 默认配置
git-doctor hotspots

# 分析最近 30 天
git-doctor hotspots --days 30

# 排除特定目录
git-doctor hotspots --exclude "**/test/**" --exclude "**/docs/**"

export - 导出报告

bash
git-doctor export [options]

Options:
  -f, --format   格式:html, markdown, json, text
  -o, --output   输出文件路径

示例:

bash
# HTML 报告
git-doctor export -f html -o report.html

# Markdown 报告
git-doctor export -f markdown -o report.md

# JSON 报告
git-doctor export -f json -o report.json

ai - AI 分析

bash
git-doctor ai [options]

Options:
  --api-key      API Key
  --provider     提供商:deepseek, kimi
  -a, --action   操作:analyze, changelog, commit-msg, plan

示例:

bash
# 智能分析
git-doctor ai --api-key sk-xxx -a analyze

# 生成 CHANGELOG
git-doctor ai --api-key sk-xxx -a changelog

# 生成提交消息
git-doctor ai --api-key sk-xxx -a commit-msg

# 生成优化计划
git-doctor ai --api-key sk-xxx -a plan

输出格式

标准输出

$ git-doctor analyze

Git Doctor 仓库诊断报告
═══════════════════════════════════════

健康评分: 85/100 ████████░░

📦 仓库体积
   .git 大小: 12.5 MB
   大文件数: 3

📝 提交质量
   规范率: 78%
   总提交: 256

🌿 分支健康
   本地分支: 8
   僵尸分支: 2

🔒 安全风险
   高风险: 0
   中风险: 2

JSON 输出

json
{
  "healthScore": 85,
  "size": {
    "total": 13107200,
    "largeFiles": 3
  },
  "commits": {
    "total": 256,
    "conventionalRate": 0.78
  },
  "branches": {
    "local": 8,
    "stale": 2
  },
  "security": {
    "high": 0,
    "medium": 2
  }
}

退出码

退出码说明
0成功
1一般错误
2参数错误
3健康评分低于阈值

CI/CD 集成

GitHub Actions

yaml
- name: Health Check
  run: |
    npm install -g git-doctor
    SCORE=$(git-doctor analyze --json | jq '.healthScore')
    if [ "$SCORE" -lt 60 ]; then
      echo "Health score too low: $SCORE"
      exit 1
    fi

GitLab CI

yaml
health-check:
  script:
    - npm install -g git-doctor
    - git-doctor analyze --json > report.json
    - |
      SCORE=$(cat report.json | jq '.healthScore')
      if [ "$SCORE" -lt 60 ]; then
        exit 1
      fi
  artifacts:
    paths:
      - report.json

Jenkins

groovy
stage('Health Check') {
  steps {
    sh 'npm install -g git-doctor'
    sh 'git-doctor analyze --json > report.json'
    script {
      def report = readJSON file: 'report.json'
      if (report.healthScore < 60) {
        error "Health score too low: ${report.healthScore}"
      }
    }
  }
}

常用组合

bash
# 快速检查
git-doctor analyze --quiet

# 生成周报
git-doctor export -f html -o weekly-report.html

# CI 质量门禁
git-doctor analyze --json | jq -e '.healthScore >= 60'

# 增量分析
git-doctor hotspots --days 7

# 提交前检查
git-doctor security && git commit -m "..."

基于 MIT 许可发布