summaryrefslogtreecommitdiff
path: root/lib/extracts_path.rb
blob: db5c3bb1d4aac16879be3dd5f090169931777d7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

# Module providing methods for dealing with separating a tree-ish string and a
# file path string when combined in a request parameter
module ExtractsPath
  extend ::Gitlab::Utils::Override
  include ExtractsRef

  # 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}/") }

    raise InvalidPathError if valid_refs.blank?

    valid_refs.max_by(&:length)
  end

  # 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.
  #
  # Automatically renders `not_found!` if a valid tree path could not be
  # resolved (e.g., when a user inserts an invalid path or ref).
  #
  # Automatically redirects to the current default branch if the ref matches a
  # previous default branch that has subsequently been deleted.
  #
  # rubocop:disable Gitlab/ModuleWithInstanceVariables
  override :assign_ref_vars
  def assign_ref_vars
    super

    rectify_atom!

    rectify_renamed_default_branch! && return

    raise InvalidPathError unless @commit

    @hex_path = Digest::SHA1.hexdigest(@path)
    @logs_path = logs_file_project_ref_path(@project, @ref, @path)
  rescue RuntimeError, NoMethodError, InvalidPathError
    render_404
  end
  # rubocop:enable Gitlab/ModuleWithInstanceVariables

  private

  # 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

  override :repository_container
  def repository_container
    @project
  end
end