Module: PryGit

Defined in:
lib/pry-git.rb,
lib/pry-git/version.rb

Defined Under Namespace

Modules: GitHelpers

Constant Summary collapse

Commands =
Pry::CommandSet.new do

  create_command "git blame", "Show blame for a method (for method in HEAD)" do
    include GitHelpers

    def options(opt)
      method_options(opt)
    end

    def process
      raise CommandError, "Could not find method source" unless method_object.source

      meth = method_object

      file_name = meth.source_location.first
      code, start_line = method_code_from_head(meth)

      git_root = find_git_root(File.dirname(file_name))

      repo = Grit::Repo.new(git_root)
      num_lines = code.lines.count
      authors = repo.blame(relative_path(git_root, 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 = []
      code.lines.zip(authors) { |line, author| lines_with_blame << ("#{author}".ljust(20) + colorize_code(line)) }
      output.puts lines_with_blame.join
    end
  end

  create_command "git diff", "Show the diff for a method (working directory vs HEAD)" do
    include GitHelpers

    def options(opt)
      method_options(opt)
    end

    def process
      output.puts colorize_code(Diffy::Diff.new(method_code_from_head(method_object).first, method_object.instance_variable_get(:@method).source))
    end
  end

  create_command "git add", "Add a method to index" do
    include GitHelpers

    def options(opt)
      method_options(opt)
    end

    def process
      meth = method_object.instance_variable_get(:@method)

      file_name = meth.source_location.first

      git_root = find_git_root(File.dirname(file_name))
      repo = Grit::Repo.new(git_root)

      rel_file_name = relative_path(git_root, 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} #{rel_file_name}")
    end
  end

end
VERSION =
"0.2.3"