Skip to content

JohnStrunk/commit-drafter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Commit Drafter Git Hook

This repository contains a prepare-commit-msg Git hook script that automatically drafts commit messages using an OpenAI-compatible language model (LLM). The script analyzes staged changes and generates a concise, well-structured commit message following best practices.

Features

  • Generates a commit message with:
    • A 1-line summary in the imperative mood (max 50 characters).
    • A detailed body explaining the changes, reasons, and context.
    • Proper formatting with line wrapping at 75 characters.
  • Supports testing mode to generate a draft for an existing commit.
  • Includes token usage information from the LLM in the draft message.

Installation

  1. Copy the prepare-commit-msg script to your repository's .git/hooks/ directory:

    cp prepare-commit-msg /path/to/your/repo/.git/hooks/
  2. Make the script executable:

    chmod +x /path/to/your/repo/.git/hooks/prepare-commit-msg

Environment Variables

The script requires the following environment variables to be set:

  • LLM_API_ENDPOINT: The base URL of the OpenAI-compatible API (e.g., https://api.model.com/v1).
  • LLM_API_KEY: The API key used as a bearer token for authentication.
  • LLM_MODEL: The name of the model to use (e.g., gpt-3.5-turbo).

Usage

Normal Mode

When committing changes, the script will automatically:

  1. Analyze the staged changes.
  2. Generate a draft commit message.
  3. Prepend the draft to the existing commit message file.

Testing Mode

To test the script with an existing commit, use the -t option followed by the commit hash:

prepare-commit-msg -t <commit-hash>

This will:

  1. Retrieve the original commit message and diff for the specified commit.
  2. Generate a new draft commit message.
  3. Print both the original and the draft commit messages to the console.

Notes

  • If the required environment variables are not set, the script will log an error but will not block the commit process.
  • The script uses only Python's standard library, ensuring minimal dependencies.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Draft git commit messages automatically via an LLM

Topics

Resources

License

Stars

Watchers

Forks

Languages