Skip to content

Commit

Permalink
Grit::Git check_applies / patch related methods take command hash
Browse files Browse the repository at this point in the history
This lets us pass an :env so we can use GIT_ALTERNATE_OBJECT_DIRECTORIES
to check if a commit applies across repositories.
  • Loading branch information
rtomayko committed Jun 22, 2011
1 parent f19c39b commit e116026
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/grit/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,23 @@ def commit_from_sha(id)

# Checks if the patch of a commit can be applied to the given head.
#
# options - grit command options hash
# head_sha - String SHA or ref to check the patch against.
# applies_sha - String SHA of the patch. The patch itself is retrieved
# with #get_patch.
#
# Returns 0 if the patch applies cleanly (according to `git apply`), or
# an Integer that is the sum of the failed exit statuses.
def check_applies(head_sha, applies_sha)
def check_applies(options={}, head_sha=nil, applies_sha=nil)
options, head_sha, applies_sha = {}, options, head_sha if !options.is_a?(Hash)
options = options.dup
options[:env] &&= options[:env].dup

git_index = create_tempfile('index', true)
options = {:env => {'GIT_INDEX_FILE' => git_index}, :raise => true}
status = 0
(options[:env] ||= {}).merge!('GIT_INDEX_FILE' => git_index)
options[:raise] = true

status = 0
begin
native(:read_tree, options.dup, head_sha)
stdin = native(:diff, options.dup, "#{applies_sha}^", applies_sha)
Expand All @@ -219,27 +226,38 @@ def check_applies(head_sha, applies_sha)

# Gets a patch for a given SHA using `git diff`.
#
# options - grit command options hash
# applies_sha - String SHA to get the patch from, using this command:
# `git diff #{applies_sha}^ #{applies_sha}`
#
# Returns the String patch from `git diff`.
def get_patch(applies_sha)
def get_patch(options={}, applies_sha=nil)
options, applies_sha = {}, options if !options.is_a?(Hash)
options = options.dup
options[:env] &&= options[:env].dup

git_index = create_tempfile('index', true)
native(:diff, {
:env => {'GIT_INDEX_FILE' => git_index}},
"#{applies_sha}^", applies_sha)
(options[:env] ||= {}).merge!('GIT_INDEX_FILE' => git_index)

native(:diff, options, "#{applies_sha}^", applies_sha)
end

# Applies the given patch against the given SHA of the current repo.
#
# options - grit command hash
# head_sha - String SHA or ref to apply the patch to.
# patch - The String patch to apply. Get this from #get_patch.
#
# Returns the String Tree SHA on a successful patch application, or false.
def apply_patch(head_sha, patch)
def apply_patch(options={}, head_sha=nil, patch=nil)
options, head_sha, patch = {}, options, head_sha if !options.is_a?(Hash)
options = options.dup
options[:env] &&= options[:env].dup
options[:raise] = true

git_index = create_tempfile('index', true)
(options[:env] ||= {}).merge!('GIT_INDEX_FILE' => git_index)

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))
Expand Down

0 comments on commit e116026

Please sign in to comment.