Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unversioned/local hooks #527

Closed
harto opened this issue Dec 1, 2017 · 4 comments
Closed

Support unversioned/local hooks #527

harto opened this issue Dec 1, 2017 · 4 comments

Comments

@harto
Copy link
Contributor

harto commented Dec 1, 2017

I was wondering if there's a generalized way to run hooks that are specific to my development environment. For example, I'd like a post-checkout hook that calls ctags with a specific configuration, but many engineers on my team don't use ctags.

One thought I had was to implement a hook that could be configured to look for unversioned hooks in a specified directory. I.e. maybe something like:

PostCheckout:
  Local:
    path: ".unversioned-git-hooks"

Then if <repo>/.unversioned-git-hooks contained post-checkout, etc. they would be run after regular Overcommit hooks. And /.unversioned-git-hooks could be added to .gitignore.

@harto
Copy link
Contributor Author

harto commented Dec 1, 2017

Actually, maybe the above makes more sense as a kind of global configuration setting. But anyway, that's one idea. :)

@harto
Copy link
Contributor Author

harto commented Feb 1, 2018

I figured out a way to do this.

I added a bunch of hook definitions like this:

PostCheckout:
  Local:
    enabled: true
    required_executable: ".git-hooks/local/post-checkout"

PostCommit:
  Local:
    enabled: true
    required_executable: ".git-hooks/local/post-commit"

Then I check those files into version control. They look like e.g.:

#!/usr/bin/env bash
if [[ -f .local-git-hooks/post-checkout ]]; then
  exec .local-git-hooks/post-checkout $*
fi

Then, I can optionally create hooks in .local-git-hooks, which is added to .gitignore.

@harto harto closed this as completed Feb 1, 2018
@sds
Copy link
Owner

sds commented Feb 1, 2018

Thanks for sharing, @harto. This is a great workaround.

On small point: careful with the use of $* in scripts. This returns all arguments passed to the script as a single string, but you want an array of arguments. Thus you should use $@.

#!/usr/bin/env bash
if [[ -f .local-git-hooks/post-checkout ]]; then
  exec .local-git-hooks/post-checkout "$@"
fi

Yes, the double quotes are important as well. Consider the following two scripts:

for arg in "$@"; do echo $arg; done
for arg in $@; do echo $arg; done

Running ./script one two three "arg with spaces" for each respectively:

"$@"

one
two
three
arg with spaces

$@

one
two
three
arg
with
spaces

Hope this helps!

@harto
Copy link
Contributor Author

harto commented Feb 1, 2018

Oh, nice. Thanks for the bash tip! (I always forget how to quote things properly...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants