Skip to content

Commit

Permalink
replace the old #raw_git stuff with #native calls for the git patchin…
Browse files Browse the repository at this point in the history
…g methods
  • Loading branch information
technoweenie committed Jan 13, 2011
1 parent 6ce0e43 commit 6684afe
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions lib/grit/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,54 +180,38 @@ def commit_from_sha(id)

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
end
false
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'
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
[out, $?.exitstatus]
native(:write_tree, :env => options[:env]).to_s.chomp!
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
Expand Down

0 comments on commit 6684afe

Please sign in to comment.