Simple git commit auto fill

Back in the days when I worked for RedHat I remember having a neat autocomplete git hook that filled in my git commit message. I liked the way it was formatted and I remember it being something similar to this.

* /Path/To/Changed/File0 (Method0):
* /Path/To/Changed/File1 (Method0):

Here I try to reproduce this effect by having a very simple script that uses the git diff. Here is the script:

git diff HEAD | \
grep -e '^@@.*@@.*(.' -e '^@@.*@@.*.struct.*{' -e '^diff ..git' | \ 
sed 's/^@@.*@@ /@@ /' | \
sed 'sed 's/ \(int\|double\) / /
sed 's/(./ /' | \
sed 's/^@@.*@@.*struct.*{/@@@ struct/' | uniq | \
awk -F " " '/^diff --git/{gsub(/^./,"",$3);print "#*"$3" :"} /^@@@ struct/{gsub(/{/,"", $3);print "#("$3") :"} /^@@ /{print "#("$2") :"}'

The `grep` line selects the relevant git diff lines. The `sed` lines remote the text that goes around the lines containing the “@@” string. And finally the awk line formats three types of lines: 1. the file change, 2. the method change, and 3. the struct change.

This script has some shortcomings:

  1. New methods, structs do not show up
  2. Sometimes the diff will have a method, struct name that is not actually changed.

Still I’ll make this post as a reminder to myself and a place where I can start next time I want to “give this a shot”


About joelgranados

I'm fascinated with how technology and science impact our reality and am drawn to leverage them in order to increase the potential of human activity.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s