Verify License Header Before Commit

Gobinath Loganathan
Cognitio
Published in
1 min readJun 8, 2016

--

Working for a software company requires more responsibilities other than coding. One is following their own standards and disciplines which includes adding license header to all the files. I rely on my IDE to automatically add license header to all the new files, but sometimes IDE fails to add the license due to some internal configuration issues. This leads me to manually check all new files I have added to the project for license header and obviously it is a boring task. Today I share my trick to automate this check.

Step 1: Create a new file named pre-commit with the following content

If you want to check the license header only for repositories from one of your account, uncomment the first if condition and replace the Git account by your one.

This script looks for license header only in newly created files.

Step 2: Make the file executable using the following command

chmod +x pre-commit

Step 3: Copy the file to Git templates folder

sudo cp pre-commit /usr/share/git-core/templates/hooks/

Step 4: Clone a new repository, add a new file without license header and try to commit the changes.

For already cloned repositories, copy the pre-commit file to the .git/hooks directory.

NOTE:

Currently there is no option to skip some selected files from verifying for license header. This hook will not allow you to commit your changes without adding license header for all new files. As a dirty fix, skip the hook as shown below if you want to avoid this validation.

git commit --no-verify

--

--