Automatic CHANGELOG with git
This is a simple tip of how you can generate automatic changelogs for your projects using git.
Know your hooks
For Git novices that never dared to tap into the .git folder, there resides the hooks folder. Inside this folder, you'll find a series of scripts can be activated and will run on git actions, let's take a look.
As you see for the image above, there are bunch of hooks we can activate. Here some:
pre-push : This will always run, as its name implies, before a push. You can use this hook for example, to run all your tests before you send code to a remote.
pre-commit : Runs before a commit
The CHANGELOG hook
Know, let's go straight to the point.
First create a post-commit file in the .git/hooks folder. The contents of my post-commit file is as follows.
The script is very simple, I do a git log with 3 parameters:
- -no-pager : Do not pipe Git output into a pager (less)
- -no-merges: Does not print commits with more than one parent (same as max-parents=1)
- -format : Pretty print the commit content in a specific format, possible values are ‘oneline’, ‘short’, ‘medium’, ‘full’, ‘fuller’, ‘email’, ‘raw’, ‘format:’ and ‘tformat:’
The log output is then redirected do the CHANGELOG.md file on the project root folder.
After saving the file, just do a chmod +x .git/hooks/post-commit and that's it. Now, always after the commit, your CHANGELOG.md will be updated.
I hope you find it useful and does not forget to comment. 'till next tip.
Top comments (0)