So I just committed a dsm() locally, and it made me think it'd be handy if we had a pre-commit hook that could warn people if their code contains any such common oopsies. It'd have to be in their local repos, of course, so it would have to be rolled out as part of whatever system we devise for getting drupally git tools into local repos.

Comments

Josh The Geek’s picture

Assigned: Unassigned » Josh The Geek
Status: Active » Needs review

Here: http://drupal.org/sandbox/JoshTheGeek/1143338
Right now it only checks for dsm() (really, only the first half: dsm( because of strstr).

Josh The Geek’s picture

Project: Drupal.org infrastructure » Anti-dsm() pre-commit hook
Component: Git » Code/Syntax

And, move to that.

attiks’s picture

can you add dpm and others as well?

budda’s picture

my vote for dpr(), db_queryd(), kpr() to be warned about.

Josh The Geek’s picture

I added the above, dprint_r() (long of dpr()) and kprint_r() (long of kpr()).
The functions to check for are now in an array at the beginning of the file.

attiks’s picture

Isn't it safer to check for a space before the function name as well?

attiks’s picture

Status: Needs review » Needs work

I added the file to .giit/hooks and added +x permission, I get the following without being able to enter y/n

Oi! Are you sure you want to commit a dpm()? Type n to cancel. (y/n) [n] [master bc3e758] blokken toevoegen
 1 files changed, 3 insertions(+), 1 deletions(-)
budda’s picture

Another one to include would be var_dump()

budda’s picture

@attiks What is the benefit of checking for a space in front of a function name? Sometimes there might not be a space before the use of a debug function.

attiks’s picture

you're right, it should be a space or the beginning of the line. The reason I asked becuase otherwise it would complain my_dpm() function calls as well

BTW: if you follow drupal coding standards, there's always a space before the function name

attiks’s picture

does anybody else have the problem described in #7, or is it my install?

Josh The Geek’s picture

@attiks #7: I think that Git is not allowing input and just runs the script. I'm going to remove the confirmation and just cancel the commit, because there are almost no possible reasons to commit any of these. To get around it, you just have to move/rename the hook temporarily.
@attiks #10: exit(dpm($blah)); is coding standards valid, right? (Just an example, I dunno what dpm returns.)
@budda #9: I agree.

I will update this later today.

agileware’s picture

Another one is dd() and that one definitely has to have a preceding space or new line.

Josh The Geek’s picture

I fixed all of the above. It no longer prompts, and I added var_dump/dd/drupal_debug().

Josh The Geek’s picture

Status: Needs work » Needs review

Once someone RTBCs this, I'll promote the project.

attiks’s picture

it's working for me, but can't we make the check more restrictive, see exit(dpm($blah)), moving the pre-commit hook isn't that user friendly ;p

so change the strstr to preg_match like this (not tested)

 if (preg_match("/\b" . $function . "\b/i", $line)) {
Josh The Geek’s picture

attiks:
From http://us2.php.net/preg_match : https://skitch.com/joshthegeek/r6ns4/php-preg-match-manual

All this will do is slow the script down on big commits.

I had a prompt about whether or not to continue, but you reported that git wasn't waiting, and just moving on. Moving the script may not be intuitive, but there are very few times you would even need to commit one of these. It tells you what function, so you can just grep $function( and get the whole line. Should I add the grep output?

attiks’s picture

josh i know it's slower, let's close this issue, i created a follow-up #1150016: Check for whitespace

attiks’s picture

Status: Needs review » Reviewed & tested by the community
budda’s picture

We've been using this for the past week at Ixis and it's been a great reminder.