Last Updated: September 09, 2019
·
6.733K
· keyboardcowboy

Never Forget to Remove your Debugging Statements with GIT Hooks

I'm as guilty as anyone when it comes to trying to push a release out, racing against the clock and accidentally leaving a debugging statement in the code. It's happened more than once, and it needs to stop.

As a Drupal developer I have a couple that I use regularly by way of the Devel module. This is exceptionally bad when I forget to remove them since I don't push development modules into production (and neither should you). These simple little functions provide the user with a nice White Screen of Death, or, if you have your error messages on, a 'function does not exist' error.

Here's a friendly reminder via GIT hooks to clean up your code before you push those changes.

STEPS

  1. Create a pre-commit hook if you don't already have one.

  2. Add this snippet to review your code.

  3. Adjust the parameters to your desired settings.

  4. Set this in a template hook so all new repos inherit this check.

1. CREATE THE HOOK

In your git project, go to .git/hooks and copy or rename the file pre-commit.sample to pre-commit.

2. ADD THIS CODE

#!/bin/sh
#
# Check for debugging statements before commiting your code.
#

# List of function names to search for in regex format
FUNCTIONS='func1|func2|func3'

# If any functions are found as executable, prevent the commit.
DIEONFAIL=false

echo "Running the debugger check..."
RES=`egrep -nr --exclude-dir=".git" "^(\s*)?[^/{2}]($FUNCTIONS)\(.*\)" .`

if [[ -n "$RES" ]]; then
  echo "\n$RES"
  echo "\nDebugging functions were found in your code!"

  if [[ $DIEONFAIL == true ]]; then
    echo "Changes were not committed."
    exit 1;
  else
    echo "You may want to clean these up before you push those changes.\nChanges were committed anyway.\n"
    exit 0;
  fi
else
  echo "\n No debugging functions were found.  Nice job, Ace!\n";
  exit 0;
fi

Check out the Gist

3. ADJUST THE PARAMS

Replace the list of functions at the top with any functions you want to search for. Use regex format.

Set $DIEONFAIL to either true if you want the commit to die if it finds any listed functions, or leave it as false if you want the changes to be committed anyway.

4. SET THE TEMPLATE

You can create system-wide templates for your git hooks so they are inherited into every new project. See this post on StackExchange for instructions.

Now, every time you attempt to commit changes on that project, git will warn you if you accidentally left any debugging statements behind.

Drupal Example

Here's the settings for the hook I have set up in my Drupal projects. Notice I added an extra directory exclude for the devel modules.

# List of function names to search for in regex format
FUNCTIONS='dpm|kpr|qpr|console\.log'

# If any functions are found as executable, prevent the commit.
DIEONFAIL=false

echo "Running the debugger check..."
RES=`egrep -nr --exclude-dir=".git" --exclude-dir="*devel*" "^(\s*)?[^/{2}]($FUNCTIONS)\(.*\)" .`

2 Responses
Add your response

great handy tip.

over 1 year ago ·

You are grepping you entire code base!
Use git diff --cached --name-only to get a list of relevant files to search.

over 1 year ago ·