2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-05 20:25:01 +05:30
|
|
|
module Gitlab
|
|
|
|
class Blame
|
2022-06-21 17:19:12 +05:30
|
|
|
attr_accessor :blob, :commit, :range
|
2016-02-05 20:25:01 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def initialize(blob, commit, range: nil)
|
2016-02-05 20:25:01 +05:30
|
|
|
@blob = blob
|
|
|
|
@commit = commit
|
2022-06-21 17:19:12 +05:30
|
|
|
@range = range
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_line
|
|
|
|
range&.first || 1
|
2016-02-05 20:25:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def groups(highlight: true)
|
|
|
|
prev_sha = nil
|
|
|
|
groups = []
|
|
|
|
current_group = nil
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
i = first_line - 1
|
|
|
|
blame.each do |commit, line, previous_path|
|
2016-02-05 20:25:01 +05:30
|
|
|
commit = Commit.new(commit, project)
|
2019-12-21 20:55:43 +05:30
|
|
|
commit.lazy_author # preload author
|
2016-02-05 20:25:01 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
if prev_sha != commit.sha
|
2016-02-05 20:25:01 +05:30
|
|
|
groups << current_group if current_group
|
2022-06-21 17:19:12 +05:30
|
|
|
current_group = { commit: commit, lines: [], previous_path: previous_path }
|
2016-02-05 20:25:01 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
current_group[:lines] << (highlight ? highlighted_lines[i].html_safe : line)
|
2016-02-05 20:25:01 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
prev_sha = commit.sha
|
2016-02-05 20:25:01 +05:30
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
groups << current_group if current_group
|
|
|
|
|
|
|
|
groups
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def blame
|
2022-06-21 17:19:12 +05:30
|
|
|
@blame ||= Gitlab::Git::Blame.new(repository, @commit.id, @blob.path, range: range)
|
2016-02-05 20:25:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_lines
|
2017-09-10 17:25:29 +05:30
|
|
|
@blob.load_all_data!
|
2018-12-13 13:39:08 +05:30
|
|
|
@highlighted_lines ||= @blob.present.highlight.lines
|
2016-02-05 20:25:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
commit.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
project.repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|