/lib/extracts_path.rb

https://gitlab.com/vicvega/gitlab-ce · Ruby · 154 lines · 59 code · 23 blank · 72 comment · 9 complexity · 2d37f116f20c16c0568e38b1e32d88f8 MD5 · raw file

  1. # Module providing methods for dealing with separating a tree-ish string and a
  2. # file path string when combined in a request parameter
  3. module ExtractsPath
  4. # Raised when given an invalid file path
  5. class InvalidPathError < StandardError; end
  6. # Given a string containing both a Git tree-ish, such as a branch or tag, and
  7. # a filesystem path joined by forward slashes, attempts to separate the two.
  8. #
  9. # Expects a @project instance variable to contain the active project. This is
  10. # used to check the input against a list of valid repository refs.
  11. #
  12. # Examples
  13. #
  14. # # No @project available
  15. # extract_ref('master')
  16. # # => ['', '']
  17. #
  18. # extract_ref('master')
  19. # # => ['master', '']
  20. #
  21. # extract_ref("f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG")
  22. # # => ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG']
  23. #
  24. # extract_ref("v2.0.0/README.md")
  25. # # => ['v2.0.0', 'README.md']
  26. #
  27. # extract_ref('master/app/models/project.rb')
  28. # # => ['master', 'app/models/project.rb']
  29. #
  30. # extract_ref('issues/1234/app/models/project.rb')
  31. # # => ['issues/1234', 'app/models/project.rb']
  32. #
  33. # # Given an invalid branch, we fall back to just splitting on the first slash
  34. # extract_ref('non/existent/branch/README.md')
  35. # # => ['non', 'existent/branch/README.md']
  36. #
  37. # Returns an Array where the first value is the tree-ish and the second is the
  38. # path
  39. def extract_ref(id)
  40. pair = ['', '']
  41. return pair unless @project
  42. if id.match(/^([[:alnum:]]{40})(.+)/)
  43. # If the ref appears to be a SHA, we're done, just split the string
  44. pair = $~.captures
  45. else
  46. # Otherwise, attempt to detect the ref using a list of the project's
  47. # branches and tags
  48. # Append a trailing slash if we only get a ref and no file path
  49. id += '/' unless id.ends_with?('/')
  50. valid_refs = ref_names.select { |v| id.start_with?("#{v}/") }
  51. if valid_refs.length == 0
  52. # No exact ref match, so just try our best
  53. pair = id.match(/([^\/]+)(.*)/).captures
  54. else
  55. # There is a distinct possibility that multiple refs prefix the ID.
  56. # Use the longest match to maximize the chance that we have the
  57. # right ref.
  58. best_match = valid_refs.max_by(&:length)
  59. # Partition the string into the ref and the path, ignoring the empty first value
  60. pair = id.partition(best_match)[1..-1]
  61. end
  62. end
  63. # Remove ending slashes from path
  64. pair[1].gsub!(/^\/|\/$/, '')
  65. pair
  66. end
  67. # If we have an ID of 'foo.atom', and the controller provides Atom and HTML
  68. # formats, then we have to check if the request was for the Atom version of
  69. # the ID without the '.atom' suffix, or the HTML version of the ID including
  70. # the suffix. We only check this if the version including the suffix doesn't
  71. # match, so it is possible to create a branch which has an unroutable Atom
  72. # feed.
  73. def extract_ref_without_atom(id)
  74. id_without_atom = id.sub(/\.atom$/, '')
  75. valid_refs = ref_names.select { |v| "#{id_without_atom}/".start_with?("#{v}/") }
  76. valid_refs.max_by(&:length)
  77. end
  78. # Assigns common instance variables for views working with Git tree-ish objects
  79. #
  80. # Assignments are:
  81. #
  82. # - @id - A string representing the joined ref and path
  83. # - @ref - A string representing the ref (e.g., the branch, tag, or commit SHA)
  84. # - @path - A string representing the filesystem path
  85. # - @commit - A Commit representing the commit from the given ref
  86. #
  87. # If the :id parameter appears to be requesting a specific response format,
  88. # that will be handled as well.
  89. #
  90. # If there is no path and the ref doesn't exist in the repo, try to resolve
  91. # the ref without an '.atom' suffix. If _that_ ref is found, set the request's
  92. # format to Atom manually.
  93. #
  94. # Automatically renders `not_found!` if a valid tree path could not be
  95. # resolved (e.g., when a user inserts an invalid path or ref).
  96. def assign_ref_vars
  97. # assign allowed options
  98. allowed_options = ["filter_ref"]
  99. @options = params.select {|key, value| allowed_options.include?(key) && !value.blank? }
  100. @options = HashWithIndifferentAccess.new(@options)
  101. @id = get_id
  102. @ref, @path = extract_ref(@id)
  103. @repo = @project.repository
  104. @commit = @repo.commit(@ref)
  105. if @path.empty? && !@commit && @id.ends_with?('.atom')
  106. @id = @ref = extract_ref_without_atom(@id)
  107. @commit = @repo.commit(@ref)
  108. request.format = :atom if @commit
  109. end
  110. raise InvalidPathError unless @commit
  111. @hex_path = Digest::SHA1.hexdigest(@path)
  112. @logs_path = logs_file_namespace_project_ref_path(@project.namespace,
  113. @project, @ref, @path)
  114. rescue RuntimeError, NoMethodError, InvalidPathError
  115. render_404
  116. end
  117. def tree
  118. @tree ||= @repo.tree(@commit.id, @path)
  119. end
  120. private
  121. # overriden in subclasses, do not remove
  122. def get_id
  123. id = params[:id] || params[:ref]
  124. id += "/" + params[:path] unless params[:path].blank?
  125. id
  126. end
  127. def ref_names
  128. return [] unless @project
  129. @ref_names ||= @project.repository.ref_names
  130. end
  131. end