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

I am using RVM. I wrote a Git pre-commit hook for the project:

 #!/usr/bin/env ruby puts RUBY_VERSION puts `echo $PATH` exit(1) 

which outputs this when starting git:

 $ git ci -m 'foo' 1.8.7 /usr/libexec/git-core:/usr/bin:/usr/local/heroku/bin:/Users/mgoerlich/.rvm/gems/ ruby-2.0.0-p195@global /bin:/Users/mgoerlich/.rvm/rubies/ruby-2.0.0-p195/bin:/Users/mgoerlich/.rvm/bin:/Users/mgoerlich/adt-bundle-mac-x86_64-20130219/sdk/platform-tools:/Users/mgoerlich/adt-bundle-mac-x86_64-20130219/sdk/tools:/usr/local/bin:/usr/local/sbin:/Users/mgoerlich/.dotfiles/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin/core_perl:/Users/mgoerlich/bin:/usr/local/share/npm/bin:/usr/local/share/npm/bin 

It seems to work with the wrong version of Ruby, because $PATH does not match bash or zsh or sh. Git seems to be manipulating $PATH . When starting manually, I get the following:

 $ .git/hooks/pre-commit 2.0.0 /usr/local/heroku/bin:/Users/mgoerlich/.rvm/gems/ ruby-2.0.0-p195@global /bin:/Users/mgoerlich/.rvm/rubies/ruby-2.0.0-p195/bin:/Users/mgoerlich/.rvm/bin:/Users/mgoerlich/adt-bundle-mac-x86_64-20130219/sdk/platform-tools:/Users/mgoerlich/adt-bundle-mac-x86_64-20130219/sdk/tools:/usr/local/bin:/usr/local/sbin:/Users/mgoerlich/.dotfiles/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin/core_perl:/Users/mgoerlich/bin:/usr/local/share/npm/bin:/usr/local/share/npm/bin 

In the output of the commit hook, two paths are added: one of them is /usr/bin , where the Ruby system executable is located.

Is this a known behavior? Can I somehow manipulate this? I know that I can point the full path to the correct Ruby version in shebang, but this is not what I want.

+4
source share
3 answers

The reason I didn’t want to use env instead of a fixed path to ruby ​​or rvm wrapper was because it was for Team Project, and not everyone in the team uses RVM.

My final decision was to write my own shell script to add it to this project.

All client git hooks' live in $PROJECT/bin/hooks , all of them are ruby ​​scripts. Now I just placed the specified shell and created a symbolic link to this shell in $PROJECT/.git/hooks for all the hooks.

script to check if RVM is used, and if that fixes $PATH var and if there are .ruby-version and / or .ruby-gemset files in the project root, then it loads the corresponding version / gemset.

Then it will run the corresponding ruby ​​script. Here is a wrapper in case you are 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 will get my version of rgm / gemset and everyone else the ruby ​​version that they have in PATH, and everyone is happy.

+3
source

you need to install ruby ​​in the shell:

 #!$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 ne shebang will look like this:

 #!$rvm_path/wrappers/git_hooks/ruby 

In the file, just replace $rvm_path with /Users/mgoerlich/.rvm so that it looks like this:

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

What I ended up with is: .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 THE_GOOD_PATH :

 export THE_GOOD_PATH="$PATH" git commit 

You can also export THE_GOOD_PATH="$PATH" from your .profile or top level of your application and symbolize all the bindings to a single file.

This method has the advantage that rbenv is agnostic: it also works with RVM or Python virtual machines.

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 original answer was WONTFIX.

0
source

Source: https://habr.com/ru/post/1490166/


All Articles