validate the css validate the xhtml

HackerMoJo.com


Ceci n'est pas une blog
by Glenn Franxman, Django Developer / Stunt Programmer.

Harnessing the Power of an LLM for Git Commit Messages

posted: 2023-08-18 14:22:33 perma-link, RSS comments feed

The integration of intelligent systems into developer workflows has always been a topic of great interest. With the evolution of the GPT series and similar language models, there are innumerable opportunities to automate and enhance various aspects of software engineering. One of these is generating commit messages. Have you ever found yourself pausing for a bit too long, thinking about the ideal way to phrase a commit? Enter the Language Model!

In this post, we'll explore an ingenious use of an LLM (Large Language Model) to automatically draft the first version of your git commit messages.

REL_NOTES_RAW=`git diff --staged | llm -s "release notes"  2>/dev/null`
REL_NOTES_RAW=$(echo "$REL_NOTES_RAW" | sed 's/^#/* /')
echo RAW: $REL_NOTES_RAW



#on bsd style systems, like OSX, sed is sentitive to newlines so back to awk:
REL_NOTES_ESCAPED=$(echo "$REL_NOTES_RAW" | awk '{print $0 "\n"}')



# on GNU based systems
#REL_NOTES_ESCAPED=$(echo "$REL_NOTES_RAW" | sed ':a;N;$!ba;s/
/\n/g')



echo ESCAPED: $REL_NOTES_ESCAPED



REL_NOTES=$(echo "$REL_NOTES" | sed 's/^/# /')



# prepend to commit msg file
# if you want the rel notes as comments
#awk -v r="$REL_NOTES" 'NR==1 {print r} {print}' "$COMMIT_MSG_FILE" > tmpfile && mv tmpfile "$COMMIT_MSG_FILE"
# or ready to go:
awk -v r="$REL_NOTES_RAW" 'NR==1 {print r} {print}' "$COMMIT_MSG_FILE" > tmpfile && mv tmpfile "$COMMIT_MSG_FILE"

It took many revisions to get that working. My local version looks much worse, so I asked Chat-GPT to break it down for me and here's what I got:


Of course! Let's break down the provided code snippet step-by-step.

1. Capture Staged Changes for Release Notes:

REL_NOTES_RAW=`git diff --staged | llm -s "release notes"  2>/dev/null`

This command captures the changes you've staged for the next commit and sends them to the llm (which presumably stands for a Large Language Model) for processing. The goal is to get a summarized "release notes" version of the changes. Any errors in the process are discarded with 2>/dev/null.

2. Transform Release Notes Formatting:

REL_NOTES_RAW=$(echo "$REL_NOTES_RAW" | sed 's/^#/* /')
echo RAW: $REL_NOTES_RAW

If the LLM prefixes any lines with #, this command replaces them with bullet points (*). The transformed release notes are then printed to the console.

3. Escape Newlines for Different Systems:
For BSD-style Systems (like OSX):

REL_NOTES_ESCAPED=$(echo "$REL_NOTES_RAW" | awk '{print $0 "\n"}')

On BSD systems, sed is sensitive to newline characters, so we use awk to escape them properly.

For GNU-based Systems:

#REL_NOTES_ESCAPED=$(echo "$REL_NOTES_RAW" | sed ':a;N;$!ba;s/
/\n/g')

On GNU systems, you'd use this sed command to escape newline characters. It's commented out because it's an alternative to the BSD-style command above.

After the appropriate newline escaping, the formatted release notes are printed:

echo ESCAPED: $REL_NOTES_ESCAPED

4. Optional Step to Prefix Each Line with #:

REL_NOTES=$(echo "$REL_NOTES" | sed 's/^/# /')

This is an optional step if you want each line of the release notes to be commented out (perhaps for manual review before finalizing the commit message).

5. Prepend Processed Notes to Commit Message:

If you want the release notes as comments:

#awk -v r="$REL_NOTES" 'NR==1 {print r} {print}' "$COMMIT_MSG_FILE" > tmpfile && mv tmpfile "$COMMIT_MSG_FILE"

This command uses awk to insert the commented-out release notes at the top of the commit message file. It's commented out because you would use one of the two awk commands based on your preference.

If you want the release notes ready to go:

awk -v r="$REL_NOTES_RAW" 'NR==1 {print r} {print}' "$COMMIT_MSG_FILE" > tmpfile && mv tmpfile "$COMMIT_MSG_FILE"

This command inserts the raw release notes (without the # prefix) at the beginning of your commit message file.

In both cases, the changes are written to a temporary file (tmpfile), which then replaces the original commit message file. This ensures that the original file isn't lost if something goes wrong during the process.

Conclusion:

The script captures the diff of your staged changes, processes them with an LLM to get a summarized "release notes" style output, formats this output, and then prepends it to your commit message. This can be particularly handy for automating commit messages based on the content of the changes you've made.


That's amazing. That's exactly the kind of clarity I wanted for my commit messages.

- And once installed in your prepare_commit_msg hook you're good to go.

- No more need to ponder over how to describe the commit. Let the model summarize it for you.

- Get standardized commit messages, especially useful if multiple developers are collaborating.

- The LLM provides clear notes, making it easier to understand the intent and content of the commit.

Automation in software engineering doesn't just pertain to CI/CD pipelines or testing. As demonstrated, with the power of an LLM, even tasks as seemingly trivial as drafting a commit message can be automated, saving time and ensuring consistency.

However, it's worth noting that while LLMs are powerful, they are not infallible. Always review the generated commit messages to ensure they accurately represent your changes.

Happy coding, and may your commit messages always be clear and concise!


Comments

Post a comment


Based upon your reading habits, might I recommend:

Or, you might like:

Copyright © 2003,2004,2005,2006,2007,2008 GFranxman. All Rights Reserved


hosting: slicehost.com. powered by: django. written in: python. controlled by: bzr. monsters by: monsterID.

You've been exposed to: {'Programming': 1}