Class: GitSpelunk::Offset

Inherits:
Object
  • Object
show all
Defined in:
lib/git_spelunk/offset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, file_name, sha) ⇒ Offset

Returns a new instance of Offset.



41
42
43
44
45
46
47
# File 'lib/git_spelunk/offset.rb', line 41

def initialize(repo, file_name, sha)
  @repo = repo
  @file_name = file_name
  @sha = sha
  parent_sha = @repo.commits(@sha)[0].parents[0].id
  @chunks = diff_chunks(@repo.diff(parent_sha, @sha, @file_name))
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



39
40
41
# File 'lib/git_spelunk/offset.rb', line 39

def chunks
  @chunks
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



39
40
41
# File 'lib/git_spelunk/offset.rb', line 39

def file_name
  @file_name
end

#repoObject (readonly)

Returns the value of attribute repo.



39
40
41
# File 'lib/git_spelunk/offset.rb', line 39

def repo
  @repo
end

#shaObject (readonly)

Returns the value of attribute sha.



39
40
41
# File 'lib/git_spelunk/offset.rb', line 39

def sha
  @sha
end

Instance Method Details

#diff_chunks(diffs) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/git_spelunk/offset.rb', line 49

def diff_chunks(diffs)
  return nil if diffs.empty?
  # split it into chunks: [["@@ -10,13 +10,18 @@", diffs], ["@@ -20,13 +20,18 @@", diffs, diff]]
  multiple_chunks = diffs[0].diff.split(/(@@.*?@@.*?\n)/)
  # Discard file name line
  multiple_chunks[1..multiple_chunks.length].each_slice(2).to_a
end

#line_number_to_parent(src_line_number) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/git_spelunk/offset.rb', line 57

def line_number_to_parent(src_line_number)
  return nil unless @chunks
  chunk = target_chunk(src_line_number)
  chunk_starting_line, chunk_total_lines = src_start_and_total(stats_line(chunk))
  parent_starting_line = parent_start_and_total(stats_line(chunk))[0]
  parent_line_offset = find_parent_line_number(diff_lines(chunk), src_line_number, chunk_starting_line, chunk_total_lines)
  parent_starting_line + parent_line_offset
end