Skip to content

theburrowhub/ai-commit

Repository files navigation

ai-commit

ai-commit is a command-line tool designed to help developers automatically generate meaningful commit messages for their Git repositories using AI. It uses the changes in the working directory to infer a commit message, promoting the use of conventional commits while keeping the messages precise and understandable.

ai-commit

Features

  • Generates commit messages using AI based on Git diffs.
  • Adheres to conventional commit standards (e.g., feat, fix, refactor).
  • Supports multiple logging levels (debug, info, warn, error).
  • Option to run in "noop" mode to see the generated commit message without making changes.

Requirements

  • AI model server running at specified URL (Ollama server).
  • A AI Model to generate commit messages. By default, it uses the "mistral" model.
    ollama pull mistral
  • Git installed and configured on the system.

Installation

Run the following command to install the ai-commit binary and git extension:

curl -s https://raw.githubusercontent.com/theburrowhub/ai-commit/main/scripts/get-ai-commit.sh | sudo bash

Clone and Build

Clone this repository and build the ai-commit binary:

git clone https://github.com/theburrowhub/ai-commit.git
cd ai-commit
make install

Usage

The ai-commit command will use AI to generate a commit message based on the current state of your working directory.

Basic Usage

Run the following command in your Git repository:

ai-commit

or

git ai-commit

These commands will:

  • Gather the current diffs in your working directory.
  • Send the diffs to the configured AI model to generate a commit message.
  • Commit the changes with the generated message.
  • Open your default editor to allow you to modify the commit message before finalizing the commit.

Docker

Run the next command to run over docker:

  • Linux:

    docker run --network host -it --rm \
           -v ${HOME}/.config:/home/ai-commit/.config \
           -v $(pwd):/source \
           -e GIT_USER_NAME="$(git config --global user.name)" \
           -e GIT_USER_EMAIL="$(git config --global user.email)" \
           ai-commit:latest
  • MacOS:

    docker run -it --rm \
           -v ${HOME}/.config:/home/ai-commit/.config \
           -v $(pwd):/source \
           -e GIT_USER_NAME="$(git config --global user.name)" \
           -e GIT_USER_EMAIL="$(git config --global user.email)" \
           ai-commit:latest --server http://host.docker.internal:11434

Two volumes are mounted:

  • ${HOME}/.config:/home/ai-commit/.config: To store the configuration file.
  • $(pwd):/source: To access the source code.

Environment variables are set to configure the git user:

  • -e GIT_USER_NAME="$(git config --global user.name)": To set the git user name.
  • -e GIT_USER_EMAIL="$(git config --global user.email)": To set the git user email.

The -it flag is used to run the container in interactive mode to allow using vim to edit the commit message just generated. And the --rm flag is used to remove the container after it stops. --network host is used to allow the container to access the host network and connect to the Ollama server.

Any additional arguments passed to the ai-commit command will be forwarded to the Docker container. For example, to run the command in noop mode --noop can be passed as an argument:

To make it easier you can create an alias in your .bashrc or .zshrc file:

alias ai-commit='docker run --network host -it --rm \
       -v ${HOME}/.config:/home/ai-commit/.config \
       -v $(pwd):/source \
       -e GIT_USER_NAME="$(git config --global user.name)" \
       -e GIT_USER_EMAIL="$(git config --global user.name)" \
       ai-commit:latest'

Command-Line Flags

  • --noop: Run without making any changes to the Git repository (dry-run mode).
  • --logLevel: Set the logging level (default is "info"). Available levels are "debug", "info", "warn", and "error".
  • --server: Set the Ollama server URL (default is http://localhost:11434).
  • --model: Specify the AI model to use for generating the commit message (default is "mistral").
  • --retries, -r: Number of retries for invalid commit messages (default is 3).
  • --quiet: Run in silent mode (no logs). Only errors are printed to stderr.
  • --commitType, -t: Select a commit type by writting the type next to the flag (feat, fix, refactor, docs, style...)

Example

ai-commit --noop --logLevel debug

This will run the command in noop mode, providing detailed logs about the process without actually committing anything.

Configuration

The configuration file $(HOME)/.config/ai-commit/config.yaml is automatically generated if it does not exist. This file contains the default values for the ai-commit configuration. Below is an example of the content of this file with the default values:

noop: false
logLevel: info
ollamaServer: http://localhost:11434
model: mistral
systemPrompt: |-
  In an impersonal way, write a commit message that explains what the commit
  is for. Use conventional commits and the imperative mood in the first line.
  The first line should start with: feat, fix, refactor, docs, style, build,
  perf, ci, style, test or chore. Set the file name and the changes made in
  the body. Only one subject line is allowed.

  An example of commit message is:

  feat(file or class): Add user authentication

  - Implement user sign-up and login functionality
  - Add password hashing for security
  - Integrate with authentication API

  Add line breaks to separate subject from body.
retriesCommitMessage: 3
numCtx: 4096
numKeep: 512
temperature: 0

This file defines the following configuration parameters:

  • logLevel: Logging level (default is "info").
  • ollamaServer: URL of the Ollama server (default is http://localhost:11434).
  • model: AI model to use for generating commit messages (default is "mistral").
  • systemPrompt: Default template for the commit message.
  • retriesCommitMessage: Number of retries for invalid commit messages (default is 3).
  • numCtx: Number of context tokens (default is 4096).
  • numKeep: Number of tokens to keep in case of truncation (default is 512).
  • temperature: Temperature for sampling (default is 0 - deterministic).

Commit Message Generation

The generated commit message will adhere to the following format:

  • A concise subject line following the conventional commit format.
  • A detailed body explaining the changes made, if applicable.

Dependencies

  • AI communication and inference managed via internal package ai.
  • Git operations performed using the internal package git.
  • Logging provided via the internal package logger.

Development

To execute the command during development:

go run ./cmd/main.go

Requirements

  • Go installed.
  • Cobra library for the CLI.
  • go-git library for interacting with Git repositories.

Makefile Targets

The Makefile provides the following targets to manage building, installing, and cleaning up:

  • bin: Compiles the ai-commit binary and places it in the ./bin directory.
    make bin
  • install: Builds the binary and copies it to /usr/local/bin for system-wide usage.
    make install
  • uninstall: Removes the installed ai-commit binary from /usr/local/bin.
    make uninstall
  • release: Make a release using goreleaser.
    make release
  • test-release: Make a test release using goreleaser.
    make test-release
  • bump: Bump the version of the project.
    make bump
  • bump-alpha: Bump version using commitizen (alpha)
    make bump-alpha
  • bump-beta: Bump version using commitizen (beta)
    make bump-beta
  • bump-rc: Bump version using commitizen (release candidate)
    make bump-rc
  • clean: Removes the ./bin directory to clean up build artifacts.
    make clean

Automatic Versioning

Just push a new tag with the version number and the CI/CD pipeline will take care of the rest.

If you want to manually bump the version:

cz bump --increment (MAJOR|MINOR|PATCH)

Then push the tag to the repository:

git push --tags

License

MIT

Contributing

Feel free to submit pull requests or open issues to improve this project.

About

Commits based on Ollama

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors