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
Conversation
@VallariAg this is awesome, thanks for the contribution! I was just curious why didn't you use .git/hooks directory itself? I tried your PR locally, we are not retaining default github comments, can we do a append to commit message defaults that github has, instead of complete template replacing? |
Hey @ideepika, thank you!
Scripts inside
Awesome, I'll make these changes!
My bad, I'll edit my commit message with the changes you mentioned above. |
06d6308
to
a521e14
Compare
Resolves https://tracker.ceph.com/issues/51537 Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
a521e14
to
f951d75
Compare
Added the "signed-off-by" and the default commit message text.
|
@VallariAg thanks for the update, added minor changes needs, looks good overall! |
githooks/prepare-commit-msg
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
users most times use git commit -s
to add signed off by line, can add a check for if signed-off-by is specified/present already in the commit message file; then do not add new.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @ideepika, most users alreay include the -s
or alias the commit -s
command.
However, what we can do here is, apart from this hook, create a complementary pre-commit
hook (if you write both in Python you can share most of the code from a third file) that basically detects:
- the commit message follows the format:
component: subject
(andcomponent
has to match a list of predefined components obtained from the path-component mapping file) - check the commit title length (<72 chars).
- Basically everything else required here.
Fixes:
(not mandatory)Signed-off-by:
.
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 comment
The 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!
I think we would be going for the commit-msg
hook, as they run in order: pre-commit
-> prepare-commit-msg
-> commit-msg
.
Both hooks in python, got it!
githooks/prepare-commit-msg
Outdated
done | ||
sed -i "1s;^;$COMMIT_MSG_HEADERS\n;" $COMMIT_MSG_FILE |
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 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.
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.
Thanks a lot @VallariAg for your magnificent contribution here! 🥳 I left a few suggestions over there.
githooks/prepare-commit-msg
Outdated
@@ -0,0 +1,43 @@ | |||
#!/usr/bin/sh |
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.
#!/usr/bin/sh | |
#!/usr/bin/env sh |
Or bash
githooks/prepare-commit-msg
Outdated
COMMIT_MSG_FILE=$1 | ||
COMMIT_SOURCE=$2 | ||
SHA1=$3 |
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.
Have you tested this with a commit template? I'm asking because many people use that and I tried and it bypasses this. It might be interesting to check if there's a way to make both things work together.
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.
I haven't tried using commit templates, I'll try and test that!
githooks/prepare-commit-msg
Outdated
# src/<project>/file.txt - "<project>:" | ||
# file.txt - "file.txt:" | ||
# <project>/file.txt - "<project>:" | ||
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 comment
The 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:
doc/* doc
src/mon/* mon
...
src/pybind/mgr/dashboard/* mgr/dashboard
BONUS 1: Additionally it'd be great not to show the same component multiple times:
# Possible commit messages:
# pybind/mgr/dashboard : <commit_message>
# pybind/mgr/dashboard : <commit_message>
# mon: <commit_message>
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):
# Possible commit messages:
# cephfs : <commit_message>
# mon: <commit_message>
Python 3.6 and above brings in a new class collections.Counter
exactly for this purpose.
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.
Creating a file for mapping paths to component sounds like a great idea! Yeah, keeping them in sync should not be a problem.
Sounds awesome, doing this in python makes sense.
githooks/prepare-commit-msg
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @ideepika, most users alreay include the -s
or alias the commit -s
command.
However, what we can do here is, apart from this hook, create a complementary pre-commit
hook (if you write both in Python you can share most of the code from a third file) that basically detects:
- the commit message follows the format:
component: subject
(andcomponent
has to match a list of predefined components obtained from the path-component mapping file) - check the commit title length (<72 chars).
- Basically everything else required here.
Fixes:
(not mandatory)Signed-off-by:
.
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).
githooks/prepare-commit-msg
Outdated
# the commit is aborted. | ||
# | ||
# To enable this hook, rename this file to "prepare-commit-msg". | ||
|
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:
set -euxo pipefail |
@VallariAg regarding the file location, if you assume that users might have their own hooks in |
Oh, yes, that makes sense! symlinks it is, then. |
To use these scripts, run: - chmod +x githooks/prepare-commit-msg - ln -s githooks/* .git/hooks/ Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days. |
I've pushed some of the work I did on this, using python scripts to achieve the suggested results. Though while working on this, I realized I need to re-approach a part of how commit-msgs are editing messages because of unexpected results in few cases (example: using "--amend" doesn't show the commit message template and use of these hooks with "-m" needs more customization). |
This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days. |
This pull request has been automatically closed because there has been no activity for 90 days. Please feel free to reopen this pull request (or open a new one) if the proposed change is still appropriate. Thank you for your contribution! |
Resolves https://tracker.ceph.com/issues/51537
For files changes in
src/abc/xyz/file.ext
,abcd/file2.txt
, andfile3.txt
, this would create a template with the file path (and remove the redundant "/src" part):The developer can simply uncomment the most relevant lines and replace
<commit_message>
with their message text.To use git hooks, configure git to use
githooks
directory instead of.git/hooks/
(reason: maintaining git hooks in repository) by running:Checklist
Show available Jenkins commands
jenkins retest this please
jenkins test classic perf
jenkins test crimson perf
jenkins test signed
jenkins test make check
jenkins test make check arm64
jenkins test submodules
jenkins test dashboard
jenkins test dashboard cephadm
jenkins test api
jenkins test docs
jenkins render docs
jenkins test ceph-volume all
jenkins test ceph-volume tox