Skip to content
forked from mrgoonie/cmai

A git hooks that automatically generates messages for commits and release notes using AI based on your staged git changes.

Notifications You must be signed in to change notification settings

partcad/pre-commit

Β 
Β 

Repository files navigation

GitHub Release Conventional Commits Keep a Changelog v1.1.0 badge Apache 2.0 License Badge

pre-commit - Git Hooks backed by AI

Fed up with crafting commit messages by hand?

Git Commit

Attribution

Related Projects

Prerequisites

  • git installed and configured
  • pre-commit installed and configured
  • jq installed
    • Used for escaping JSON
  • An OpenRouter API key
  • curl installed

Configuration

Export API key for OpenRouter

Option 1: Store in .env file (recommended):

echo "OPENROUTER_API_KEY=your_key_here" > .env
source .env

Option 2: Use credential storage (alternative)

export OPENROUTER_API_KEY="$(pass show openrouter/api_key)"

prepare-commit-msg

Welcome to prepare-commit-msg, a git hook that automatically generates conventional commit messages using AI based on your staged git changes. Your commit messages will look like this:

VS Code Preview 1

VS Code Preview 2

GitHub PR Preview

Features

  • πŸ€– AI-powered commit message generation (using google/gemini-flash-1.5-8b - SUPER CHEAP!)
    • Around $0.00001/commit -> $1 per 100K commit messages!
  • πŸ“ Follows Conventional Commits format
  • πŸ› Debug mode for troubleshooting

Installation

  1. Install pre-commit.
  2. Add hook configuration to .pre-commit-config.yaml.
    # See https://pre-commit.com for more information
    # See https://pre-commit.com/hooks.html for more hooks
    repos:
      - repo: https://github.com/partcad/pre-commit
        rev: v1.1.0
        hooks:
          - id: prepare-commit-msg
            verbose: true

By default, only filenames collected using git diff --cached --name-status will be sent to OpenRouter. If you want to share commit diffs using git diff --cached with OpenRouter and get more detailed commit messages, you can use the --open-source option.

If you set custom args you will have to provide --commit-msg-filename as last argument.

- id: prepare-commit-msg
  args: [
     "--debug",
     "--open-source",
     "--commit-msg-filename",
]

keep-a-changelog

Features

  • πŸ€– AI-powered commit message generation (using google/gemini-flash-1.5-8b - SUPER CHEAP!)
    • Around $0.00001/commit -> $1 per 100K commit messages!
  • πŸ“ Follows Keep a Changelog format
  • πŸ› Debug mode for troubleshooting

Installation

  1. Install pre-commit.
  2. Add hook configuration to .pre-commit-config.yaml.
    # See https://pre-commit.com for more information
    # See https://pre-commit.com/hooks.html for more hooks
    repos:
      - repo: https://github.com/partcad/pre-commit
        rev: v1.1.0
        hooks:
          - id: keep-a-changelog
            verbose: true

API Security

  • API key is stored in environment variable or secure credential storage
  • No data is persisted except the API key in your secure storage
  • All communication is done via HTTPS

Data Privacy

  • By default, only file names are sent to OpenRouter
  • When using --open-source, complete diffs are sent
  • Consider reviewing changes before staging to avoid leaking sensitive data

Security and Privacy Considerations for --open-source Option

When you use the --open-source option, the complete diffs of your staged changes are sent to the OpenRouter API to generate more detailed commit messages. This means that the actual content of your code changes β€” including additions, deletions, and modifications β€” is transmitted over the network. While OpenRouter uses secure HTTPS connections to protect data in transit, sharing full diffs can potentially expose sensitive code, proprietary algorithms, or confidential information. It's important to ensure that your code does not contain any sensitive or private data before using this option. By default, only filenames collected using git diff --cached --name-status are sent, minimizing the amount of information shared.

Rate Limiting & Costs

  • API is rate limited to prevent abuse
  • Costs approximately $0.00001 per commit
  • Large diffs may incur higher costs

Uninstallation

Development

Consider reading at least following docs for pre-commit:

You can use following snippet for local testing:

# Stage some changes
git add .

# Trigger hook
pre-commit try-repo \
   --verbose \
   --hook-stage=prepare-commit-msg \
   --commit-msg-filename=$(mktemp) \
   . \
   prepare-commit-msg \
   --verbose \
   --all-files \

...but it's easier just work in the fork directly and let .pre-commit-hooks.yaml do the magic for you.

Licenses

Join The Project Team

Join #pre-commit channel on Discord

Roadmap

  • Integrate with 1Password.
  • Allow truncate data which is sent to OpenRouter.
  • Cross-platform support (Windows, Linux, macOS)
  • Allow direct usage of LLMs.
    • Add Ollama integration.

Keep a Changelog

  • Move to JSON format.
  • Use JSON schema for response validation.
  • Send only current version number of the changelog.
    • Let LLM decide which version part should be updated (major, minor, patch).

Conventional Commits

  • Move to JSON format.
  • Format message to wrap at 50 characters.

Done

  • Create dedicated hook for updating CHANGELOG.md
  • Add GitHub stars chart.
  • Allow override user prompt.
  • Allow override system prompt.

Star History

Star History Chart

About

A git hooks that automatically generates messages for commits and release notes using AI based on your staged git changes.

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 53.8%
  • Python 46.2%