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
.envfile 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=3Run 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.tsYou 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
.bashrcfile located in your home directory:
nano ~/.bashrc- Add the following line to the end of the file, replacing
/path/to/your-repo/main.tswith the actual path to themain.tsfile 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
.bashrcfile by running:
source ~/.bashrcNow you can use the commitgen command from any directory to generate a commit message based on the current diff:
commitgenIf you want to specify a directory where the Git repository is located, you can still use the -d option:
commitgen -d /path/to/your/repoYou 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.tsThis 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
.bashrcfile:
export OPENAI_API_KEY=yout_openai_api_key_here
export MODEL_VERSION=gpt-4
export NUM_COMMIT_MESSAGES=3Now you can use the commitgen command from any directory to generate a commit message based on the current diff:
commitgenIf you want to specify a directory where the Git repository is located, you can still use the -d option:
commitgen -d /path/to/your/repoThis code is licensed under the MIT License.
