debian-mirror-gitlab/app/services/projects/blame_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
1.5 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +05:30
# frozen_string_literal: true
# Service class to correctly initialize Gitlab::Blame and Kaminari pagination
# objects
module Projects
class BlameService
PER_PAGE = 1000
def initialize(blob, commit, params)
@blob = blob
@commit = commit
@page = extract_page(params)
2022-10-11 01:57:18 +05:30
@pagination_enabled = pagination_state(params)
2022-07-16 23:28:13 +05:30
end
2022-08-13 15:12:31 +05:30
attr_reader :page
2022-07-16 23:28:13 +05:30
def blame
Gitlab::Blame.new(blob, commit, range: blame_range)
end
def pagination
2022-10-11 01:57:18 +05:30
return unless pagination_enabled
2022-07-16 23:28:13 +05:30
2022-08-13 15:12:31 +05:30
Kaminari.paginate_array([], total_count: blob_lines_count, limit: per_page)
.tap { |pagination| pagination.max_paginates_per(per_page) }
2022-07-16 23:28:13 +05:30
.page(page)
end
private
2022-10-11 01:57:18 +05:30
attr_reader :blob, :commit, :pagination_enabled
2022-07-16 23:28:13 +05:30
def blame_range
2022-10-11 01:57:18 +05:30
return unless pagination_enabled
2022-07-16 23:28:13 +05:30
first_line = (page - 1) * per_page + 1
last_line = (first_line + per_page).to_i - 1
first_line..last_line
end
def extract_page(params)
page = params.fetch(:page, 1).to_i
return 1 if page < 1 || overlimit?(page)
page
end
def per_page
PER_PAGE
end
2022-10-11 01:57:18 +05:30
def pagination_state(params)
return false if Gitlab::Utils.to_boolean(params[:no_pagination], default: false)
Feature.enabled?(:blame_page_pagination, commit.project)
end
2022-07-16 23:28:13 +05:30
def overlimit?(page)
page * per_page >= blob_lines_count + per_page
end
def blob_lines_count
@blob_lines_count ||= blob.data.lines.count
end
end
end