Skip to content

Phase 4: Pre-commit hook integration for --suggest #145

@Kadajett

Description

@Kadajett

Overview

Add semfora-engine precommit subcommand and hook installation support. Only analyzes changed files for speed.

Depends on: #141 (Phase 2)
Parent research: #135

Implementation

1. Add Precommit subcommand to CLI

In src/cli.rs:

/// Run suggestions on staged changes (for git pre-commit hooks)
Precommit(PrecommitArgs),

#[derive(Args, Debug)]
pub struct PrecommitArgs {
    #[arg(long, default_value = "warn", value_enum)]
    pub mode: PrecommitMode,     // warn or fail
    
    #[arg(long, default_value = "0.7")]
    pub threshold: f64,          // min confidence to trigger fail
    
    #[arg(short, long)]
    pub path: Option<PathBuf>,
}

pub enum PrecommitMode { Warn, Fail }

2. Create src/commands/precommit.rs

  1. Run git diff --cached --name-only to get staged files
  2. Filter to supported languages (reuse Lang::from_extension)
  3. Load index, build SuggestContext scoped to changed files only
  4. Run suggest engine
  5. Format output (always text for terminal)
  6. Exit code: 0 for warn mode; 0 or 1 for fail mode based on threshold

3. Add setup precommit subcommand

semfora-engine setup precommit [--mode warn|fail]

  • Detect hook system: check for .husky/, .pre-commit-config.yaml, lefthook.yml
  • If none found, write .git/hooks/pre-commit directly
  • Generate appropriate config for detected system

4. Pre-commit framework support

Create .pre-commit-hooks.yaml in repo root:

- id: semfora-suggest
  name: semfora suggestions
  entry: semfora-engine precommit
  language: system
  types: [file]
  stages: [pre-commit]

5. Configuration

In semfora.toml:

[suggest.precommit]
mode = "warn"           # warn or fail
threshold = 0.7
fail_categories = ["dead-code", "complexity"]   # only these can block commits
warn_categories = ["naming", "module-org"]       # always warn-only

Testing

  • Test with mock git staged files
  • Test warn mode (always exit 0)
  • Test fail mode with threshold
  • Test category-based fail/warn split
  • Test hook installation for plain git, husky, pre-commit framework
  • Performance test: should complete in <200ms for typical commit (5-20 files)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions