Class: Grit::Blame

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/blame.rb

Defined Under Namespace

Classes: BlameLine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, file, commit, lines = nil) ⇒ Blame

Returns a new instance of Blame.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/grit/blame.rb', line 7

def initialize(repo, file, commit, lines=nil)
  @repo = repo
  @file = file
  @commit = commit
  if lines.nil?
    @lines = []
    load_blame
  else
    @lines = lines
  end
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



5
6
7
# File 'lib/grit/blame.rb', line 5

def lines
  @lines
end

Instance Method Details

#inspectObject

Pretty object inspection



54
55
56
# File 'lib/grit/blame.rb', line 54

def inspect
  %Q{#<Grit::Blame "#{@file} <#{@commit}>">}
end

#load_blameObject



19
20
21
22
# File 'lib/grit/blame.rb', line 19

def load_blame
  output = @repo.git.blame({'p' => true}, @commit, '--', @file)
  process_raw_blame(output)
end

#process_raw_blame(output) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/grit/blame.rb', line 24

def process_raw_blame(output)
  lines, final = [], []
  info, commits = {}, {}

  # process the output
  output.split("\n").each do |line|
    if line[0, 1] == "\t"
      lines << line[1, line.size]
    elsif m = /^(\w{40}) (\d+) (\d+)/.match(line)
      commit_id, old_lineno, lineno = m[1], m[2].to_i, m[3].to_i
      commits[commit_id] = nil if !commits.key?(commit_id)
      info[lineno] = [commit_id, old_lineno]
    end
  end

  # load all commits in single call
  @repo.batch(*commits.keys).each do |commit|
    commits[commit.id] = commit
  end

  # get it together
  info.sort.each do |lineno, (commit_id, old_lineno)|
    commit = commits[commit_id]
    final << BlameLine.new(lineno, old_lineno, commit, lines[lineno - 1])
  end

  @lines = final
end