DrupalCon Nashville 2018: Integration of Drupal Coding standards with Git hooks

Introduction

Git hooks are scripts that Git executes before or after events such as: commit,
push, and receive. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally.

Hooks we will use in our session :

pre-commit: Check for the line of code thats getting committed. This is something i will be explaining in detail in this article.
pre-receive: Checks for the code that is getting pushed to git-repo. Invoked before the line of code gets pushed to git repo.
post-receive: Gets triggered once the code is merged to the remote git repo. A simple usecase could be to notify other team members by sending an email with commit link and the commit message or to deploy the committed code to production


These hooks are disabled in default state on your local repository and resides in .git/hooks. you just need to remove .sample extension, make them executable and you are all set to go!!

The main objective while reviewing a pull request is to focus on the logic. As a Drupal developer and code reviewer, i have noticed people commiting code which lacks coding standard and at times they have syntactical errors as well. This used to waste a lot of my time as well as the developers leading to obvious problems towards the end of the Projects. when i found git-hooks and started playing around with it. Right now i have a pre-commit hook that tests the files for coding standards and syntactical errors before them getting committed and reaching to me. The following code in the pre-commit file takes care of parsing any file thats getting committed against Drupalcoding standards using PHP_Codesniffer and then parsing them against php lint.



Take Away for attendees:

Improved Project Management.
Proper code as per coding standards.
Proper workflow when you deploy something on the repository.
Proper formatted commit messages as per defined in the documentation.

Drupal is a registered trademark of Dries Buytaert.