Class: Gitlab::Blame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blob, commit, range: nil) ⇒ Blame

Returns a new instance of Blame.



7
8
9
10
11
# File 'lib/gitlab/blame.rb', line 7

def initialize(blob, commit, range: nil)
  @blob = blob
  @commit = commit
  @range = range
end

Instance Attribute Details

#blobObject

Returns the value of attribute blob.



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

def blob
  @blob
end

#commitObject

Returns the value of attribute commit.



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

def commit
  @commit
end

#rangeObject

Returns the value of attribute range.



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

def range
  @range
end

Instance Method Details

#first_lineObject



13
14
15
# File 'lib/gitlab/blame.rb', line 13

def first_line
  range&.first || 1
end

#groups(highlight: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/blame.rb', line 17

def groups(highlight: true)
  prev_sha = nil
  groups = []
  current_group = nil

  i = first_line - 1
  blame.each do |commit, line, previous_path, span|
    commit = Commit.new(commit, project)
    commit.lazy_author # preload author

    if prev_sha != commit.sha
      groups << current_group if current_group
      current_group = { commit: commit, lines: [], previous_path: previous_path, span: span, lineno: i + 1 }
    end

    current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line)

    prev_sha = commit.sha
    i += 1
  end
  groups << current_group if current_group

  groups
end