简体   繁体   中英

Is it possible to call Git or other command line tools from inside a Thor script?

I find that I'm often running a sequence of routine 'cleanup' tasks before and after I make a git commit for my Rails 3 app.

I was thinking about putting these things into a Thor script, but one thing I haven't been able to figure out is how to use Thor (or Rake) to call other tools on the system.

Is it possible to call a command like git log from a Thor or Rake script, and if so what does that look like?

Thanks!

Just shell out:

result = %x(git log)
puts result

or

system('git log')

if you just want to pass the output to the terminal.

There is also the grit gem that abstracts the Git tools into a Ruby library:

require 'grit'
repo = Grit::Repo.new("/path/to/repo")
repo.commits.each do |commit|
  puts "#{commit.id}: #{commit.message}"
end

不要忘记那些只是Ruby文件,所以你也可以在那里使用Ruby库中的所有东西,所以像%x [rm -rf /], system (“rm -rf /”)和`rm -rf这样的东西/`也可以在这些脚本中访问。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM