为什么我的Ruby Git脚本钩子运行错误的$ PATH? [英] Why is my Ruby Git script hook run with the wrong $PATH?

查看:125
本文介绍了为什么我的Ruby Git脚本钩子运行错误的$ PATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RVM。我为项目写了一个Git pre-commit 钩子:

  !/ usr / bin / env ruby​​ 

放RUBY_VERSION
puts`echo $ PATH`
退出(1)
1.8.7
us / / / bin -2.0.0-p195@global/bin:/Users/mgoerlich/.rvm/rubies/ruby-2.0.0-p195/bin:/Users/mgoerlich/.rvm/bin:/用户/ mgoerlich / ADT-bundle- MAC-x86_64-20130219 / SDK /平台的工具:/用户/ mgoerlich / ADT-束-MAC-x86_64-20130219 / SDK /工具:在/ usr / local / bin目录:在/ usr / local / sbin中:/用户/ mgoerlich / .dotfiles /斌:在/ usr / bin中:/ bin中:/ usr / sbin目录:/ sbin目录:在/ usr / bin中/ core_perl:/用户/ mgoerlich /斌:在/ usr / local / share下/ NPM /斌:在/ usr /本地/ share / npm / bin

似乎运行错误的Ruby版本,因为 $ PATH 与bash或zsh或sh不同。似乎git正在操纵 $ PATH 。手动运行时,我得到这个:

  $ .git / hooks / pre-commit 
2.0.0 $ b美元/ /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

在提交钩子的输出中,前面有两个路径,其中一个 / usr / bin 其中放置了系统Ruby的可执行文件。 / p>

这是一个已知的行为吗?我可以操纵吗?我知道我可以在shebang中指定正确的Ruby版本的完整路径,但这不是我想要的。

解决方案

我不想使用 env 而不是ruby或rvm包装器的固定路径的原因是这是一个Team Project,而不是Team中的每个人都在使用RVM。



我的最终解决方案是将自己的包装脚本添加到该项目中。



所有客户端git钩子在$ code> $ PROJECT / bin / hooks 中,都是ruby脚本。
现在,我刚刚把所提到的包装器放在那里,并为所有钩子的 $ PROJECT / .git / hooks 中的包装器创建了一个符号链接。



脚本检查是否使用RVM,如果这样修复了 $ PATH var,如果有$ code> .ruby-version 和/或 .ruby-gemset 项目根中的文件加载了相应的版本/ gemset。



然后它将运行相应的ruby脚本
这是包装器,以防您感兴趣:

 #!/ bin / bash 
如果[-d$ HOME / .rvm / bin];然后
PATH =$ HOME / .rvm / bin:$ PATH
[[-s$ HOME / .rvm / scripts / rvm]]&&源$ HOME / .rvm / scripts / rvm

如果[-f.ruby-version];然后
rvm使用$(cat .ruby-version)
fi

如果[-f.ruby-gemset];然后
rvm gemset使用$(cat .ruby-gemset)
fi
fi
ruby​​bin / hooks / $(basename$ 0)。 $ b

所以,我会得到我的rvm版本/ gemset和其他所有他们在路径中的ruby版本,大家都快乐。


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)

which outputs this when run by 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 run with the wrong version of Ruby because $PATH is not the same as in bash or zsh or sh. It seems like git is manipulating $PATH. When run manually, I get this:

$ .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, there are two paths prepended, one of them /usr/bin where the system Ruby's executable is placed.

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

解决方案

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.

这篇关于为什么我的Ruby Git脚本钩子运行错误的$ PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