Git commit logs can be tedious to write… but are useful for long term maintenance and code audit.
They are especially useful for triaging production issues after a release e.g. a menu component has stopped working, and a commit message points exactly to that change.
Turns out that GPT4 is fantastic for generating git logs from git diffs, making the process a breeze.
The following shell script gets staged code and generates a diff and calls the GTP4 api to write a git log:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
diff_content="Write a commit message in the style of conventional commits specification, using bullet points, for the following: \n $(git --no-pager diff --cached)"
echo $diff_content
payload=$(jq -nc --arg content "$diff_content" '{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are ChatGPT, a large language model trained by OpenAI. Carefully heed the users instructions."},
{
"role": "user",
"content": $content
}
],
"temperature": 1
}')
# Pass the payload to the OpenAI API chat completions endpoint
response=$(curl -s -X POST https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
--data "$payload")
echo $response | jq -r '.choices[0].message.content'
or for LLAMA3 via groq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
diff_content="Write a commit message in the style of conventional commits specification, using bullet points, for the following: \n $(git --no-pager diff --cached)"
echo $diff_content
payload=$(jq -nc --arg content "$diff_content" '{
"model": "llama3-70b-8192",
"messages": [
{"role": "system", "content": "You are ChatGPT, a large language model trained by OpenAI. Carefully heed the users instructions"},
{
"role": "user",
"content": $content
}
],
"temperature": 1
}')
# Pass the payload to the OpenAI API chat completions endpoint
response=$(curl -s -X POST https://api.groq.com/openai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROQ_API_KEY" \
--data "$payload")
echo $response
echo $response | jq -r '.choices[0].message.content'
Save the file somewhere locally, e.g ~/git_diff_to_gpt4.sh
, and set it to executable chmod +x ~/git_diff_to_gpt4.sh
.
Then add to either your .zshrc or .bashrc, an alias and the openai api key as an environmental variable, i.e.
1
2
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
alias gdiffgpt4='~/git_diff_to_gpt4.sh'
Then, either restart the shell or reload it (e.g. ‘source ~/.zshrc’), stage your code and run the shell alias i.e.
1
2
git add .
gdiffgpt4
Note, this sends your git diff to OpenAI, and should not used for proprietary codebases without permission. It should be possible to modify this code to us a locally hosted LLM such as LLAMA2 or CodeLLAMA.