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...

 

Featured blog

web-personalization

Personalized Content is a Proven Entity !!

Irrespective of how big a business icon or brand you are, increasing the relevance of your website will always be critical to your success.

Read More

Git Hooks

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.

Read More

Drupal ,varnish cache

Hard time with Drupal, Varnish Cache and Cookies

Using a reverse proxy server in front of a web server is usually needed for every big site and it is a very good thing to do so as reverse proxy server will handle all the anonymous traff

Read More

Say no to captcha

Say no to captcha - Various Spam Protection Methods

Maintaining high traffic websites have their own merits and demerits, the most annoying thing about them is SPAM.

Read More