2020-03-31 14:07:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
# Centralized class for repository size related calculations.
|
|
|
|
class RepositorySizeChecker
|
2020-10-19 08:09:20 -04:00
|
|
|
attr_reader :limit
|
2020-03-31 14:07:42 -04:00
|
|
|
|
2020-04-06 20:09:33 -04:00
|
|
|
# @param current_size_proc [Proc] returns repository size in bytes
|
2020-10-19 08:09:20 -04:00
|
|
|
def initialize(current_size_proc:, limit:, namespace:, enabled: true)
|
2020-03-31 14:07:42 -04:00
|
|
|
@current_size_proc = current_size_proc
|
|
|
|
@limit = limit
|
2020-10-19 08:09:20 -04:00
|
|
|
@namespace = namespace
|
2020-03-31 14:07:42 -04:00
|
|
|
@enabled = enabled && limit != 0
|
|
|
|
end
|
|
|
|
|
2020-04-06 20:09:33 -04:00
|
|
|
# @return [Integer] bytes
|
2020-03-31 14:07:42 -04:00
|
|
|
def current_size
|
|
|
|
@current_size ||= @current_size_proc.call
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def above_size_limit?
|
|
|
|
return false unless enabled?
|
|
|
|
|
|
|
|
current_size > limit
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param change_size [int] in bytes
|
|
|
|
def changes_will_exceed_size_limit?(change_size)
|
|
|
|
return false unless enabled?
|
|
|
|
|
2020-10-27 14:08:59 -04:00
|
|
|
above_size_limit? || exceeded_size(change_size) > 0
|
2020-03-31 14:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @param change_size [int] in bytes
|
|
|
|
def exceeded_size(change_size = 0)
|
2020-10-29 14:09:11 -04:00
|
|
|
size = current_size + change_size - limit
|
|
|
|
|
|
|
|
[size, 0].max
|
2020-03-31 14:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2020-10-16 08:09:33 -04:00
|
|
|
@error_message_object ||= ::Gitlab::RepositorySizeErrorMessage.new(self)
|
2020-03-31 14:07:42 -04:00
|
|
|
end
|
2020-10-19 08:09:20 -04:00
|
|
|
|
2020-11-17 01:09:39 -05:00
|
|
|
def additional_repo_storage_available?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-10-19 08:09:20 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :namespace
|
2020-03-31 14:07:42 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-16 08:09:33 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::RepositorySizeChecker.prepend_mod_with('Gitlab::RepositorySizeChecker')
|