Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform git hooks

How do you manage git pre/post commit hooks across various platforms (say, *nix and windows)?

Any best practices recommended for this scenario?

like image 458
Yuval Adam Avatar asked Mar 08 '11 16:03

Yuval Adam


1 Answers

The git hooks are really no different than any shell script in that the best-practices for keeping them "cross-platform" apply just the same. The minor difference is that you may also be targeting the Windows *nix emulation layers as major users of these hooks as well, and these can be comparatively crippled by comparison to most actual Unix environments.

I just ported a post-commit hook (and wrote a pre-commit hook) to the default Git for Windows, which uses MINGW32. A few of the changes I had to make since the previous version made some assumptions about the scripting environment available:

  • no readlink available, but it was ineffectual on Windows anyway, so I just hacked in a check of which readlink and no-oped if there was none
  • redirecting to /dev/null doesn't work in Windows, instead you have to redirect to nul, to do this I just did a uname -s check and set a variable for what to redirect to
  • no mktemp available, but since it was a directory that we ended up pruning anyway, we used a naively hard-coded directory name
  • for some reason, redirecting of STDOUT via exec >>$out works fine in MINGW32, but exec 2>>$err for STDERR does not (it seems to die silently), so unfortunately we just had to eschew that redirection

Other than that, it worked pretty well as-is because there wasn't a lot of platform-specific "stuff" in it. So just stick to best practices for making cross-platform scripts and that should get you at least 80% of the way there. After that, test test test in all of the various environments you're targeting.

like image 69
Daniel DiPaolo Avatar answered Oct 22 '22 00:10

Daniel DiPaolo