All of lore.kernel.org
 help / color / mirror / Atom feed
* simple example for git hooks
@ 2010-09-02 22:41 Gelonida
  2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Gelonida @ 2010-09-02 22:41 UTC (permalink / raw)
  To: git

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: simple example for git hooks
  2010-09-02 22:41 simple example for git hooks Gelonida
@ 2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
  2010-09-02 22:54   ` Gelonida
  2010-09-03 20:37 ` Neal Kreitzinger
  2010-09-05 21:48 ` Gelonida
  2 siblings, 1 reply; 6+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2010-09-02 22:49 UTC (permalink / raw)
  To: Gelonida; +Cc: git

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: simple example for git hooks
  2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
@ 2010-09-02 22:54   ` Gelonida
  2010-09-03  9:43     ` Knut Franke
  0 siblings, 1 reply; 6+ messages in thread
From: Gelonida @ 2010-09-02 22:54 UTC (permalink / raw)
  To: git

Him
THanks a lot for your answer.

On 09/03/2010 12:49 AM, Ævar Arnfjörð Bjarmason wrote:
> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: simple example for git hooks
  2010-09-02 22:54   ` Gelonida
@ 2010-09-03  9:43     ` Knut Franke
  0 siblings, 0 replies; 6+ messages in thread
From: Knut Franke @ 2010-09-03  9:43 UTC (permalink / raw)
  To: Gelonida; +Cc: git

On Friday 03 September 2010 00:54:44 Gelonida wrote:
> 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 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: simple example for git hooks
  2010-09-02 22:41 simple example for git hooks Gelonida
  2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
@ 2010-09-03 20:37 ` Neal Kreitzinger
  2010-09-05 21:48 ` Gelonida
  2 siblings, 0 replies; 6+ messages in thread
From: Neal Kreitzinger @ 2010-09-03 20:37 UTC (permalink / raw)
  To: git

"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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: simple example for git hooks
  2010-09-02 22:41 simple example for git hooks Gelonida
  2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
  2010-09-03 20:37 ` Neal Kreitzinger
@ 2010-09-05 21:48 ` Gelonida
  2 siblings, 0 replies; 6+ messages in thread
From: Gelonida @ 2010-09-05 21:48 UTC (permalink / raw)
  To: git

Hi everybody,
On 09/03/2010 12:41 AM, Gelonida wrote:
> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-09-05 21:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-02 22:41 simple example for git hooks Gelonida
2010-09-02 22:49 ` Ævar Arnfjörð Bjarmason
2010-09-02 22:54   ` Gelonida
2010-09-03  9:43     ` Knut Franke
2010-09-03 20:37 ` Neal Kreitzinger
2010-09-05 21:48 ` Gelonida

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.