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

Website Web Design tips

10 Effective Website Web Design Tips: How to Design a Website That Works

A website is an important identity of any brand in today’s digital-first era. Users will first visit the website to gather information about any brand or company.

Read More

Website Revamp Process

When to plan for your website revamp and how?

The only constant in the world is "CHANGE". Though this line has become a cliche, it is very important to implement the same in our businesses.

Read More

Wireframing Tools

7 Awesome Wireframing Tools You Should Be Using

In the last decade, the importance of UI/ UX designing has increased significantly in the segment of websites, apps and mobile games mockup design and development.

Read More

CMS to use for website

Are you getting confused about which CMS to choose for your new website?

A very happy new year 2020! I am sure the celebration was fun. And I am also sure that the targets, the meetings, the daily chores are already haunting you.

Read More