Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an env variable from post-checkout hook in git

I've got a work environment in which I frequently build binaries and then kick off long-running processes using that binary. While these are running, I would like to switch to another git branch and do some work there, and potentially build some new binaries that wouldn't overwrite the ones currently running.

Right now, the build and run scripts use a $OUT_DIR env variable to determine the output dir for binaries. Ideally, I would like to have $OUT_DIR be branch-specific to avoid the above mentioned problem of builds overwriting each other.
At first, this seemed like a simple job for the post-checkout hook -- set OUT_DIR to be OUT_DIR_BASE + "/" + current branch... except post-checkout hook runs in a child shell so it can't set parent env variables.

Are there any clever ways to get around this?

Some ideas I've considered:

  • I could output the "export OUT_DIR..." command to a file and make the user source it -- that's annoying and people will forget to do it.
  • I could write the new OUT_DIR into an .rc file and have build/run scripts pick it up from there, but that requires changes to the build environment, and user experience gets a bit worse (i.e. you can't do "cd $OUT_DIR" to get into the output directory...
  • I could write a wrapper for 'git checkout' that would open a new shell with OUT_DIR set appropriately... but that's "leaking" shells where over time I'd have quite a large number running.

None of these seem very nice for what feels like should be an easy solution.

like image 462
Stan Avatar asked Nov 01 '22 15:11

Stan


1 Answers

I could print the branch name into a file and have build/run scripts pick it up from there

That remains the way to explore, even though it involve modifying the build script.

But getting the current branch name for a repo isn't hard:

git -C /path/to/repo rev-parse --abbrev-ref HEAD

So you wouldn't need to write the name of the current branch: the build script could get it directly from the repo.

like image 158
VonC Avatar answered Nov 09 '22 20:11

VonC