1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

added git add to experimental command set, lets you add a method to a git repo

This commit is contained in:
John Mair 2011-07-14 04:25:34 +12:00
parent 939c7f144f
commit 7386692030
2 changed files with 40 additions and 9 deletions

View file

@ -12,28 +12,30 @@ class Pry
meth.reload
end
command "blame", "Show blame for a method", :requires_gem => "grit" do |meth_name|
command "git blame", "Show blame for a method", :requires_gem => "grit" do |meth_name|
require 'grit'
if (meth = get_method_object(meth_name, target, {})).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
file_name = meth.source_location.first
code, start_line = method_code_from_head(meth)
repo ||= Grit::Repo.new(".")
start_line = meth.source_location.last
num_lines = meth.source.lines.count
authors = repo.blame(meth.source_location.first).lines.select do |v|
num_lines = code.lines.count
authors = repo.blame(file_name, repo.head.commit).lines.select do |v|
v.lineno >= start_line && v.lineno <= start_line + num_lines
end.map do |v|
v.commit.author.output(Time.new).split(/</).first.strip
end
lines_with_blame = []
meth.source.lines.zip(authors) { |line, author| lines_with_blame << ("#{author}".ljust(10) + colorize_code(line)) }
output.puts lines_with_blame.join
code.lines.zip(authors) { |line, author| lines_with_blame << ("#{author}".ljust(10) + colorize_code(line)) }
output.puts lines_with_blame.join
end
command "diff", "Show the diff for a method", :requires_gem => ["grit", "diffy"] do |meth_name|
command "git diff", "Show the diff for a method", :requires_gem => ["grit", "diffy"] do |meth_name|
require 'grit'
require 'diffy'
@ -42,7 +44,35 @@ class Pry
next
end
output.puts colorize_code(Diffy::Diff.new(method_code_from_head(meth), meth.source))
output.puts colorize_code(Diffy::Diff.new(method_code_from_head(meth).first, meth.source))
end
command "git add", "Add a method to index", :requires_gem => ["grit", "diffy"] do |meth_name|
require 'grit'
require 'tempfile'
if (meth = get_method_object(meth_name, target, {})).nil?
output.puts "Invalid method name: #{meth_name}."
next
end
repo = Grit::Repo.new('.')
file_name = relative_path(meth.source_location.first)
file_data = get_file_from_commit(file_name)
code, start_line = method_code_from_head(meth)
end_line = start_line + code.lines.count
before_code = file_data.lines.to_a[0..(start_line - 2)]
after_code = file_data.lines.to_a[end_line - 1..-1]
final_code = before_code << meth.source.lines.to_a << after_code
t = Tempfile.new("tmp")
t.write final_code.join
t.close
sha1 = `git hash-object -w #{t.path}`.chomp
system("git update-index --cacheinfo 100644 #{sha1} #{file_name}")
end
helpers do
@ -62,7 +92,7 @@ class Pry
search_line = meth.source.lines.first.strip
_, start_line = code.lines.to_a.each_with_index.find { |v, i| v.strip == search_line }
start_line
Pry.new(:input => StringIO.new(code.lines.to_a[start_line..-1].join)).r(target)
[Pry.new(:input => StringIO.new(code.lines.to_a[start_line..-1].join)).r(target), start_line + 1]
end
def relative_path(path)

View file

@ -163,6 +163,7 @@ class Pry
# @example
# Pry.new.repl(Object.new)
def repl(target=TOPLEVEL_BINDING)
target = Pry.binding_for(target)
target_self = target.eval('self')