Git Hooks
2016 07 May

Git hooks for better codes

By Shuva Mallick

We are programmers and we are always on the lookout for ways to improve our code. A good and structured way of coding defines the completeness of a programmer. GIT, a distributed version control system, is a platform that is a savior .... also has a way that lets programmers improve their structure of code.
Yes, we are talking about "Git Hooks" that come bundled with it.

Generally, in Drupal, we have the coder module and even many other software packages that help us in structuring our codes to follow coding standards. But sometimes when we work on large existing projects that contain enormous amount of codes, it becomes difficult for an individual to check every bit of code for coding standards, even more frustrating during urgent client works when the commit shows an enormous list of coding standard issues that are not even written by us.

Today i will discuss a method by which your code will be checked for coding standards only for code you have written to be commit. Among the several hooks git provides, one such hook that comes to the rescue for only committed code is the "pre-commit" hook.
You can find it under .git->hooks. This hook will not let you commit unless your code follows Drupal coding standards.

You can also see a list of other git hooks, and you can try them out according to your needs and desires.

  • Copy the pre-commit.sample to create another file named pre-commit and give it the executable permission using "chmod +x pre-commit"
  • Open the new file pre-commit in your favourite text editor and paste the following code:

 

#!/usr/bin/php

$output = array();
$return = 0;
exec('git rev-parse --verify HEAD', $output, $return);

// Get GIT revision
$against = $return == 0 ? 'HEAD' : '';

// Get the list of files in this commit.
exec("git diff-index --cached --name-only {$against}", $output);

// Pattern to identify the files that need to be run with phpcs and php lint
$filename_pattern = '/\.(php|module|inc|install)$/';
$exit_status = 0;

// Loop through files.
foreach ($output as $file) {
if ( ! preg_match($filename_pattern, $file)) {
// don't check files that don't match the pattern
continue;
}

// If file is removed from git do not sniff.
if ( ! file_exists($file))
{
continue;
}

$lint_output = array();
// Run the sniff
exec("phpcs --standard=Drupal " . escapeshellarg($file), $lint_output, $return_phpcs);
// Run php lint
exec("php -l " . escapeshellarg($file), $lint_output, $return_lint);

if (($return_phpcs == 0) && ($return_lint == 0)) {
continue;
}
echo implode("\n", $lint_output), "\n";
$exit_status = 1;
}

exit($exit_status);
 

 

This piece of code checks for any code that gets committed against Drupal Coding standards against "PHP_Codesniffer".

  • Now you need PHP_Codesniffer to check your code. To install run the following code on your linux systems: "pear install PHP_CodeSniffer".
  • Next we will set the rules for Drupal Coding standards so that phpcs will be able to evaluate the code against the standards:

drush dl coder(download coder)
cd coder/coder_sniffer
cp -r Drupal {full path to php_codesniffer/CodeSniffer/Standards/ (it should be something like this: /usr/share/php/PHP/CodeSniffer/Standards/)

You are all but done.
To test if your phpcs is working execute the following code: phpcs --standard=Drupal {path to any Drupal file}

If you are good till now then you are good to go. The next time you commit, only the code you want to commit with coding standard issues will be highlighted. Anything that does not fulfill Drupal Coding standards will not be committed.

In case you want some of your committed code to bypass the check you can execute the following command after git add: "git commit -m "your commit message" --no-verify". BUT DO NOT DO IT.

 

HAPPY CODING...

 

Latest Blogs

intor_react

Introduction to React JS and Components

React is a flexible JavaScript library for building user interfaces and it lets you compose complex UIs from small and isolated pieces of codes which is known as Components.

Read More

Flexbox

How to simplify the layout of your website using Flexbox?

Have you ever scratched your head while working with CSS to make some simple layouts for your website? Are you fed up of running to the UI developers for small layout designs every time?

Read More

cache_context

Drupal 8 Cache Context: An efficient way for context based caching.

We are well aware of the fact that Drupal Cache API is a remarkable feature introduced in Drupal 8.

Read More

CacheImg

How does entity cache work in Drupal 8

The Drupal Cache API is used to store data that takes a long time to compute.

Read More