This script generates a Git commit message based on a diff using OpenAI's GPT-4. It uses Deno for running the script.
- Deno installed
- OpenAI API key
- Clone this repository:
git clone https://github.com/Flviuus/git-commit-generator
cd git-commit-generator
- Create a
.env
file in the root directory of the project and add your OpenAI API key and configuration:
OPENAI_API_KEY=your_openai_api_key_here
MODEL_VERSION=gpt-4
NUM_COMMIT_MESSAGES=3
Run the script in a Git repository to generate a commit message based on the current diff:
deno run --allow-net --allow-read --allow-run --allow-env main.ts
You can also specify a directory where the Git repository is located:
deno run --allow-net --allow-read --allow-run --allow-env main.ts -d /path/to/your/repo
-h, --help
: Show the help message and exit-d, --dir
: Directory where to perform the diff (default: current directory)--debug
: Show debug information
To run the script globally on your system, you can add an alias in your .bashrc
file.
- Open your
.bashrc
file located in your home directory:
nano ~/.bashrc
- Add the following line to the end of the file, replacing
/path/to/your-repo/main.ts
with the actual path to themain.ts
file in your repository:
alias commitgen='deno run --allow-net --allow-read --allow-run --allow-env /path/to/your-repo/main.ts'
- Add the following environment variables to the file:
export OPENAI_API_KEY=yout_openai_api_key_here
export MODEL_VERSION=gpt-4
export NUM_COMMIT_MESSAGES=3
-
Save the file and exit the editor.
-
Reload your
.bashrc
file by running:
source ~/.bashrc
Now you can use the commitgen
command from any directory to generate a commit message based on the current diff:
commitgen
If you want to specify a directory where the Git repository is located, you can still use the -d
option:
commitgen -d /path/to/your/repo
You can also compile the Deno script into a standalone executable binary, so you don't need to run it through the Deno runtime.
- Compile the script:
deno compile --allow-net --allow-read --allow-run --allow-env -o commitgen main.ts
This will create a binary called commitgen
.
- Move the binary to a directory in your
$PATH
, like/usr/local/bin
:
sudo mv commitgen /usr/local/bin/
- Make sure the binary is executable:
sudo chmod +x /usr/local/bin/commitgen
- Add the following environment variables to your
.bashrc
file:
export OPENAI_API_KEY=yout_openai_api_key_here
export MODEL_VERSION=gpt-4
export NUM_COMMIT_MESSAGES=3
Now you can use the commitgen
command from any directory to generate a commit message based on the current diff:
commitgen
If you want to specify a directory where the Git repository is located, you can still use the -d
option:
commitgen -d /path/to/your/repo
This code is licensed under the MIT License.