Skip to content

AI-powered git commit message generator that analyzes staged changes.

Notifications You must be signed in to change notification settings

CJHwong/rs-git-msg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rs-git-msg

An AI-powered git commit message generator written in Rust.

Features

  • Automatically generates commit messages based on staged changes in your repository
  • Follows the Conventional Commits format
  • Supports multiple AI providers:
    • Ollama (local inference)
    • OpenAI API (GPT models)
    • Gemini
  • Customizable with different models and parameters
  • Generate multiple message options
  • Add specific instructions to guide message generation
  • Integrates with lazygit for a seamless workflow

Installation

Using pre-built binaries (quickest)

You can download and install pre-built binaries directly from GitHub Releases:

# Linux (amd64)
curl -L https://github.com/CJHwong/rs-git-msg/releases/latest/download/rs-git-msg-linux-amd64 -o /usr/local/bin/rs-git-msg && chmod +x /usr/local/bin/rs-git-msg

# macOS (amd64)
curl -L https://github.com/CJHwong/rs-git-msg/releases/latest/download/rs-git-msg-macos-amd64 -o /usr/local/bin/rs-git-msg && chmod +x /usr/local/bin/rs-git-msg

Using the install script (recommended)

The easiest way to install rs-git-msg is using the provided install script:

# Clone the repository
git clone https://github.com/CJHwong/rs-git-msg.git
cd rs-git-msg

# Run the install script
./scripts/install.sh

The script will:

  • Build the binary with optimizations
  • Install it to an appropriate location in your PATH
  • Set up necessary environment configurations if needed

From source (manual)

  1. Clone the repository

  2. Build with Cargo:

    cargo build --release
  3. Move the built executable to a location in your PATH:

    cp target/release/rs-git-msg ~/.local/bin/
    # or
    sudo cp target/release/rs-git-msg /usr/local/bin/

Uninstallation

Using the uninstall script

To remove rs-git-msg from your system, you can use the uninstall script:

./scripts/uninstall.sh

This script will:

  • Remove the rs-git-msg binary from standard installation locations
  • Clean up any configuration files created during use

Manual uninstallation

To manually uninstall, simply remove the binary from where you installed it:

# If installed to ~/.local/bin
rm ~/.local/bin/rs-git-msg

# Or if installed to /usr/local/bin
sudo rm /usr/local/bin/rs-git-msg

# Optionally remove config files
rm -rf ~/.config/rs-git-msg

Usage

Basic usage:

# Stage some changes first
git add .

# Generate a commit message
rs-git-msg

Options

Usage: rs-git-msg [OPTIONS]

