All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Neal Kreitzinger" <neal@rsss.com>
To: git@vger.kernel.org
Subject: Re: simple example for git hooks
Date: Fri, 3 Sep 2010 15:37:00 -0500	[thread overview]
Message-ID: <i5rmb2$q4n$1@dough.gmane.org> (raw)
In-Reply-To: i5p96s$u7q$1@dough.gmane.org

"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

  parent reply	other threads:[~2010-09-03 20:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2010-09-05 21:48 ` Gelonida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='i5rmb2$q4n$1@dough.gmane.org' \
    --to=neal@rsss.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.