Use git hooks to automatically add JIRA ID at the start of each commit message.
Navigate to the root of the main project repository.
Create a copy of .git/hooks/commit-msg.sample
and name it commit-msg
.
cp .git/hooks/commit-msg.sample .git/hooks/commit-msg
Remove the example code from it.
# Remove this
test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
Add the following bash script code at the end of the file.
# If commit message is a fixup message, ignore it
[[ -n "$(cat $1 | grep 'fixup!')" ]] && FIXUP="YES"
TICKET=$(git symbolic-ref HEAD | rev | cut -d/ -f1 | rev | grep -o -E "[A-Z]+-[0-9]+" | head -n1)
if [[ -n "${TICKET}" && -z "${FIXUP}" ]]; then
sed -i.bak -e "1s/^/[${TICKET}] /" $1
fi
Git hooks for submodules are located in .git/modules/<Submodule/Path>/hooks/
. For example .git/modules/Libraries/SomeLib/hooks
.
Run this script to use the same gi thooks for all submodules.
for smh in $(find .git/modules -name hooks); do cp -f .git/hooks/commit-msg ${smh}/; done
Make sure you re-run it every time you change the hooks for main repo.