Git hooks

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

Git hooks

Post by igorw »

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.

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

Re: Git hooks

Post by ToonArmy »

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

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

Re: Git hooks

Post by igorw »

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.

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

Re: Git hooks

Post by ToonArmy »

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

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

Re: Git hooks

Post by igorw »

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 »

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

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

Re: Git hooks

Post by igorw »

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? :)

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

Re: Git hooks

Post by igorw »

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.

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

Re: Git hooks

Post by igorw »

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.

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

Re: Git hooks

Post by igorw »

Is anyone interested in testing these?

Post Reply