Commit dc88ced2 authored by Stan Hu's avatar Stan Hu Committed by Alessio Caiazza

Merge branch '46498-do-not-modify-strings' into 'master'

Resolve "Blob#batch can't handle frozen string paths"

Closes #46498

See merge request gitlab-org/gitlab-ce!19039
parent 92c05bee
...@@ -104,25 +104,22 @@ module Gitlab ...@@ -104,25 +104,22 @@ module Gitlab
# file.rb # oid: 4a # file.rb # oid: 4a
# #
# #
# Blob.find_entry_by_path(repo, '1a', 'app/file.rb') # => '4a' # Blob.find_entry_by_path(repo, '1a', 'blog', 'app', 'file.rb') # => '4a'
# #
def find_entry_by_path(repository, root_id, path) def find_entry_by_path(repository, root_id, *path_parts)
root_tree = repository.lookup(root_id) root_tree = repository.lookup(root_id)
# Strip leading slashes
path[%r{^/*}] = ''
path_arr = path.split('/')
entry = root_tree.find do |entry| entry = root_tree.find do |entry|
entry[:name] == path_arr[0] entry[:name] == path_parts[0]
end end
return nil unless entry return nil unless entry
if path_arr.size > 1 if path_parts.size > 1
return nil unless entry[:type] == :tree return nil unless entry[:type] == :tree
path_arr.shift path_parts.shift
find_entry_by_path(repository, entry[:oid], path_arr.join('/')) find_entry_by_path(repository, entry[:oid], *path_parts)
else else
[:blob, :commit].include?(entry[:type]) ? entry : nil [:blob, :commit].include?(entry[:type]) ? entry : nil
end end
...@@ -185,10 +182,13 @@ module Gitlab ...@@ -185,10 +182,13 @@ module Gitlab
def find_by_rugged(repository, sha, path, limit:) def find_by_rugged(repository, sha, path, limit:)
return unless path return unless path
# Strip any leading / characters from the path
path = path.sub(%r{\A/*}, '')
rugged_commit = repository.lookup(sha) rugged_commit = repository.lookup(sha)
root_tree = rugged_commit.tree root_tree = rugged_commit.tree
blob_entry = find_entry_by_path(repository, root_tree.oid, path) blob_entry = find_entry_by_path(repository, root_tree.oid, *path.split('/'))
return nil unless blob_entry return nil unless blob_entry
......
...@@ -6,7 +6,7 @@ module Gitlab ...@@ -6,7 +6,7 @@ module Gitlab
class << self class << self
def normalize_path(filename) def normalize_path(filename)
# Strip all leading slashes so that //foo -> foo # Strip all leading slashes so that //foo -> foo
filename[%r{^/*}] = '' filename = filename.sub(%r{\A/*}, '')
# Expand relative paths (e.g. foo/../bar) # Expand relative paths (e.g. foo/../bar)
filename = Pathname.new(filename) filename = Pathname.new(filename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment