debian-mirror-gitlab/lib/gitlab/blame.rb

58 lines
1.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-02-05 20:25:01 +05:30
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)
sha = commit.sha
if prev_sha != sha
groups << current_group if current_group
current_group = { commit: commit, lines: [] }
end
line = highlighted_lines[i].html_safe if highlight
current_group[:lines] << line
prev_sha = sha
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-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