refactor patch methods, remove #raw_git \m/

This commit is contained in:
rick 2010-12-08 17:41:13 -08:00
Родитель a6470558f0
Коммит bb86f83590
2 изменённых файлов: 66 добавлений и 35 удалений

Просмотреть файл

@ -160,11 +160,18 @@ module Grit
# Returns the exit status of the commands. Anything above 0 means there
# was an error.
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)
return exit1 if exit1 != 0
(o2, exit2) = raw_git("git diff #{applies_sha}^ #{applies_sha} | git apply --check --cached >/dev/null 2>/dev/null", git_index)
return (exit1 + exit2)
check_patch_applies(head_sha, get_patch(applies_sha))
end
# Checks if a diff applies against the HEAD of the current repository.
#
# head_sha - String HEAD SHA of the repository.
# patch - String patch to apply (see #get_patch)
#
# Returns the exit status of the commands. Anything above 0 means there
# was an error.
def check_patch_applies(head_sha, patch)
apply_patch(head_sha, patch, :check => true) ? 0 : 1
end
# Gets the patch for a given commit.
@ -192,18 +199,26 @@ module Grit
#
# Returns a String SHA of the written tree, or false if the patch did not
# apply cleanly.
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
def apply_patch(head_sha, patch, options = {})
options[:cached] = true
tree_sha = nil
with_custom_index do
native(:read_tree, {}, head_sha)
cmd = sh_command('', :apply, '', options, [])
(ret, err) = sh(cmd) do |stdin|
stdin << patch
stdin.close
end
if err =~ /error/
tree_sha = false
elsif options[:check]
tree_sha = true
else
tree_sha = native(:write_tree)
tree_sha.strip!
end
end
false
tree_sha
end
# Run the given git command with the specified arguments and return
@ -230,7 +245,17 @@ module Grit
def run(prefix, cmd, postfix, options, args, &block)
timeout = options.delete(:timeout) rescue nil
timeout = true if timeout.nil?
call = sh_command(prefix, cmd, postfix, options, args)
Grit.log(call) if Grit.debug
response, err = timeout ? sh(call, &block) : wild_sh(call, &block)
if Grit.debug
Grit.log(response)
Grit.log(err)
end
response
end
def sh_command(prefix, cmd, postfix, options, args)
base = options.delete(:base) rescue nil
base = true if base.nil?
@ -239,17 +264,12 @@ module Grit
if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|bccwin/
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|' || Grit.no_quote) ? a : "\"#{e(a)}\"" }
gitdir = base ? "--git-dir=\"#{self.git_dir}\"" : ""
call = "#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
"#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
else
ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|' || Grit.no_quote) ? a : "'#{e(a)}'" }
gitdir = base ? "--git-dir='#{self.git_dir}'" : ""
call = "#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
"#{prefix}#{Git.git_binary} #{gitdir} #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
end
Grit.log(call) if Grit.debug
response, err = timeout ? sh(call, &block) : wild_sh(call, &block)
Grit.log(response) if Grit.debug
Grit.log(err) if Grit.debug
response
end
def sh(command, &block)
@ -344,15 +364,5 @@ module Grit
ensure
ENV['GIT_INDEX_FILE'] = tmp
end
def raw_git(command, index)
out = nil
Dir.chdir(self.git_dir) do
with_custom_index(index) do
out = `#{command}`
end
end
[out, $?.exitstatus]
end
end # Git
end # Grit

Просмотреть файл

@ -4,8 +4,11 @@ class TestPatch < Test::Unit::TestCase
def setup
@path = cloned_testpath('revert.git')
@r = Repo.new(@path)
@master = '7c45b5f16ff3bae2a0063191ef832701214d4df5'
@cherry = '73bd3b5e44af956b2e0d64d7a2ee5931396c31e3'
@master = '7c45b5f16ff3bae2a0063191ef832701214d4df5'
@cherry = '73bd3b5e44af956b2e0d64d7a2ee5931396c31e3'
@spam_a_1 = '302a5491a9a5ba12c7652ac831a44961afa312d2'
@spam_a_2 = 'b26b791cb7917c4f37dd9cb4d1e0efb24ac4d26f'
@spam_b = @master
end
def teardown
@ -24,6 +27,16 @@ class TestPatch < Test::Unit::TestCase
def test_check_applies
assert_equal 0, @r.git.check_applies(@master, @cherry)
assert_equal 1, @r.git.check_applies(@master, @spam_a_1)
end
def test_check_patch_applies
revert_master = @r.git.get_patch(@master, :R => true)
revert_spam_a = @r.git.get_patch(@spam_a_1, :R => true)
revert_spams = @r.git.get_patch("#{@spam_a_1}^", @spam_a_2, :R => true)
assert_equal 0, @r.git.check_patch_applies(@master, revert_master)
assert_equal 1, @r.git.check_patch_applies(@master, revert_spam_a)
assert_equal 0, @r.git.check_patch_applies(@master, revert_spams)
end
def test_apply_patch
@ -33,4 +46,12 @@ class TestPatch < Test::Unit::TestCase
blob = tree / 'B.md'
assert_match /^INITIAL\!/, blob.data
end
def test_apply_multiple_reverts
patch = @r.git.get_patch("#{@spam_a_1}^", @spam_a_2, :R => true)
tree_sha = @r.git.apply_patch(@master, patch)
tree = Grit::Tree.create(@r, :id => tree_sha)
blob = tree / 'A.md'
assert_equal "INITIAL", blob.data.strip
end
end