[Koha-devel] Client-side git hooks

Martin Renvoize martin.renvoize at ptfs-europe.com
Fri Jul 20 13:45:39 CEST 2018


I use git-hooks pretty extensively though I'm still porting many of them
from the project I've been working on for the last year for use with Koha.

There's so much you can do, for instance, I used to always run
perltidy over any changed files in my commit to ensuring I stayed tidy
(won't be porting that one).

The compile check is a great one too.. perhaps we should share more of
these on the wiki?  There are a few in the release management and
maintenance pages, but there's the scope that we could improve everyone's
coding by suggesting some (I used to also run a perl lint script that would
point out all my bad habbit and ensure I conformed to perl best practices..
it was a great way to learn and improve my perl).

*Martin Renvoize*

Development Manager




*T:* +44 (0) 1483 378728

*F:* +44 (0) 800 756 6384

*E:* martin.renvoize at ptfs-europe.com

www.ptfs-europe.com



<https://www.ptfs-europe.com>



Registered in the United Kingdom No. 06416372   VAT Reg No. 925 7211 30

The information contained in this email message may be privileged,
confidential and protected from disclosure. If you are not the intended
recipient, any dissemination, distribution or copying is strictly
prohibited. If you think that you have received this email message in
error, please email the sender at info at ptfs-europe.com



On Fri, 20 Jul 2018 at 09:53, David Cook <dcook at prosentient.com.au> wrote:

> 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-filter=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)
> _______________________________________________
> Koha-devel mailing list
> Koha-devel at lists.koha-community.org
> http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel
> website : http://www.koha-community.org/
> git : http://git.koha-community.org/
> bugs : http://bugs.koha-community.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.koha-community.org/pipermail/koha-devel/attachments/20180720/160aa9b9/attachment.html>


More information about the Koha-devel mailing list