From 6684afe506181c1436582042c6d23288f9c5838d Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 13 Jan 2011 00:02:49 -0800 Subject: [PATCH] replace the old #raw_git stuff with #native calls for the git patching methods --- lib/grit/git.rb | 56 ++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/grit/git.rb b/lib/grit/git.rb index eb60b17..c84c3a4 100644 --- a/lib/grit/git.rb +++ b/lib/grit/git.rb @@ -180,54 +180,38 @@ module Grit def check_applies(head_sha, applies_sha) git_index = create_tempfile('index', true) - (o1, exit1) = raw_git("git read-tree #{head_sha} 2>/dev/null", git_index) - (o2, exit2) = raw_git("git diff #{applies_sha}^ #{applies_sha} | git apply --check --cached >/dev/null 2>/dev/null", git_index) - return (exit1 + exit2) + options = {:env => {'GIT_INDEX_FILE' => git_index}, :raise => true} + status = 0 + begin + native(:read_tree, options.dup, head_sha) + stdin = native(:diff, options.dup, "#{applies_sha}^", applies_sha) + native(:apply, options.merge(:check => true, :cached => true, :input => stdin)) + rescue CommandFailed + status += 1 + end + status end def get_patch(applies_sha) git_index = create_tempfile('index', true) - (patch, exit2) = raw_git("git diff #{applies_sha}^ #{applies_sha}", git_index) - patch + native(:diff, { + :env => {'GIT_INDEX_FILE' => git_index}}, + "#{applies_sha}^", applies_sha) end def apply_patch(head_sha, patch) git_index = create_tempfile('index', true) - git_patch = create_tempfile('patch') - File.open(git_patch, 'w+') { |f| f.print patch } - - raw_git("git read-tree #{head_sha} 2>/dev/null", git_index) - (op, exit) = raw_git("git apply --cached < #{git_patch}", git_index) - if exit == 0 - return raw_git("git write-tree", git_index).first.chomp + options = {:env => {'GIT_INDEX_FILE' => git_index}, :raise => true} + begin + native(:read_tree, options.dup, head_sha) + native(:apply, options.merge(:cached => true, :input => patch)) + rescue CommandFailed + return false end - false + native(:write_tree, :env => options[:env]).to_s.chomp! end - # RAW CALLS WITH ENV SETTINGS - def raw_git_call(command, index) - tmp = ENV['GIT_INDEX_FILE'] - ENV['GIT_INDEX_FILE'] = index - out = `#{command}` - after = ENV['GIT_INDEX_FILE'] # someone fucking with us ?? - ENV['GIT_INDEX_FILE'] = tmp - if after != index - raise 'environment was changed for the git call' - end - [out, $?.exitstatus] - end - - def raw_git(command, index) - output = nil - Dir.chdir(self.git_dir) do - output = raw_git_call(command, index) - end - output - end - # RAW CALLS WITH ENV SETTINGS END - - # Execute a git command, bypassing any library implementation. # # cmd - The name of the git command as a Symbol. Underscores are