<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-AU link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal>Hi all,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>How many of you are using client-side git hooks? That is, git hooks that are in your working directory. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>A while ago, I added a “pre-commit” git hook in my Koha git working directory, which runs a little Python script every time I make a commit. It’s nothing fancy. Basically it gets a list of all the files I’m changing in Git and for a “.pl” or a “.pm” file, it runs “perl -c", which compiles the Perl code without running it (with a caveat about BEGIN{} blocks which do get run at compile time so could be a problem if you’re running untrusted code especially as a privileged user…). The Python script gets the exit code of that operation, and if there’s an error, it uses an exit code of 1 itself and prevents the commit from happening. I’ve attached some sample code to the bottom of this email under “__PYTHON_SCRIPT__”. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I find that it’s a nice way of catching errors. Maybe you write some code and you don’t think you need to test it, or you tested it but you made a last minute change and that last minute change has a typo… this catches that kind of things – at least if it’s an error which prevents successful compilation.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Anyway, I was just porting a patch between different versions of Koha, and everything looked good at a glance and the code merge was successful, but the commit failed because one variable name in one line of the many lines changed was slightly different. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The error messages told me exactly where to go and then it was obvious what the problem was and what to do. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Anyway, I just thought I’d share that. Maybe everyone is already using client-side git hooks, but I thought I’d share just in case someone else finds it useful. Especially as it saved my bacon just now. <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>David Cook<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Systems Librarian<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Prosentient Systems<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>72/330 Wattle St<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Ultimo, NSW 2007<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Australia<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'><o:p> </o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Office: 02 9212 0899<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-AU'>Direct: 02 8005 0595<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>__PYTHON_SCRIPT__<o:p></o:p></p><p class=MsoNormal>#!/usr/bin/env python<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>import subprocess, os<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>errors = 0<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>output = subprocess.check_output(["git","diff","--cached","--name-only","--diff-filter=ACMR"])<o:p></o:p></p><p class=MsoNormal>lines = output.split('\n')<o:p></o:p></p><p class=MsoNormal>for line in lines:<o:p></o:p></p><p class=MsoNormal>    if line:<o:p></o:p></p><p class=MsoNormal>        filename, file_extension = os.path.splitext(line)<o:p></o:p></p><p class=MsoNormal>        print(filename)<o:p></o:p></p><p class=MsoNormal>        print(file_extension)<o:p></o:p></p><p class=MsoNormal>        if file_extension == ".pl" or file_extension == ".pm":<o:p></o:p></p><p class=MsoNormal>            rv = subprocess.call(["perl","-c",line])<o:p></o:p></p><p class=MsoNormal>            if rv:<o:p></o:p></p><p class=MsoNormal>                errors += 1<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>if errors > 0:<o:p></o:p></p><p class=MsoNormal>    exit(1)<o:p></o:p></p></div></body></html>