2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-28 08:49:21 -05:00
|
|
|
module Gitlab
|
|
|
|
class Blame
|
2022-04-19 08:08:54 -04:00
|
|
|
attr_accessor :blob, :commit, :range
|
2016-01-28 08:49:21 -05:00
|
|
|
|
2022-04-19 08:08:54 -04:00
|
|
|
def initialize(blob, commit, range: nil)
|
2016-01-28 08:49:21 -05:00
|
|
|
@blob = blob
|
|
|
|
@commit = commit
|
2022-04-19 08:08:54 -04:00
|
|
|
@range = range
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_line
|
|
|
|
range&.first || 1
|
2016-01-28 08:49:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def groups(highlight: true)
|
|
|
|
prev_sha = nil
|
|
|
|
groups = []
|
|
|
|
current_group = nil
|
|
|
|
|
2022-04-19 08:08:54 -04:00
|
|
|
i = first_line - 1
|
2022-03-24 14:07:52 -04:00
|
|
|
blame.each do |commit, line, previous_path|
|
2016-01-28 08:49:21 -05:00
|
|
|
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
|
2022-03-24 14:07:52 -04:00
|
|
|
current_group = { commit: commit, lines: [], previous_path: previous_path }
|
2016-01-28 08:49:21 -05:00
|
|
|
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
|
2022-04-19 08:08:54 -04:00
|
|
|
@blame ||= Gitlab::Git::Blame.new(repository, @commit.id, @blob.path, range: range)
|
2016-01-28 08:49:21 -05:00
|
|
|
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
|