Client-side git hooks

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Client-side git hooks

David Cook

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
[hidden email]
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/
Reply | Threaded
Open this post in threaded view
|

Re: Client-side git hooks

MartinRenvoize
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: [hidden email]

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 [hidden email]




On Fri, 20 Jul 2018 at 09:53, David Cook <[hidden email]> 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
[hidden email]
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/

_______________________________________________
Koha-devel mailing list
[hidden email]
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/
Reply | Threaded
Open this post in threaded view
|

Re: Client-side git hooks

Tomas Cohen Arazi
Maybe run the QA script should be one of them?

El vie., 20 de jul. de 2018 8:46 a. m., Martin Renvoize <[hidden email]> escribió:
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: [hidden email]

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 [hidden email]




On Fri, 20 Jul 2018 at 09:53, David Cook <[hidden email]> 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
[hidden email]
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/
_______________________________________________
Koha-devel mailing list
[hidden email]
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/
--
Tomás Cohen Arazi
Theke Solutions (https://theke.io)
✆ +54 9351 3513384
GPG: B2F3C15F

_______________________________________________
Koha-devel mailing list
[hidden email]
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/
Reply | Threaded
Open this post in threaded view
|

Re: Client-side git hooks

Jonathan Druart
In reply to this post by David Cook
I shared my pre-commit git hook years ago (2012)
It does -wc and much more...

On Fri, 20 Jul 2018 at 05:53 David Cook <[hidden email]> 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: <a href="tel:02%2092%2012%2008%2099" value="+33292120899" target="_blank">02 9212 0899

Direct: <a href="tel:02%2080%2005%2005%2095" value="+33280050595" target="_blank">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
[hidden email]
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/

_______________________________________________
Koha-devel mailing list
[hidden email]
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/
Reply | Threaded
Open this post in threaded view
|

Re: Client-side git hooks

barton
In reply to this post by Tomas Cohen Arazi


On Fri, Jul 20, 2018 at 8:07 AM, Tomas Cohen Arazi <[hidden email]> wrote:
Maybe run the QA script should be one of them?

+1


_______________________________________________
Koha-devel mailing list
[hidden email]
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/