diff --git a/lib/gitrepo.rb b/lib/gitrepo.rb index a04d8ad..eb90286 100644 --- a/lib/gitrepo.rb +++ b/lib/gitrepo.rb @@ -39,6 +39,28 @@ def root @root end + def git *args + args = args.map { |a| a.to_s } + Dir.chdir(@root) do + out = IO.popen('-', 'r') do |io| + if io + # parent, read the git output + block_given? ? yield(io) : io.read + else + STDERR.reopen STDOUT + exec 'git', *args + end + end + + if $?.exitstatus > 0 + # return '' if $?.exitstatus == 1 && out == '' + raise GitError.new("git #{args.join(' ')}: #{out}") + end + + out + end + end + # i.e. remote_add 'rails', 'http://github.com/rails/rails.git' def remote_add name, remote Dir.chdir(@root) { diff --git a/spec/gitrepo_spec.rb b/spec/gitrepo_spec.rb index 2636776..ea90b73 100644 --- a/spec/gitrepo_spec.rb +++ b/spec/gitrepo_spec.rb @@ -36,7 +36,10 @@ def with_git_commit *args it "should allow remotes to be added and removed" do with_git_commit(:bare => true) do |repo| - + repo.remote_add :origin, 'http://example.com/' + repo.git(:remote).should == "origin\n" + repo.remote_remove :origin + repo.git(:remote).should == "" end end end