threads / discuss / 24955

simple example for git hooks

Subject: simple example for git hooks

## tl;dr

6 messages between Sep 2, 2010 and Sep 5, 2010.

replies: 5people: 4as markdown or json

Gelonida· Sep 2, 2010, 22:41 UTC · lore
Does anyone have a simple example of a git pre-commit hook
I have difficulties finding understandable tutorals about git hooks.
What I am looking at is basic examples about

precommit hooks ================

- how get the commit comment and check it's contents
- how to get list of modified files
The issue I'm currently blocked with is rather simple.

I'd like to get a list aof all new or modified file names, such, that I can check, that for example all .h files contain a project specific header.

As soon as I have the file names I should be able to proceed.
How could I do this best from a shell script.

Is there any clear documentation about hich git commands I'm allowed to use during a trigger script and which ones I can't

Lateron I would be interested to implement a small server script, that refuses a git push in case, that the most recent commit in a branch would contain .h files without a certain header text

thanks for your help
Ævar Arnfjörð Bjarmason· Sep 2, 2010, 22:49 UTC · re: Gelonida · lore

Re: simple example for git hooks

On Thu, Sep 2, 2010 at 22:41, Gelonida <gelonida@gmail.com> wrote:
> Is there any clear documentation about hich git commands I'm allowed to
> use during a trigger script and which ones I can't

It's just a program that's run at a certain point, and its exit status determines if git proceeds.

So you can use any git command, but of course what you might affect what's about to be commited.

For a pre-commit hook (IIRC) you should find the stuff to be commited in the index, finding a list of things that changed from a hook is just a matter of diffing HEAD against the contents of the index then.

Gelonida· Sep 2, 2010, 22:54 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: simple example for git hooks

Him THanks a lot for your answer.

On 09/03/2010 12:49 AM, Ævar Arnfjörð Bjarmason wrote:
Show 14 quoted lines
> On Thu, Sep 2, 2010 at 22:41, Gelonida <gelonida@gmail.com> wrote:
> 
>> Is there any clear documentation about hich git commands I'm allowed to
>> use during a trigger script and which ones I can't
> 
> It's just a program that's run at a certain point, and its exit status
> determines if git proceeds.
> 
> So you can use any git command, but of course what you might affect
> what's about to be commited.
> 
> For a pre-commit hook (IIRC) you should find the stuff to be commited
> in the index, finding a list of things that changed from a hook is
> just a matter of diffing HEAD against the contents of the index then.

ok, so it seems what I'm missing is THE command to list all added / modified files I could of course use

git status
and parse its output, but this is probably not the right way to do.

I use git for quite some time, but don't know many commands except clone/pull/commit/reset/status/pull/push/cherrypick/rebase/tag/remote

thanks a lot for the typical way to identify added / modified files and for fetching the commit comment

Knut Franke· Sep 3, 2010, 09:43 UTC · re: Gelonida · lore

Re: simple example for git hooks

On Friday 03 September 2010 00:54:44 Gelonida wrote:
Show 7 quoted lines
> ok, so it seems what I'm missing is THE command to list all added /
> modified files
> I could of course use
> 
> git status
> 
> and parse its output, but this is probably not the right way to do.

Not unless you use the --porcelain option, which is explicitly meant to produce parsable output.

For instance, if "a" has staged modifications, "b" has unstaged modifications and "c" is added (and staged), you'll get

$ git status --porcelain    
M  a                                           
 M b                                           
A  c

i.e. the first character in a line denotes the difference between HEAD and index and the second character denotes the difference between index and working copy.

-- 
Vorstand/Board of Management:
Dr. Bernd Finkbeiner, Dr. Roland Niemeier, 
Dr. Arno Steitz, Dr. Ingrid Zech
Vorsitzender des Aufsichtsrats/
Chairman of the Supervisory Board:
Michel Lepert
Sitz/Registered Office: Tuebingen
Registergericht/Registration Court: Stuttgart
Registernummer/Commercial Register No.: HRB 382196 
Neal Kreitzinger· Sep 3, 2010, 20:37 UTC · re: Gelonida · lore

