- 🤖 Introduction
- ⚙️ Dependencies installation for Mac Os
- ⚙️ Dependencies installation for Linux
- ⚙️ Dependencies installation for Windows
- 🦾 Project setup
⚠️ Warning: This project is primarily configured for macOS. However, configurations have been adapted for Linux and Windows. If you encounter any issues or errors, feel free to report them in the issues section or submit a pull request.
This project provides an automated Git hook that improves every commit message using Gemini AI. It automatically updates your commit messages before they are applied to the repository. The script requires a Gemini API key, which you need to store in a .env
file as an environment variable named GEMINI_API_KEY
.
To automatically set up pyhton env and dependencies run this script:
chmod +x dependencies-macos.sh
./dependencies-macos.sh
Now you can go 👉 (🦾 Project setup)
This guide walks you through the manual steps required to set up a Python environment on macOS using pyenv
, install Python version 3.12.5, create a virtual environment, and install the google-generativeai
package.
Make sure you have the following installed:
- Homebrew (If not, follow the instructions in Step 1)
- pyenv
- Install Homebrew (if not installed)
Homebrew is required to install pyenv. To check if Homebrew is installed, open a terminal and run:
brew --version
If Homebrew is not installed, you can install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install pyenv
Check if pyenv is installed by running:
pyenv --version
If pyenv is not installed, install it using Homebrew:
brew update
brew install pyenv
- Configure pyenv
To ensure pyenv works properly, you need to add it to your shell’s environment variables. Add the following lines to your ~/.bash_profile (or ~/.zshrc if you’re using Zsh):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
After adding these lines, apply the changes by running:
source ~/.bash_profile # or ~/.zshrc for Zsh users
- Install Python 3.12.5 (current newer version)
Now that pyenv is set up, you can install the specific version of Python (3.12.5 in this case) by running:
pyenv install 3.12.5
- Set Python 3.12.5 as the Global Version
pyenv global 3.12.5
- Create a Virtual Environment
brew install pyenv-virtualenv
With Python 3.12.5 installed, create a virtual environment (in this example, we’ll call it myenv):
pyenv virtualenv 3.12.5 myenv
- Activate the Virtual Environment
pyenv activate myenv
- Install the google-generativeai Package
pip install google-generativeai
pip install python-dotenv
You now have a Python 3.12.5 virtual environment with the google-generativeai package installed. To deactivate the environment at any point, run:
pyenv deactivate
For future use, you can reactivate the environment by running:
pyenv activate myenv
To automatically set up pyhton env and dependencies run this script:
chmod +x dependencies-linux.sh
./dependencies-linux.sh
In the dependency script, if there is an installation error, replace the commented installation commands with those corresponding to the correct Linux distribution you are using.
Now you can go 👉 (🦾 Project setup)
- Visit the official Python website: https://www.python.org/downloads/
- Find and download Python 3.12.5 for Windows.
- Run the installer and make sure to check the box "Add Python 3.12 to PATH" before proceeding with the installation.
Once Python is installed, open a Command Prompt (CMD) window and run the following command to verify the installation:
python --version
You should see output similar to:
Python 3.12.5
In the Command Prompt (CMD) window, navigate to the directory where you want to create your virtual environment. Run the following command to create a virtual environment named env:
python -m venv env
Once the virtual environment is created, activate it with this command:
.\env\Scripts\activate
Step 1: Install google-generativeai With the virtual environment activated, run the following command to install the required package:
pip install google-generativeai
This will install the necessary library in your virtual environment.
Now you can go 👉 (🦾 Project setup)
To use this hook, follow the steps below.
- You need to have Python installed on your system. (See ⚙️ Dependencies installation)
- You must have an API key for Gemini AI. Store this key in a
.env
file located in the root directory of the project:GEMINI_API_KEY=your_gemini_api_key_here
- Also you have the option to send the diff code to gemini, by default is false, you can change it
USE_GIT_DIFF = "true"
To automatically set up the hook for your Git repositories, clone the project and run the following commands:
chmod +x setup_hooks.py
./setup_hooks.py
The script will ask you where your Git projects are located. It will then configure the Git hooks for all the projects in that directory. Any new repositories you clone will have the hook applied automatically.
If you prefer to set up the hook manually, follow these steps:
- Create a Global Hooks Directory:
mkdir -p ~/.git-templates/hooks
- Configure Git to Use the Global Hooks Template:
git config --global init.templateDir '~/.git-templates'
- Create the commit-msg Hook in the Global Hooks Directory:
nano ~/.git-templates/hooks/commit-msg
- Add the Hook Script:
Copy the following script into the commit-msg file. This script will invoke a Python script that uses Gemini AI to enhance your commit messages.
#!/bin/bash
# commit-msg hook script
# Path to your Python script
SCRIPT_PATH="/path/to/Main.py"
# Temporary file that contains the commit message
COMMIT_MSG_FILE="$1"
# Read the message from the temporary file
COMMIT_MSG=$(<"$COMMIT_MSG_FILE")
# Call the Python script with the commit message
MODIFIED_MSG=$(python3 "$SCRIPT_PATH" --message "$COMMIT_MSG" --test)
# If the Python script returned a modified message, replace it in the temporary file
if [ "$COMMIT_MSG" != "$MODIFIED_MSG" ]; then
echo "$MODIFIED_MSG" > "$COMMIT_MSG_FILE"
fi
- Make the Hook Executable:
chmod +x ~/.git-templates/hooks/commit-msg
- Apply the Hook to Existing Repositories:
To apply this hook to existing Git repositories, copy the commit-msg file to the .git/hooks/ directory of each repository:
cp ~/.git-templates/hooks/commit-msg /path/to/your/repository/.git/hooks/
Once the hook is set up, it will automatically enhance your commit messages using Gemini AI every time you commit changes. Simply use Git as you normally would, and the commit messages will be updated before being applied to the repository.
git commit -m "your commit message"
Gemini will process the commit message, improve it, and apply the changes to the commit automatically.