Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git hook to warn if changelog not updated?

Tags:

git

githooks

How can I write a git hook that outputs a warning in the comment section if there have been no commits to CHANGELOG on the current branch ?

I would like to output something like:

# CHANGELOG Not updated.
#
# Update changelog before submitting PR.
#
like image 291
Stuart Axon Avatar asked Mar 08 '19 10:03

Stuart Axon


People also ask

What is pre-commit hook in git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

How do you trigger a pre-commit hook?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow.

What is Githooks?

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.


1 Answers

If you want a local commit hook, you can add this script under .git/hooks/pre-commit

#!/bin/bash
if git status -s | grep -q "M CHANGELOG"; then
    exit 0
else
    echo "# CHANGELOG Not updated."
    exit 1
fi

Please notice that commit hooks are not versioned nor included in the repository

like image 123
wiomoc Avatar answered Sep 22 '22 16:09

wiomoc