simple example for git hooks
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
Quoted 2 lines · Gelonida
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.
Him
THanks a lot for your answer.
Quoted 8 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Gelonida
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.
Quoted 6 lines
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
Quoted 7 lines · 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 usegit 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.
Signature · 9 lines
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
"Gelonida" <[email protected]> wrote in message
news:<[email protected]>...
Quoted 12 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
Quoted 7 lines
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.
Quoted 1 line
- 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.
Quoted 6 lines
The issue I'm currently blocked with is rather simple.
Quoted 7 lines
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 6fi
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 7fi
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.
Quoted 4 lines
As soon as I have the file names I should be able to proceed.
Quoted 9 lines
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...
Quoted 7 lines
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
Hi everybody,
Quoted 8 lines · Gelonida
Does anyone have a simple example of a git pre-commit hook
I have difficulties finding understandable tutorals about git hooks.
Quoted 8 lines
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.