My Python script can write better commit messages than you ;) Just kidding, but they are pretty good.
Table of Contents
We can run the script when the current working directory contains a git repository, and there are staged commits. The script will generate a commit message based on the staged commits and commit them.
Here is the script:
import subprocess
import openai
def run_command(command):
process = subprocess.run(command, shell=True, capture_output=True, text=True)
if process.returncode != 0:
raise Exception(f'Command {command} failed with exit code {process.returncode}')
return process.stdout
def check_if_commits_are_staged():
try:
result = run_command('git diff --staged')
if result == '':
return False
except Exception:
return False
return True
def generate_commit_message_from_diff(diff):
prompt = f"""Given the following git patch file:
{diff}
###
Generate a one-sentence long git commit message.
Return only the commit message without comments or other text.
"""
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt, temperature=0,
max_tokens=128)
message = response['choices'][0]['text']
return message.strip().replace('"', '').replace("\n", '')
if __name__ == '__main__':
if not check_if_commits_are_staged():
print('No staged commits')
exit(0)
diff = run_command('git diff --staged')
commit_message = generate_commit_message_from_diff(diff)
run_command(f'git commit -m "{commit_message}"')
print(f'Committed with message: {commit_message}')
Usage
Before we run the script, we need to install the OpenAI Python library and set the API key environment variable.
When we finish the setup, we can modify files in the project directory, stage them for commit and run the script.
Example:
$ python aicommit.py
Committed with message: Update aicommit.py to return the text of the first choice in the response.
In the git log
, we see the commit message generated by the script:
commit f1f7da697aa3e7de634d598a4cc43695ad4d7fca (HEAD -> main)
Author: Bartosz Mikulski <xxx>
Date: Tue Feb 14 19:25:35 2023 +0100
Update aicommit.py to return the text of the first choice in the response.
Want to build AI systems that actually work?
Download my expert-crafted GenAI Transformation Guide for Data Teams and discover how to properly measure AI performance, set up guardrails, and continuously improve your AI solutions like the pros.
How does it work?
The script uses the OpenAI API to generate a commit message based on the staged commits. We use the git diff --staged
command to get the staged commits and pass them to the OpenAI API.
If the command returns an empty response, we know there are no staged commits, and we can exit the script. It will also fail if the current working directory is not a git repository.
In the next step, we get the git patch file from the git diff --staged
command and pass it to the OpenAI API.
In the prompt, I instructed it to generate a one-sentence long commit without adding any comments. It’s crucial to add the “Return only the commit message without comments or other text.” instruction to the prompt. Otherwise, it tends to start the response with “Here is the commit message,” “For the given changes we can generate the following commit message,” or “The commit message is.” We don’t want it in our commit messages.
We don’t want double quotes or new lines in the message, either. However, removing them in the Python script is easier than asking GPT-3 not to generate them.
After getting the commit message, we pass it as the message argument to the git commit
command.