Skip to content

Instantly share code, notes, and snippets.

@vancura
Last active November 19, 2023 11:48
Show Gist options
  • Save vancura/14b280b158b181fdf60aaa07b1ea7d0b to your computer and use it in GitHub Desktop.
Save vancura/14b280b158b181fdf60aaa07b1ea7d0b to your computer and use it in GitHub Desktop.
This script summarizes staged Git changes, utilizes GPT-4 to generate a Git commit message, and then copies the results to the clipboard.
import subprocess
import clipboard
from openai import OpenAI
client = OpenAI(api_key = "your-openai-key")
# This command returns the staged changes as a patch
git_diff_command = ['git', 'diff', '--cached']
result = subprocess.run(git_diff_command, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.DEVNULL)
# Generate a summary.
prompt_messages = [
{
"role": "system",
"content": """
As a skilled git mergemaster, you possess the expertise to craft clear and comprehensible git commit messages. Kindly generate a git commit message based
on the provided git patch. Use verbosity judiciously. Please generate two text snippets, separated with a blank line, following these rules:
- Capitalized, short (50 chars or less) summary
- More detailed explanatory text, if necessary.
- In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from
the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together.
- Always leave the second line blank.
- Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by
commands like git merge and git revert.
- Further paragraphs come after blank lines.
- Bullet points are okay, too, but make sure every line ends with a period.
- Typically a hyphen or asterisk is used for the bullet, preceded by a single space, with blank lines in between, but conventions vary here.
- Use a hanging indent.
- When referencing a class, always avoid using the file extension suffix (e.g., '.cs') since it is evident that it is a class. The filenames themselves
are not significant. Instead, enclose the mentioned class within backticks (`) to create a Markdown code snippet.
- When using a method, or field name, always remember to enclose it in ` / ` (backticks) to create a Markdown code snippet.
- If there's a mention of a method, remember to suffix its name with the `()`, as how it's done in C#.
- Please exclude files with the .meta extension, as they are Unity helper files and can be disregarded in the commit message.
"""
},
{
"role": "user",
"content": format(result.stdout)
},
]
response = client.chat.completions.create(
# model="gpt-3.5-turbo-16k",
model="gpt-4-1106-preview",
messages=prompt_messages,
temperature=0.5,
max_tokens=256,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0
)
commit_message = response.choices[0].message.content.strip()
print(commit_message)
# Copy the commit message to clipboard
clipboard.copy(commit_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment