Class: Git::Blame

Inherits:
Object
  • Object
show all
Defined in:
lib/Git/Blame.rb

Defined Under Namespace

Classes: PorcelainEntry

Instance Method Summary collapse

Constructor Details

#initialize(filename, line_number = nil) ⇒ Blame

class PorcelainEntry



81
82
83
84
# File 'lib/Git/Blame.rb', line 81

def initialize(filename, line_number = nil)
  @filename = filename
  @line_number = line_number
end

Instance Method Details

#blame_outputObject



94
95
96
# File 'lib/Git/Blame.rb', line 94

def blame_output
  `#{command_string}`
end

#command_stringObject



86
87
88
89
90
91
92
# File 'lib/Git/Blame.rb', line 86

def command_string
  if @line_number
    "git --no-pager blame -L #{@line_number},#{@line_number} #{@filename} --line-porcelain"
  else
    "git --no-pager blame #{@filename} --line-porcelain"
  end
end

#entriesObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/Git/Blame.rb', line 98

def entries
  line_count = 0
  porcelain_entry_string = ''
  entries = []
  blame_array = blame_output.split("\n")
  line_number = 0
  i = -1
  until i >= blame_array.size - 1
    until blame_array[i].split.first == 'filename'
      porcelain_entry_string << blame_array[i += 1]
      porcelain_entry_string << "\n"
    end
    porcelain_entry_string << blame_array[i += 1]
    porcelain_entry_string << "\n"
    entries << PorcelainEntry.new(porcelain_entry_string, line_number += 1).parse
    line_count = 0
    porcelain_entry_string = ''
  end
  entries
end

#find(line_number) ⇒ Object



119
120
121
# File 'lib/Git/Blame.rb', line 119

def find(line_number)
  entries.detect{|entry| entry.line_number == line_number}
end