2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-28 08:49:21 -05:00
|
|
|
module Gitlab
|
|
|
|
class Blame
|
|
|
|
attr_accessor :blob, :commit
|
|
|
|
|
|
|
|
def initialize(blob, commit)
|
|
|
|
@blob = blob
|
|
|
|
@commit = commit
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups(highlight: true)
|
|
|
|
prev_sha = nil
|
|
|
|
groups = []
|
|
|
|
current_group = nil
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
blame.each do |commit, line|
|
|
|
|
commit = Commit.new(commit, project)
|
2019-10-14 14:06:24 -04:00
|
|
|
commit.lazy_author # preload author
|
2016-01-28 08:49:21 -05:00
|
|
|
|
2021-03-08 19:09:23 -05:00
|
|
|
if prev_sha != commit.sha
|
2016-01-28 08:49:21 -05:00
|
|
|
groups << current_group if current_group
|
|
|
|
current_group = { commit: commit, lines: [] }
|
|
|
|
end
|
|
|
|
|
2021-03-08 19:09:23 -05:00
|
|
|
current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line)
|
2016-01-28 08:49:21 -05:00
|
|
|
|
2021-03-08 19:09:23 -05:00
|
|
|
prev_sha = commit.sha
|
2016-01-28 08:49:21 -05:00
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
groups << current_group if current_group
|
|
|
|
|
|
|
|
groups
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def blame
|
|
|
|
@blame ||= Gitlab::Git::Blame.new(repository, @commit.id, @blob.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_lines
|
2017-06-06 17:20:24 -04:00
|
|
|
@blob.load_all_data!
|
2018-09-06 00:34:25 -04:00
|
|
|
@highlighted_lines ||= @blob.present.highlight.lines
|
2016-01-28 08:49:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
commit.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
project.repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|