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

前端 未结 3 1243
旧时难觅i
旧时难觅i 2021-01-06 07:00

I\'m using RVM. I wrote a Git pre-commit hook for a project:

#!/usr/bin/env ruby

puts RUBY_VERSION
puts `echo $PATH`
exit(1)

3条回答
  •  轮回少年
    2021-01-06 07:13

    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.

提交回复
热议问题