Module: PryGit::GitHelpers

Defined in:
lib/pry-git.rb

Instance Method Summary collapse

Instance Method Details

#find_git_root(dir) ⇒ Object

return the git top level for a give directory



38
39
40
41
42
43
44
45
46
# File 'lib/pry-git.rb', line 38

def find_git_root(dir)
  git_root = "."
  Dir.chdir dir do
    git_root =  `git rev-parse --show-toplevel`.chomp
  end

  raise "No associated git repository found!" if git_root =~ /fatal:/
  git_root
end

#get_file_from_commit(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pry-git.rb', line 12

def get_file_from_commit(path)
  git_root = find_git_root(File.dirname(path))
  repo = Grit::Repo.new(git_root)
  head = repo.commits.first
  tree_names = relative_path(git_root, path).split("/")
  start_tree = head.tree
  blob_name = tree_names.last
  tree = tree_names[0..-2].inject(start_tree)  { |a, v|  a.trees.find { |t| t.basename == v } }
  blob = tree.blobs.find { |v| v.basename == blob_name }
  blob.data
end

#method_code_from_head(meth) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/pry-git.rb', line 24

def method_code_from_head(meth)
  code = get_file_from_commit(meth.source_location.first)
  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), start_line + 1]
end

#relative_path(root, path) ⇒ Object



32
33
34
35
# File 'lib/pry-git.rb', line 32

def relative_path(root, path)
  path =~ /#{root}\/(.*)/
  $1
end