Re: simple example for git hooks

"Gelonida" <gelonida@gmail.com> wrote in message news:<i5p96s$u7q$1@dough.gmane.org>...

> Does anyone have a simple example of a git pre-commit hook
>
> I have difficulties finding understandable tutorals about git hooks.
>
> What I am looking at is basic examples about
>
> precommit hooks
> ================
>
> - how get the commit comment and check it's contents

I think there is a special hook for this besides the pre-commit hook. Haven't tried it myself, yet. Look at githooks manpage in the reference manual.

> - how to get list of modified files
I use this script to get modified files:
ABORTMSG="Commit Aborted!"

DIFFFILES=`git diff-index HEAD --cached --name-only SRC/*/* MYSRC/*` if [ $? -ne 0 ]; then

  echo "error running git diff-index command"
  echo $ABORTMSG
  exit 4
fi

Where SRC and MYSRC are the paths that contain the files I'm interested in. If you really want to list every thing that changed then don't specify paths.

>
> The issue I'm currently blocked with is rather simple.
>
> I'd like to get a list aof all
> new or modified file names, such, that I can check, that for  example
> all .h files contain a project specific header.
>
I use this script to check the header:
for FILES in ${DIFFFILES}
  do
  echo "checking header for keywords in working copy of:${FILES}"
  if [ ! -r ${FILES} ]; then
    echo "${FILES} needs read permission"
    echo $ABORTMSG
    exit 6
  fi
  CHKHEADER=$(/usr/bin/head -n 2 ${FILES} | /bin/egrep -c 'somekeyword')
  if [ ${CHKHEADER} -ne 1 ]; then
    echo "line 1 or 2 needs those special keywords in it:${FILES}"
    echo $ABORTMSG
    exit 7
  fi
  done

THIS IS BASED ON THE ASSUMPTION THAT YOUR WORKING COPY AND INDEX ENTRY ARE SUPPOSED TO MATCH! Its based on a workflow in which you commit your current work. I you want to be able to git-add a file to the index and then modify the file again and then only commit what's in the index while retaining a working copy that differs from the index, THEN THIS SCRIPT WON'T WORK.

> As soon as I have the file names I should be able to proceed.
>
> How could I do this best from a shell script.
>
> Is there any clear documentation about hich git commands I'm allowed
> to use during a trigger script and which ones I can't
>

Keep in mind that git hooks don't allow you to prompt the user in your script. However, the exception may be git commands that prompt the user. Haven't tried that combination myself yet...

> Lateron I would be interested to implement a small server script, that
> refuses a git push in case, that the most recent commit in a branch
> would contain .h files without a certain header text
>

Your pre-commit hook will prevent this because you can only push commits. Unless you don't have control over what hooks the push-er is using in their repo... In which case there is a hook that checks push-ed content before committing it and will reject it if need be. Can't remember what it is offhand. Look at the githooks refmanual entry http://www.kernel.org/pub/software/scm/git/docs/v1.7.1.2/githooks.html for whatever git version you're using and you should be able to find it. These scripts would probably work in that remote-side hook.

v/r,
Neal
Gelonida· Sep 5, 2010, 21:48 UTC · re: Gelonida · lore

Re: simple example for git hooks

Hi everybody, On 09/03/2010 12:41 AM, Gelonida wrote:

Show 16 quoted lines
> Does anyone have a simple example of a git pre-commit hook
> 
> 
> I have difficulties finding understandable tutorals about git hooks.
> 
> 
> 
> 
> What I am looking at is basic examples about
> 
> 
> precommit hooks
> ================
> 
> - how to get list of modified files
> 

Thanks for all your answers. I'm currently using git diff-index HEAD --cached

This seems to do what I was looking for.

← back to recent threads