This is how I detect git in ruby:
`which git 2>/dev/null` and $?.success?
However, this is not cross-platform. It fails on non-unix systems or those without the which
command (although I'm not sure what those are).
I need a way to detect git that satisfies these conditions:
- works reliably cross-platform, even on Windows
- doesn't output anything to $stdout or $stderr
- small amount of code
Update: the solution is to avoid using which
altogether and to redirect output to NUL
on Windows.
require 'rbconfig'
void = RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw/ ? 'NUL' : '/dev/null'
system "git --version >>#{void} 2>&1"
The system
command returns true on success and false on failure, saving us the trip to $?.success?
which is needed when using backticks.