Options:
  -n, --number <NUMBERS>    Number of commit messages to generate (1-5) [default: 1]
  -i, --instructions <INSTRUCTIONS>
                            Additional context or instructions for the AI
  -v, --verbose             Enable verbose output
  -p, --provider <PROVIDER> AI provider to use [default: ollama] [possible values: ollama, openai, gemini]
  -m, --model <MODEL>       Model name to use [default: qwen2.5-coder]
  -k, --api-key <API_KEY>   API key for the provider (not needed for Ollama)
  -u, --api-url <API_URL>   API base URL (defaults to provider's standard URL)
  -h, --help                Print help
  -V, --version             Print version

Examples

# Using Ollama with a different model
rs-git-msg -m llama3

# Generate 3 message options
rs-git-msg -n 3

# Using OpenAI's GPT-3.5 Turbo
rs-git-msg -p openai -m gpt-3.5-turbo -k your_api_key_here

# Using Gemini
rs-git-msg -p gemini -m gemini-2.0-flash -k your_api_key_here

# Enable verbose output for debugging
rs-git-msg -v

Lazygit Integration

You can integrate rs-git-msg with lazygit for an even smoother workflow:

Lazygit Integration Demo

  1. Run the setup script:

    ./scripts/setup-lazygit.sh
  2. In lazygit, you can now use the ctrl + g (or cmd + g on macOS) key in the files view to generate a commit message automatically.

The command will:

  • Generate a commit message using rs-git-msg
  • Automatically populate the commit message field
  • Use your configured AI provider and settings

Manual lazygit Setup

If you prefer to manually configure lazygit without running the setup script:

  1. Locate your lazygit config file:

    • macOS: ~/Library/Application Support/lazygit/config.yml
    • Linux/Others: ~/.config/lazygit/config.yml
  2. Add the following configuration to your config.yml:

    customCommands:
      - key: <c-g>
        prompts:
          - type: input
            title: Additional Instructions (optional)
            key: Instructions
            initialValue: ""
          - type: menuFromCommand
            title: AI Commit Messages
            key: Msg
            command: 'rs-git-msg -n 5 {{if .Form.Instructions}}-i "{{.Form.Instructions}}"{{end}}'
        command: git commit -m "{{.Form.Msg}}"
        context: 'files'
        description: 'Generate commit message using rs-git-msg'
        loadingText: 'Generating commit messages...'
        stream: false
  3. Save the file and lazygit should now have the new keybinding.

Environment Variables

  • RS_GIT_MSG_API_KEY: Set your API key for OpenAI or Gemini

AI Provider Setup

Ollama (Default)

  1. Install Ollama
  2. Pull the desired model: ollama pull qwen2.5-coder (or another model of your choice)
  3. Run rs-git-msg (no API key needed)

OpenAI

  1. Create an account at OpenAI
  2. Generate an API key
  3. Run rs-git-msg with -p openai -k your_api_key

Gemini

To use the Gemini provider, you need to:

  1. Obtain an API key from Google AI Studio.
  2. Set the RS_GIT_MSG_API_KEY environment variable with your Gemini API key.
  3. Use the --provider gemini flag when running rs-git-msg.

Example:

export RS_GIT_MSG_API_KEY="YOUR_GEMINI_API_KEY"
rs-git-msg --provider gemini

You can also specify the model and API URL if needed:

rs-git-msg --provider gemini --model gemini-2.0-flash --api-url https://generativelanguage.googleapis.com

Development Guide

Git Hooks

This project uses cargo-husky to manage Git hooks. When you clone the repository and run any Cargo command, the Git hooks will be automatically set up.

The pre-commit hook will:

  1. Format your code with cargo fmt
  2. Run tests with cargo test
  3. Run linting checks with cargo clippy
  4. Verify the code compiles with cargo check

If any of these steps fail, the commit will be prevented.

Testing Guide

This project has a comprehensive test suite. Here's how to run and work with the tests:

Running Tests

To run all tests in the project:

cargo test

To run tests with output (including println statements):

cargo test -- --nocapture

To run a specific test:

cargo test test_name

To run tests in a specific module:

cargo test module_name

Test Coverage

To check test coverage, you can use tools like cargo-tarpaulin:

# Install tarpaulin
cargo install cargo-tarpaulin

# Generate coverage report
cargo tarpaulin --out Html

Mock Provider

For testing without a real AI provider, the project includes a MockProvider implementation:

use crate::ai::mock::MockProvider;

#[test]
fn test_with_mock() {
    // Create a mock provider with a predefined response
    let mock_provider = MockProvider::new("feat(test): add new feature");
    
    // Use the mock provider
    // ...
    
    // Check what prompts were sent to the mock
    let calls = mock_provider.get_calls();
    assert_eq!(calls[0], "expected prompt");
}

Writing Tests

When adding new features, please follow these guidelines for tests:

  1. Unit Tests: Place them in the same file as the code they test, within a mod tests block
  2. Mock External Services: Always use mocks for external services like API calls
  3. Test Edge Cases: Include tests for error conditions and edge cases
  4. Test Public API: Ensure all public functions and methods have tests

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

When contributing, please:

  1. Add tests for any new features
  2. Ensure all tests pass with cargo test
  3. Run cargo fmt for consistent code formatting
  4. Run cargo clippy to catch common issues

About

AI-powered git commit message generator that analyzes staged changes.

Resources

Stars

Watchers

Forks

Packages

No packages published