Why is my Ruby Git script hook run with the wrong $PATH?

点点圈 提交于 2019-11-30 22:40:11

The reason i didn't wanted to use env instead of a fixed path to ruby or a rvm wrapper was that this is for a Team Project and not everyone in the Team is using RVM.

My final solution was to write my own wrapper script an add it to that project.

All client-side git hooks 're living in $PROJECT/bin/hooks, all of them ruby scripts. Now, i've just put that mentioned wrapper in there, and created a symlink to that wrapper in $PROJECT/.git/hooks for all the hooks.

The script check's if RVM is used and if so fixes the $PATH var and if there are .ruby-version and/or .ruby-gemset files in the project root it loads the according version/gemset.

Then it'll run the according ruby script Here's the wrapper in case you're interested:

#!/bin/bash
if [ -d "$HOME/.rvm/bin" ]; then
  PATH="$HOME/.rvm/bin:$PATH"
  [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

  if [ -f ".ruby-version" ]; then
    rvm use "$(cat .ruby-version)"
  fi

  if [ -f ".ruby-gemset" ]; then
    rvm gemset use "$(cat .ruby-gemset)"
  fi
fi
ruby "bin/hooks/$(basename "$0").rb"

So, i'll get my rvm version/gemset and everybody else the ruby version they have in their PATH, and everyone is Happy.

you need to set ruby to a wrapper:

#!$rvm_path/wrappers/ruby-2.0.0-p195/ruby

You can simplify it with an alias:

rvm alias create git_hooks 2.0.0-p195

And then the ne shebang will look like this:

#!$rvm_path/wrappers/git_hooks/ruby

In the file just make sure to replace $rvm_path with /Users/mgoerlich/.rvm so finally it looks like:

#!/Users/mgoerlich/.rvm/wrappers/git_hooks/ruby

What I ended up doing is: the .git file structure:

  • .git/hooks/pre-commit
  • .git/hooks/pre-commit-main.rb

.git/hooks/pre-commit:

#!/usr/bin/env bash
export PATH="$THE_GOOD_PATH"
ruby "$GIT_DIR/hooks/pre-commit-main.rb"

.git/hooks/pre-commit-main.rb:

#!/usr/bin/env ruby
puts RUBY_VERSION

Then, when you call git commit, make sure that THE_GOOD_PATH, is defined:

export THE_GOOD_PATH="$PATH"
git commit

You could also export THE_GOOD_PATH="$PATH" from your .profile or the toplevel of your application and symlink all hooks to a single file.

This method has the advantage of being rbenv agnostic: it also works with RVM or Python virtualenv.

I wrote to the Git developers at: http://permalink.gmane.org/gmane.comp.version-control.git/258454 asking them to leave our PATH alone, but the initial reply was WONTFIX.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!