Home Using GPT4 to generate git logs for OpenSource projects in the style of conventional commits via a terminal
Post
Cancel

Using GPT4 to generate git logs for OpenSource projects in the style of conventional commits via a terminal

Git commit logs can be tedious to write… but are useful for long term maintenance and code audit.

<img data-src="https://xkcd.com/1296/">

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'

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.

Terminal recording

This post is licensed under CC BY 4.0 by the author.