[Koha-devel] Client-side git hooks

David Cook dcook at prosentient.com.au
Fri Jul 20 10:53:03 CEST 2018


Hi all,

 

How many of you are using client-side git hooks? That is, git hooks that are
in your working directory. 

 

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__". 

 

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.

 

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. 

 

The error messages told me exactly where to go and then it was obvious what
the problem was and what to do. 

 

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. 

 

David Cook

Systems Librarian

Prosentient Systems

72/330 Wattle St

Ultimo, NSW 2007

Australia

 

Office: 02 9212 0899

Direct: 02 8005 0595

 

__PYTHON_SCRIPT__

#!/usr/bin/env python

 

import subprocess, os

 

errors = 0

 

output =
subprocess.check_output(["git","diff","--cached","--name-only","--diff-filte
r=ACMR"])

lines = output.split('\n')

for line in lines:

    if line:

        filename, file_extension = os.path.splitext(line)

        print(filename)

        print(file_extension)

        if file_extension == ".pl" or file_extension == ".pm":

            rv = subprocess.call(["perl","-c",line])

            if rv:

                errors += 1

 

if errors > 0:

    exit(1)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.koha-community.org/pipermail/koha-devel/attachments/20180720/0b28292d/attachment.html>


More information about the Koha-devel mailing list