2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-09-20 13:55:14 -04:00
|
|
|
# Module providing methods for dealing with separating a tree-ish string and a
|
|
|
|
# file path string when combined in a request parameter
|
|
|
|
module ExtractsPath
|
2020-06-04 05:08:01 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
include ExtractsRef
|
2012-09-17 14:24:31 -04:00
|
|
|
|
2016-10-07 11:49:48 -04:00
|
|
|
# If we have an ID of 'foo.atom', and the controller provides Atom and HTML
|
|
|
|
# formats, then we have to check if the request was for the Atom version of
|
|
|
|
# the ID without the '.atom' suffix, or the HTML version of the ID including
|
|
|
|
# the suffix. We only check this if the version including the suffix doesn't
|
|
|
|
# match, so it is possible to create a branch which has an unroutable Atom
|
|
|
|
# feed.
|
|
|
|
def extract_ref_without_atom(id)
|
|
|
|
id_without_atom = id.sub(/\.atom$/, '')
|
|
|
|
valid_refs = ref_names.select { |v| "#{id_without_atom}/".start_with?("#{v}/") }
|
|
|
|
|
|
|
|
valid_refs.max_by(&:length)
|
|
|
|
end
|
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
# Extends the method to handle if there is no path and the ref doesn't
|
|
|
|
# exist in the repo, try to resolve the ref without an '.atom' suffix.
|
|
|
|
# If _that_ ref is found, set the request's format to Atom manually.
|
2016-10-07 11:49:48 -04:00
|
|
|
#
|
2012-09-20 13:55:14 -04:00
|
|
|
# Automatically renders `not_found!` if a valid tree path could not be
|
|
|
|
# resolved (e.g., when a user inserts an invalid path or ref).
|
2020-06-04 05:08:01 -04:00
|
|
|
#
|
2021-07-14 08:09:23 -04:00
|
|
|
# Automatically redirects to the current default branch if the ref matches a
|
|
|
|
# previous default branch that has subsequently been deleted.
|
|
|
|
#
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2020-06-04 05:08:01 -04:00
|
|
|
override :assign_ref_vars
|
2012-09-17 14:24:31 -04:00
|
|
|
def assign_ref_vars
|
2020-06-04 05:08:01 -04:00
|
|
|
super
|
2016-10-14 18:36:05 -04:00
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
rectify_atom!
|
2016-10-07 11:49:48 -04:00
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
rectify_renamed_default_branch! && return
|
2013-10-01 11:26:55 -04:00
|
|
|
|
2016-11-05 23:33:39 -04:00
|
|
|
raise InvalidPathError unless @commit
|
|
|
|
|
2013-04-14 08:08:16 -04:00
|
|
|
@hex_path = Digest::SHA1.hexdigest(@path)
|
2017-06-29 13:06:35 -04:00
|
|
|
@logs_path = logs_file_project_ref_path(@project, @ref, @path)
|
2013-03-01 08:59:43 -05:00
|
|
|
rescue RuntimeError, NoMethodError, InvalidPathError
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404
|
2012-09-17 14:24:31 -04:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2013-04-14 08:08:16 -04:00
|
|
|
|
2016-08-23 20:22:30 -04:00
|
|
|
private
|
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
# Override in controllers to determine which actions are subject to the redirect
|
|
|
|
def redirect_renamed_default_branch?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def rectify_atom!
|
|
|
|
return if @commit
|
|
|
|
return unless @id.ends_with?('.atom')
|
|
|
|
return unless @path.empty?
|
|
|
|
|
|
|
|
@id = @ref = extract_ref_without_atom(@id)
|
|
|
|
@commit = @repo.commit(@ref)
|
|
|
|
|
|
|
|
request.format = :atom if @commit
|
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
|
|
|
# For GET/HEAD requests, if the ref doesn't exist in the repository, check
|
|
|
|
# whether we're trying to access a renamed default branch. If we are, we can
|
|
|
|
# redirect to the current default branch instead of rendering a 404.
|
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def rectify_renamed_default_branch!
|
|
|
|
return unless redirect_renamed_default_branch?
|
|
|
|
return if @commit
|
|
|
|
return unless @id && @ref && repository_container.respond_to?(:previous_default_branch)
|
|
|
|
return unless repository_container.previous_default_branch == @ref
|
|
|
|
return unless request.get? || request.head?
|
|
|
|
|
|
|
|
flash[:notice] = _('The default branch for this project has been changed. Please update your bookmarks.')
|
|
|
|
redirect_to url_for(id: @id.sub(/\A#{Regexp.escape(@ref)}/, repository_container.default_branch))
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
override :repository_container
|
|
|
|
def repository_container
|
|
|
|
@project
|
2016-10-07 11:49:48 -04:00
|
|
|
end
|
2012-09-17 10:44:36 -04:00
|
|
|
end
|