2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-28 13:49:21 +00: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
|
2022-03-24 18:07:52 +00:00
|
|
|
blame.each do |commit, line, previous_path|
|
2016-01-28 13:49:21 +00:00
|
|
|
commit = Commit.new(commit, project)
|
2019-10-14 18:06:24 +00:00
|
|
|
commit.lazy_author # preload author
|
2016-01-28 13:49:21 +00:00
|
|
|
|
2021-03-09 00:09:23 +00:00
|
|
|
if prev_sha != commit.sha
|
2016-01-28 13:49:21 +00:00
|
|
|
groups << current_group if current_group
|
2022-03-24 18:07:52 +00:00
|
|
|
current_group = { commit: commit, lines: [], previous_path: previous_path }
|
2016-01-28 13:49:21 +00:00
|
|
|
end
|
|
|
|
|
2021-03-09 00:09:23 +00:00
|
|
|
current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line)
|
2016-01-28 13:49:21 +00:00
|
|
|
|
2021-03-09 00:09:23 +00:00
|
|
|
prev_sha = commit.sha
|
2016-01-28 13:49:21 +00: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 21:20:24 +00:00
|
|
|
@blob.load_all_data!
|
2018-09-06 04:34:25 +00:00
|
|
|
@highlighted_lines ||= @blob.present.highlight.lines
|
2016-01-28 13:49:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
commit.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
project.repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|