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

Digital Public Goods Alliance Strategy 2021–2026

Boosting Digital Infrastructure with Digital Public Goods

The United Nations (UN) defines a roadmap for Digital Public Goods (DPGs) as open-source software, open data, open AI models, open standards, and open content.

Read More

Best Practices for Software Testing

Power of Software Testing: Why Software Teams Use It Effectively

In the modern digital era, where software is used in nearly every aspect of everyday life, the importance of software testing cannot be emphasized.

Read More

Benefits of Programmatic SEO Implementation

What It Is and How to Implement It How SEOs Make the Web Better

In the rapidly evolving field of SEO, staying ahead of the curve necessitates adopting new strategies and utilizing technology.

Read More

Unlocking the potential of SEO for boosting business

Branding With SEO: Boost brand Visibility, credibility, and Engagement With An SEO Strategy

Many people think that the primary function of search engine optimization is to garner organic traffic on a website; however, it is beyond that.

Read More