Git hooks

Discuss general development subjects that are not specific to a particular version like the versioning control system we use or other infrastructure.
User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Git hooks

Post by igorw » Mon Mar 08, 2010 12:37 am

The first git hook has just been committed. I will quote from the commit:
Git supports several hooks, some of which are client-side. The
prepare-commit-msg hook is run right after a `git commit` call, before
the editor is opened. This allows the initial message to be altered.

This hook will check if the current branch name begins with `bug/`, in
which case it will prepend `[$branchname]` to the commit message. This
makes it easier to create proper commit messages.

For more information refer to the hook source.
Additionally: `feature/` is now also supported. You can find installation instructions on the wiki.

I'm interested if there's any other hooks people could think of.

User avatar
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: Git hooks

Post by ToonArmy » Mon Mar 08, 2010 10:26 pm

Great idea :)
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Mon Mar 08, 2010 10:48 pm

I've just had another idea for a hook. Well, actually it's not mine, I've seen this used at a popular open source project (phpBB).

How about a pre-commit hook that runs `php -l` on any file with .php extension. the -l stands for "lint", and makes sure there are no syntax errors in the file. If there are errors, they are echoed and the commit is aborted.

User avatar
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: Git hooks

Post by ToonArmy » Tue Mar 09, 2010 7:37 pm

eviL3 wrote:I've just had another idea for a hook. Well, actually it's not mine, I've seen this used at a popular open source project (phpBB).

How about a pre-commit hook that runs `php -l` on any file with .php extension. the -l stands for "lint", and makes sure there are no syntax errors in the file. If there are errors, they are echoed and the commit is aborted.
Sounds good to me. :)
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Wed Mar 10, 2010 7:15 pm

I have something: http://github.com/evil3/phpbb3/compare/git-tools

Please test it (and the other one too), thanks.

Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: Git hooks

Post by Oleg » Wed Mar 10, 2010 8:14 pm

I would suggest using mktemp to create a temporary file name.

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Wed Mar 10, 2010 8:43 pm

Thanks, implemented. Now there's still a problem though. The hook will use the current working tree file. If the file is only partially staged (or staged and then edited) it will use the current state instead of the staged one.

My guess is that this would need more low-level git commands, to read the index, echo the contents and pipe them into php -l. Any git gurus around? :)

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Wed Mar 10, 2010 10:01 pm

It might be possible to do it using git diff-index --cached $against and git cat-file, but I've been unable to parse the former command's output properly. Perhaps it's easier to do it using something other than bash.

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Wed Mar 17, 2010 8:20 pm

I have finally managed to get it working with the methods I described above. It will now fetch the file contents from the index using `git cat-file` instead of using the filesystem. Suggestions and testing are welcome.

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Git hooks

Post by igorw » Sat Apr 03, 2010 12:18 am

Is anyone interested in testing these?

Post Reply