Git Hooks
2016 07 May

Git hooks for better codes

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

Healthcare mobile app development

How Healthcare Mobile Apps is Booming the Healthcare Sector

No one can deny that mobile apps have revolutionized all sectors, whether eCommerce, education, or mobile healthcare apps. There is hardly any sector untouched by the blessings of mobile apps.

Read More

Mobile UX design

Top Do's & Dont's for a Perfect Mobile User Experience Design

In any situation, the first impression is considered the last impression. The same is the case in the world of mobile applications.

Read More

Mobile app development trends

15 Effective Mobile App Development Trends That Will Boost

The mobile app development industry is continuously evolving with great speed. There is indeed tough competition in this industry.

Read More

Why it is Best to Use ReactJS for Web Development

Why it is Best to Use ReactJS for Web Development

Front-end and frameworks are inevitable and essential parts of web development. As the technologies improve day by day, many new frameworks and libraries are coming to get better results.

Read More