lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Writing a git pre-commit hook


From: Vadim Zeitlin
Subject: Re: [lmi] Writing a git pre-commit hook
Date: Wed, 9 Nov 2016 10:55:31 +0100

On Tue, 8 Nov 2016 22:13:57 +0000 Greg Chicares <address@hidden> wrote:

GC> However, when I let 'git commit' run it, it failed to navigate the
GC> subdirectories:

 I can explain the problem, but I'm not completely sure what is the best
solution. The explanation first: when git runs its hooks, it sets a few
environment variables for them, such as (for pre-commit hook at least)
GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL. It also explicitly sets GIT_DIR and,
for whatever reason, it sets it to a relative path, i.e. typically
"GIT_DIR=.git". So, in essence, you end up running "GIT_DIR=.git g
rev-parse --show-toplevel" in a subdirectory and if you run the same
command from the same location in an interactive shell, you're going to get
the same output, i.e. "fatal: Not a git repository: '.git'".

GC> This article:
GC>   http://stackoverflow.com/questions/11512155/how-to-test-git-hooks
GC> suggests adding 'set -x' inside the script (good idea), then running
GC> it in a throwaway clone of the repository (yick--I'd rather commit
GC> a nonsensical change and then 'reset --hard'). But I stumbled upon a
GC> method I like better: just run 'git commit' in a clean repository;
GC> that runs the hook and then commits nothing.

 How interesting, I didn't know that pre-commit hook was run even if there
was nothing to commit, thanks.

GC> when I figured that if I was going to go to all that trouble, I should
GC> first refactor:
GC> 
GC> + toplevel=$(git rev-parse --show-toplevel)
GC> - cd $(git rev-parse --show-toplevel)/src  && check_concinnity
GC> + cd $toplevel/src  && check_concinnity
GC> - cd $(git rev-parse --show-toplevel)/data && check_concinnity
GC> - cd $(git rev-parse --show-toplevel)/test && check_concinnity
GC> + [...similarly]

 As now you run "git rev-parse --show-toplevel" from the top level
directory, where GIT_DIR=.git is correct, it's not surprising it works. I
still don't really like it though, it seems like it's going to be too
simple to break it again accidentally and I think it's more complicated
than necessary. I'd just do:

        (cd src && check_concinnity)
        (cd data && check_concinnity)
        (cd test && check_concinnity)

GC> so, whatever the ultimate cause, that's a reproducible problem.

 And I hope to have explained it but please let me know if you still have
any questions.

 Good luck (with this and for the next 4 years),
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]