New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
githooks: create commit message template with prepare-commit-msg #44872
Changes from 1 commit
ab7c7df
f951d75
028c3f9
83b0c48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,33 +9,35 @@ | |
# | ||
# To enable this hook, rename this file to "prepare-commit-msg". | ||
|
||
# This hook includes three examples. The first one removes the | ||
# "# Please enter the commit message..." help message. | ||
# | ||
# The second includes the output of "git diff --name-status -r" | ||
# into the message, just before the "git status" output. It is | ||
# commented because it doesn't cope with --amend or with squashed | ||
# commits. | ||
# | ||
# The third example adds a Signed-off-by line to the message, that can | ||
# still be edited. This is rarely a good idea. | ||
|
||
COMMIT_MSG_FILE=$1 | ||
COMMIT_SOURCE=$2 | ||
SHA1=$3 | ||
|
||
|
||
# Removes the "# Please enter the commit message..." help message. | ||
# /usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" | ||
|
||
if [[ -z "$COMMIT_SOURCE" ]] | ||
then | ||
echo "# Please enter the commit message for your changes. " > "$COMMIT_MSG_FILE" | ||
COMMIT_MSG_HEADERS="# Possible commit messages: " | ||
for FILE in $(git diff --cached --name-only -r) | ||
do | ||
# According to files changed the commit message would start "<project_name>: <commit_message>" | ||
# src/<project>/file.txt - "<project>:" | ||
# file.txt - "file.txt:" | ||
# <project>/file.txt - "<project>:" | ||
echo "# $FILE: <commit_message>" | sed -E 's|src/+||g' | sed -r 's/(.*)(\/[a-z0-9.-]+)/\1 /' >> "$COMMIT_MSG_FILE" | ||
COMMIT_MSG_HEADERS+="\n$(echo "# $FILE: <commit_message>" | sed -E 's|src/+||g' | sed -r 's/(.*)(\/[a-z0-9.-]+)/\1 /')" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a couple of files where we map paths to Github teams, and to Github labels. I'm not advocating for a third file mapping paths to components, but... yeah, on a second thought, it makes all the sense, right? These files are not changed that often to become tedious to keep them in sync. While sed supports for script files, at this point in complexity I'd consider rewriting this in Python. With a CSV reader and a file pattern matching library, you could easily read the mappings from a file:
BONUS 1: Additionally it'd be great not to show the same component multiple times:
If you implement this in Python it's quite easy to perform the deduplication and only show 1 line per component. BONUS 2: If you count the number of occurrences per component, you can also suggest first the component with most files changed. Let's say a commit changed "src/mon/file1, src/cephfs/file2 and src/cephfs/file3", so we display first the component with more files modified (cephfs):
Python 3.6 and above brings in a new class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a file for mapping paths to component sounds like a great idea! Yeah, keeping them in sync should not be a problem. |
||
done | ||
sed -i "1s;^;$COMMIT_MSG_HEADERS\n;" $COMMIT_MSG_FILE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have the commit message headers after signed-off by, a developer would be able to then identify more clearly if the commit is signed or not, and will not have to check after text the git default template. |
||
fi | ||
|
||
|
||
# Adds a Signed-off-by line to the message, that can still be edited (This is rarely a good idea). | ||
SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. users most times use Another thing I noticed was, doing a git amend adds a duplicate signed-off by as well; the above approach should also resolve that issue as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with @ideepika, most users alreay include the However, what we can do here is, apart from this hook, create a complementary
There are tools that already do this, but they are mostly for semantic commits, a convention we are not adhered to (it's configurable though). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, adding a complimentary hook to check the commit message is an awesome idea! Both hooks in python, got it! |
||
printf "\n$SOB" >> "$COMMIT_MSG_FILE" | ||
|
||
# Adds a newline at the start of the message | ||
# if test -z "$COMMIT_SOURCE" | ||
# then | ||
# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" | ||
# fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always better to write safer shell scripts: