Ruby on Rails : GIT Precommit hooks Basics : Block debug command from commit

When you initialize git in your project you get a .git dir created in your project directory. The .git directoy contains a folder hooks along with other folders. Inside the hooks folder if you look you can see some files with prefix ‘sample’ in their names. The postfixes are the types of Hooks supported by git client. However, it may differ due to git version you are using.

What is Hook?

Hook is a piece of script with the help of which developer/programmer can customize the behavior of pre-packaged software. From the name of the hook we can normally anticipate at which point of execution the pre-packaged software execute the custom code.

For Example:

In Ruby On Rails, there are several hooks like 'before_filter' , 'before_action', etc are supported and in the most famous ORM (ActiveRecord) hooks like 'before_save' ,'before_create', 'after_create', etc are supported.

Client-Side git Hooks

– applypatch-msg
– pre-applypatch
– post-applypatch
– pre-commit
– prepare-commit-msg
– commit-msg
– post-commit
– pre-rebase
– post-checkout
– post-merge
– pre-push
– pre-receive
– update
– post-receive
– post-update
– push-to-checkout
– pre-auto-gc
– post-rewrite

pre-commit

This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git commit to abort.

The default pre-commit hook, when enabled, catches introduction of lines with trailing white spaces and aborts the commit when such a line is found.

All the git commit hooks are invoked with the environment variable GIT_EDITOR=: if the command will not bring up an editor to modify the commit message.

You can see the file .git/hooks/pre-commit.sample to learn to write hook script. You can alse write the hook script in Ruby Language. Make sure the file has read-write-execute permission and remove the .sample postfix from the filename.

How to Use?

Here is an example pre-commit hook file written in ruby language. Download the file and save as .git/hooks/pre-commit

The Hook has following features:

# Here you can see, the git hook script check if the current commit 
# contains the following string or not
/TMP_DEBUG/, # My TextExpander macros for embedding debug code always include this for easy scanning.
 />>>>>>/, # Git conflict markers
 /<<<<<# ''
 /binding\.pry/, # pry debugging code
 /binding\.remote_pry/, # ''
 /save_and_open_page/, # Launchy debugging code
 /debugger/, # Ruby < 2.0 debugging code
 /byebug/, # Ruby >= 2.0 debugging code
 /logger\.debug/ # I almost never want to commit a (Ruby) call to logger.debug. error, message, etc., but not debug.

Advantages of Above pre-commit hook

  • Now if unknowingly you have type some random words then you cannot commit because it checks of any syntax errors
  • Debugging codes such as ‘binding.pry‘, ‘byebug‘, ‘debugger‘ and so on cannot be commited
  • console.[xxx]‘ snippets such as ‘console.log‘ cannot be commit – it breaks the code sequence in IE browsers
  • You can customize the file for your own use as you like

Note:

the hooks files defined inside the path .git/hooks/ need to have executable permissions. so make sure it has by executing the commands ls -la. You can change permission using the chmod command in Linux and Mac OSes

In some cases (OSes) the ruby Shebang may not work so you can try the following

#!/usr/bin/env ruby
doesnot work in my case
#!/usr/bin/ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s