<div dir="ltr"><div>I shared my pre-commit git hook years ago (2012)<br></div><div><a href="https://wiki.koha-community.org/wiki/Tips_and_tricks" target="_blank">https://wiki.koha-community.org/wiki/Tips_and_tricks</a></div><div>It does -wc and much more...<br></div><div dir="ltr"><div><br><div class="gmail_quote"><div dir="ltr">On Fri, 20 Jul 2018 at 05:53 David Cook <<a href="mailto:dcook@prosentient.com.au" target="_blank">dcook@prosentient.com.au</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div link="#0563C1" vlink="#954F72" lang="EN-AU"><div class="m_767925265239840002m_-1834264677431341903WordSection1"><p class="MsoNormal">Hi all,<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">How many of you are using client-side git hooks? That is, git hooks that are in your working directory. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></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__”. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></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.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></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. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></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. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></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. <u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><span>David Cook<u></u><u></u></span></p><p class="MsoNormal"><span>Systems Librarian<u></u><u></u></span></p><p class="MsoNormal"><span>Prosentient Systems<u></u><u></u></span></p><p class="MsoNormal"><span>72/330 Wattle St<u></u><u></u></span></p><p class="MsoNormal"><span>Ultimo, NSW 2007<u></u><u></u></span></p><p class="MsoNormal"><span>Australia<u></u><u></u></span></p><p class="MsoNormal"><span><u></u> <u></u></span></p><p class="MsoNormal"><span>Office: <a href="tel:02%2092%2012%2008%2099" value="+33292120899" target="_blank">02 9212 0899</a><u></u><u></u></span></p><p class="MsoNormal"><span>Direct: <a href="tel:02%2080%2005%2005%2095" value="+33280050595" target="_blank">02 8005 0595</a><u></u><u></u></span></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">__PYTHON_SCRIPT__<u></u><u></u></p><p class="MsoNormal">#!/usr/bin/env python<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">import subprocess, os<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">errors = 0<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">output = subprocess.check_output(["git","diff","--cached","--name-only","--diff-filter=ACMR"])<u></u><u></u></p><p class="MsoNormal">lines = output.split('\n')<u></u><u></u></p><p class="MsoNormal">for line in lines:<u></u><u></u></p><p class="MsoNormal">    if line:<u></u><u></u></p><p class="MsoNormal">        filename, file_extension = os.path.splitext(line)<u></u><u></u></p><p class="MsoNormal">        print(filename)<u></u><u></u></p><p class="MsoNormal">        print(file_extension)<u></u><u></u></p><p class="MsoNormal">        if file_extension == ".pl" or file_extension == ".pm":<u></u><u></u></p><p class="MsoNormal">            rv = subprocess.call(["perl","-c",line])<u></u><u></u></p><p class="MsoNormal">            if rv:<u></u><u></u></p><p class="MsoNormal">                errors += 1<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">if errors > 0:<u></u><u></u></p><p class="MsoNormal">    exit(1)<u></u><u></u></p></div></div>_______________________________________________<br>
Koha-devel mailing list<br>
<a href="mailto:Koha-devel@lists.koha-community.org" target="_blank">Koha-devel@lists.koha-community.org</a><br>
<a href="http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel" rel="noreferrer" target="_blank">http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel</a><br>
website : <a href="http://www.koha-community.org/" rel="noreferrer" target="_blank">http://www.koha-community.org/</a><br>
git : <a href="http://git.koha-community.org/" rel="noreferrer" target="_blank">http://git.koha-community.org/</a><br>
bugs : <a href="http://bugs.koha-community.org/" rel="noreferrer" target="_blank">http://bugs.koha-community.org/</a></blockquote></div></div></div></div>