{"thread":{"id":"24955","subject":"simple example for git hooks","startedAt":"2010-09-02T22:41:31Z","lastAt":"2010-09-05T21:48:41Z","messageCount":6,"participants":["Gelonida","Ævar Arnfjörð Bjarmason","Knut Franke","Neal Kreitzinger"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"149730","messageId":"i5p96s$u7q$1@dough.gmane.org","threadId":"24955","inReplyTo":null,"subject":"simple example for git hooks","fromName":"Gelonida","fromEmail":"gelonida@gmail.com","sentAt":"2010-09-02T22:41:31Z","receivedAt":"2010-09-02T22:41:31Z","isPatch":false,"sender":{"key":"gelonida@gmail.com","avatar":null},"body":"Does anyone have a simple example of a git pre-commit hook\n\n\nI have difficulties finding understandable tutorals about git hooks.\n\n\n\n\nWhat I am looking at is basic examples about\n\n\nprecommit hooks\n================\n\n- how get the commit comment and check it's contents\n- how to get list of modified files\n\nThe issue I'm currently blocked with is rather simple.\n\nI'd like to get a list aof all\nnew or modified file names, such, that I can check, that for  example\nall .h files contain a project specific header.\n\n\nAs soon as I have the file names I should be able to proceed.\n\n\nHow could I do this best from a shell script.\n\n\n\n\n\nIs there any clear documentation about hich git commands I'm allowed to\nuse during a trigger script and which ones I can't\n\n\n\nLateron I would be interested to implement a small server script, that\nrefuses a git push in case, that the most recent commit in a branch\nwould contain .h files without a certain header text\n\n\n\n\nthanks for your help\n"},{"id":"149732","messageId":"AANLkTimEo=sV=bKHyuQ5Md_7uQ0jAFKx+=dv4+KK+oD7@mail.gmail.com","threadId":"24955","inReplyTo":"i5p96s$u7q$1@dough.gmane.org","subject":"Re: simple example for git hooks","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2010-09-02T22:49:45Z","receivedAt":"2010-09-02T22:49:45Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Thu, Sep 2, 2010 at 22:41, Gelonida <gelonida@gmail.com> wrote:\n\n> Is there any clear documentation about hich git commands I'm allowed to\n> use during a trigger script and which ones I can't\n\nIt's just a program that's run at a certain point, and its exit status\ndetermines if git proceeds.\n\nSo you can use any git command, but of course what you might affect\nwhat's about to be commited.\n\nFor a pre-commit hook (IIRC) you should find the stuff to be commited\nin the index, finding a list of things that changed from a hook is\njust a matter of diffing HEAD against the contents of the index then.\n"},{"id":"149733","messageId":"i5p9vl$tk$1@dough.gmane.org","threadId":"24955","inReplyTo":"AANLkTimEo=sV=bKHyuQ5Md_7uQ0jAFKx+=dv4+KK+oD7@mail.gmail.com","subject":"Re: simple example for git hooks","fromName":"Gelonida","fromEmail":"gelonida@gmail.com","sentAt":"2010-09-02T22:54:44Z","receivedAt":"2010-09-02T22:54:44Z","isPatch":false,"sender":{"key":"gelonida@gmail.com","avatar":null},"body":"Him\nTHanks a lot for your answer.\n\nOn 09/03/2010 12:49 AM, Ævar Arnfjörð Bjarmason wrote:\n> On Thu, Sep 2, 2010 at 22:41, Gelonida <gelonida@gmail.com> wrote:\n> \n>> Is there any clear documentation about hich git commands I'm allowed to\n>> use during a trigger script and which ones I can't\n> \n> It's just a program that's run at a certain point, and its exit status\n> determines if git proceeds.\n> \n> So you can use any git command, but of course what you might affect\n> what's about to be commited.\n> \n> For a pre-commit hook (IIRC) you should find the stuff to be commited\n> in the index, finding a list of things that changed from a hook is\n> just a matter of diffing HEAD against the contents of the index then.\n\nok, so it seems what I'm missing is THE command to list all added /\nmodified files\nI could of course use\n\ngit status\n\nand parse its output, but this is probably not the right way to do.\n\nI use git for quite some time, but don't know many commands except\nclone/pull/commit/reset/status/pull/push/cherrypick/rebase/tag/remote\n\nthanks a lot for the typical way to identify added / modified files\nand for fetching the commit comment\n"},{"id":"149751","messageId":"201009031143.10539.k.franke@science-computing.de","threadId":"24955","inReplyTo":"i5p9vl$tk$1@dough.gmane.org","subject":"Re: simple example for git hooks","fromName":"Knut Franke","fromEmail":"k.franke@science-computing.de","sentAt":"2010-09-03T09:43:08Z","receivedAt":"2010-09-03T09:43:08Z","isPatch":false,"sender":{"key":"knut.franke@gmx.de","avatar":null},"body":"On Friday 03 September 2010 00:54:44 Gelonida wrote:\n> ok, so it seems what I'm missing is THE command to list all added /\n> modified files\n> I could of course use\n> \n> git status\n> \n> and parse its output, but this is probably not the right way to do.\n\nNot unless you use the --porcelain option, which is explicitly meant to produce\nparsable output.\n\nFor instance, if \"a\" has staged modifications, \"b\" has unstaged modifications\nand  \"c\" is added (and staged), you'll get\n\n$ git status --porcelain    \nM  a                                           \n M b                                           \nA  c\n\ni.e. the first character in a line denotes the difference between HEAD and\nindex and the second character denotes the difference between index and\nworking copy.\n-- \nVorstand/Board of Management:\nDr. Bernd Finkbeiner, Dr. Roland Niemeier, \nDr. Arno Steitz, Dr. Ingrid Zech\nVorsitzender des Aufsichtsrats/\nChairman of the Supervisory Board:\nMichel Lepert\nSitz/Registered Office: Tuebingen\nRegistergericht/Registration Court: Stuttgart\nRegisternummer/Commercial Register No.: HRB 382196 \n\n"},{"id":"149784","messageId":"i5rmb2$q4n$1@dough.gmane.org","threadId":"24955","inReplyTo":"i5p96s$u7q$1@dough.gmane.org","subject":"Re: simple example for git hooks","fromName":"Neal Kreitzinger","fromEmail":"neal@rsss.com","sentAt":"2010-09-03T20:37:00Z","receivedAt":"2010-09-03T20:37:00Z","isPatch":false,"sender":{"key":"neal@rsss.com","avatar":null},"body":"\"Gelonida\" <gelonida@gmail.com> wrote in message \nnews:<i5p96s$u7q$1@dough.gmane.org>...\n\n> Does anyone have a simple example of a git pre-commit hook\n\n>\n\n> I have difficulties finding understandable tutorals about git hooks.\n\n>\n\n> What I am looking at is basic examples about\n\n>\n\n> precommit hooks\n\n> ================\n\n>\n\n> - how get the commit comment and check it's contents\n\n\n\nI think there is a special hook for this besides the pre-commit hook. \nHaven't tried it myself, yet.  Look at githooks manpage in the reference \nmanual.\n\n\n\n> - how to get list of modified files\n\n\n\nI use this script to get modified files:\n\n\n\nABORTMSG=\"Commit Aborted!\"\n\nDIFFFILES=`git diff-index HEAD --cached --name-only SRC/*/* MYSRC/*` if [ \n$? -ne 0 ]; then\n\n  echo \"error running git diff-index command\"\n\n  echo $ABORTMSG\n\n  exit 4\n\nfi\n\n\n\nWhere SRC and MYSRC are the paths that contain the files I'm interested in. \nIf you really want to list every thing that changed then don't specify \npaths.\n\n\n\n>\n\n> The issue I'm currently blocked with is rather simple.\n\n>\n\n> I'd like to get a list aof all\n\n> new or modified file names, such, that I can check, that for  example\n\n> all .h files contain a project specific header.\n\n>\n\n\n\nI use this script to check the header:\n\n\n\nfor FILES in ${DIFFFILES}\n\n  do\n\n  echo \"checking header for keywords in working copy of:${FILES}\"\n\n  if [ ! -r ${FILES} ]; then\n\n    echo \"${FILES} needs read permission\"\n\n    echo $ABORTMSG\n\n    exit 6\n\n  fi\n\n  CHKHEADER=$(/usr/bin/head -n 2 ${FILES} | /bin/egrep -c 'somekeyword')\n\n  if [ ${CHKHEADER} -ne 1 ]; then\n\n    echo \"line 1 or 2 needs those special keywords in it:${FILES}\"\n\n    echo $ABORTMSG\n\n    exit 7\n\n  fi\n\n  done\n\n\n\nTHIS IS BASED ON THE ASSUMPTION THAT YOUR WORKING COPY AND INDEX ENTRY ARE \nSUPPOSED TO MATCH!  Its based on a workflow in which you commit your current \nwork.  I you want to be able to git-add a file to the index and then modify \nthe file again and then only commit what's in the index while retaining a \nworking copy that differs from the index, THEN THIS SCRIPT WON'T WORK.\n\n\n\n> As soon as I have the file names I should be able to proceed.\n\n>\n\n> How could I do this best from a shell script.\n\n>\n\n> Is there any clear documentation about hich git commands I'm allowed\n\n> to use during a trigger script and which ones I can't\n\n>\n\n\n\nKeep in mind that git hooks don't allow you to prompt the user in your \nscript.  However, the exception may be git commands that prompt the user. \nHaven't tried that combination myself yet...\n\n\n\n> Lateron I would be interested to implement a small server script, that\n\n> refuses a git push in case, that the most recent commit in a branch\n\n> would contain .h files without a certain header text\n\n>\n\n\n\nYour pre-commit hook will prevent this because you can only push commits. \nUnless you don't have control over what hooks the push-er is using in their \nrepo...  In which case there is a hook that checks push-ed content before \ncommitting it and will reject it if need be.  Can't remember what it is \noffhand.  Look at the githooks refmanual entry \nhttp://www.kernel.org/pub/software/scm/git/docs/v1.7.1.2/githooks.html for \nwhatever git version you're using and you should be able to find it.  These \nscripts would probably work in that remote-side hook.\n\n\n\nv/r,\n\nNeal\n"},{"id":"150004","messageId":"i6137p$uet$1@dough.gmane.org","threadId":"24955","inReplyTo":"i5p96s$u7q$1@dough.gmane.org","subject":"Re: simple example for git hooks","fromName":"Gelonida","fromEmail":"gelonida@gmail.com","sentAt":"2010-09-05T21:48:41Z","receivedAt":"2010-09-05T21:48:41Z","isPatch":false,"sender":{"key":"gelonida@gmail.com","avatar":null},"body":"Hi everybody,\nOn 09/03/2010 12:41 AM, Gelonida wrote:\n> Does anyone have a simple example of a git pre-commit hook\n> \n> \n> I have difficulties finding understandable tutorals about git hooks.\n> \n> \n> \n> \n> What I am looking at is basic examples about\n> \n> \n> precommit hooks\n> ================\n> \n> - how to get list of modified files\n> \nThanks for all your answers.\nI'm currently using\ngit diff-index HEAD --cached\n\nThis seems to do what I was looking for.\n"}]}