A minimally modified Gerrit commit-msg hook that automatically adds a Change-Id footer to Git commit messages.
This repository also includes a prepare-commit-msg hook to support commits created with git revert.
commit-msg
prepare-commit-msg
Generates and inserts a Gerrit Change-Id into the commit message footer.
Detects commit messages created by git revert and runs the commit-msg hook before the revert commit is finalized.
Run the following commands from the root directory of your Git repository:
mkdir -p .git/hooks
curl -fsSL \
https://raw.githubusercontent.com/greenforce-project/commit-msg/main/commit-msg \
-o .git/hooks/commit-msg
curl -fsSL \
https://raw.githubusercontent.com/greenforce-project/commit-msg/main/prepare-commit-msg \
-o .git/hooks/prepare-commit-msg
chmod +x .git/hooks/commit-msg
chmod +x .git/hooks/prepare-commit-msgVerify the installation:
ls -l .git/hooks/commit-msg .git/hooks/prepare-commit-msgValidate the shell syntax:
sh -n .git/hooks/commit-msg
sh -n .git/hooks/prepare-commit-msgNo output means the syntax is valid.
After installation, use Git normally.
git add .
git commit -m "kernel: update scheduler configuration"Check the generated commit message:
git show -s --format=%B HEADExpected result:
kernel: update scheduler configuration
Change-Id: Ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
To add a missing Change-Id to the latest commit:
git commit --amend --no-editThe hook preserves the existing commit message and adds a new Change-Id if one does not already exist.
Check the result:
git show -s --format=%B HEADTo always generate a Change-Id, including for fixup and squash commits:
git config gerrit.createChangeId alwaysApply the setting globally:
git config --global gerrit.createChangeId alwaysCheck the current setting:
git config --get gerrit.createChangeIdExpected output:
always
To disable automatic Change-Id generation for the current repository:
git config gerrit.createChangeId falseRemove the local configuration:
git config --unset gerrit.createChangeIdRun these commands from the root of the target repository:
curl -fsSL \
https://raw.githubusercontent.com/greenforce-project/commit-msg/main/commit-msg \
-o .git/hooks/commit-msg
curl -fsSL \
https://raw.githubusercontent.com/greenforce-project/commit-msg/main/prepare-commit-msg \
-o .git/hooks/prepare-commit-msg
chmod +x .git/hooks/commit-msg
chmod +x .git/hooks/prepare-commit-msgCheck whether the hook exists:
ls -l .git/hooks/commit-msgMake sure it is executable:
chmod +x .git/hooks/commit-msgCheck whether automatic generation is disabled:
git config --get gerrit.createChangeIdIf the result is false, enable it:
git config gerrit.createChangeId alwaysCheck whether prepare-commit-msg is installed:
ls -l .git/hooks/prepare-commit-msgMake it executable:
chmod +x .git/hooks/prepare-commit-msgFor an already-created revert commit:
git commit --amend --no-editValidate both hook files:
sh -n .git/hooks/commit-msg
sh -n .git/hooks/prepare-commit-msgBack up existing hooks before installation:
cp .git/hooks/commit-msg .git/hooks/commit-msg.backup 2>/dev/null || true
cp .git/hooks/prepare-commit-msg .git/hooks/prepare-commit-msg.backup 2>/dev/null || trueRemove both hooks:
rm -f .git/hooks/commit-msg
rm -f .git/hooks/prepare-commit-msgRemove the local Gerrit configuration if needed:
git config --unset gerrit.createChangeId- Git hooks are stored inside
.git/hooksand are not committed with the target project repository. - Each developer must install the hooks in their local clone.
- Existing
Change-Idfooters are preserved. - A Gerrit
Change-Idis different from a Git commit hash. - Amending a commit preserves its existing
Change-Id.