2017-07-12 10:31:36 -04:00
|
|
|
# Gitaly note: JV: When the time comes I think we will want to copy this
|
|
|
|
# class into Gitaly. None of its methods look like they should be RPC's.
|
|
|
|
# The RPC's will be at a higher level.
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Index
|
2017-04-19 20:37:44 -04:00
|
|
|
IndexError = Class.new(StandardError)
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
DEFAULT_MODE = 0o100644
|
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
ACTIONS = %w(create create_dir update move delete).freeze
|
2018-01-03 20:47:25 -05:00
|
|
|
ACTION_OPTIONS = %i(file_path previous_path content encoding).freeze
|
2017-04-19 20:37:44 -04:00
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
attr_reader :repository, :raw_index
|
|
|
|
|
|
|
|
def initialize(repository)
|
|
|
|
@repository = repository
|
|
|
|
@raw_index = repository.rugged.index
|
|
|
|
end
|
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
delegate :read_tree, :get, to: :raw_index
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2018-01-03 20:47:25 -05:00
|
|
|
def apply(action, options)
|
|
|
|
validate_action!(action)
|
|
|
|
public_send(action, options.slice(*ACTION_OPTIONS)) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
def write_tree
|
|
|
|
raw_index.write_tree(repository.rugged)
|
|
|
|
end
|
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
def dir_exists?(path)
|
|
|
|
raw_index.find { |entry| entry[:path].start_with?("#{path}/") }
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(options)
|
2017-02-24 10:32:10 -05:00
|
|
|
options = normalize_options(options)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
if get(options[:file_path])
|
|
|
|
raise IndexError, "A file with this name already exists"
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
add_blob(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_dir(options)
|
2017-02-24 10:32:10 -05:00
|
|
|
options = normalize_options(options)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
if get(options[:file_path])
|
|
|
|
raise IndexError, "A file with this name already exists"
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
if dir_exists?(options[:file_path])
|
2017-04-19 20:37:44 -04:00
|
|
|
raise IndexError, "A directory with this name already exists"
|
2017-02-24 10:32:10 -05:00
|
|
|
end
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
options = options.dup
|
|
|
|
options[:file_path] += '/.gitkeep'
|
|
|
|
options[:content] = ''
|
|
|
|
|
|
|
|
add_blob(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(options)
|
2017-02-24 10:32:10 -05:00
|
|
|
options = normalize_options(options)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
file_entry = get(options[:file_path])
|
2017-02-13 16:09:36 -05:00
|
|
|
unless file_entry
|
2017-04-19 20:37:44 -04:00
|
|
|
raise IndexError, "A file with this name doesn't exist"
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
add_blob(options, mode: file_entry[:mode])
|
|
|
|
end
|
|
|
|
|
|
|
|
def move(options)
|
2017-02-24 10:32:10 -05:00
|
|
|
options = normalize_options(options)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
file_entry = get(options[:previous_path])
|
2017-02-13 16:09:36 -05:00
|
|
|
unless file_entry
|
2017-04-19 20:37:44 -04:00
|
|
|
raise IndexError, "A file with this name doesn't exist"
|
|
|
|
end
|
|
|
|
|
|
|
|
if get(options[:file_path])
|
|
|
|
raise IndexError, "A file with this name already exists"
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
raw_index.remove(options[:previous_path])
|
|
|
|
|
|
|
|
add_blob(options, mode: file_entry[:mode])
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(options)
|
2017-02-24 10:32:10 -05:00
|
|
|
options = normalize_options(options)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
unless get(options[:file_path])
|
|
|
|
raise IndexError, "A file with this name doesn't exist"
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
raw_index.remove(options[:file_path])
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
def normalize_options(options)
|
|
|
|
options = options.dup
|
2017-02-13 16:09:36 -05:00
|
|
|
options[:file_path] = normalize_path(options[:file_path]) if options[:file_path]
|
|
|
|
options[:previous_path] = normalize_path(options[:previous_path]) if options[:previous_path]
|
2017-02-24 10:32:10 -05:00
|
|
|
options
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_path(path)
|
2017-04-19 20:37:44 -04:00
|
|
|
unless path
|
|
|
|
raise IndexError, "You must provide a file path"
|
|
|
|
end
|
|
|
|
|
2017-02-24 10:32:10 -05:00
|
|
|
pathname = Gitlab::Git::PathHelper.normalize_path(path.dup)
|
2017-02-13 16:09:36 -05:00
|
|
|
|
2017-04-19 20:37:44 -04:00
|
|
|
pathname.each_filename do |segment|
|
|
|
|
if segment == '..'
|
|
|
|
raise IndexError, 'Path cannot include directory traversal'
|
|
|
|
end
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
pathname.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_blob(options, mode: nil)
|
|
|
|
content = options[:content]
|
2017-04-19 20:37:44 -04:00
|
|
|
unless content
|
|
|
|
raise IndexError, "You must provide content"
|
|
|
|
end
|
|
|
|
|
2017-02-13 16:09:36 -05:00
|
|
|
content = Base64.decode64(content) if options[:encoding] == 'base64'
|
|
|
|
|
|
|
|
detect = CharlockHolmes::EncodingDetector.new.detect(content)
|
|
|
|
unless detect && detect[:type] == :binary
|
|
|
|
# When writing to the repo directly as we are doing here,
|
|
|
|
# the `core.autocrlf` config isn't taken into account.
|
|
|
|
content.gsub!("\r\n", "\n") if repository.autocrlf
|
|
|
|
end
|
|
|
|
|
|
|
|
oid = repository.rugged.write(content, :blob)
|
|
|
|
|
|
|
|
raw_index.add(path: options[:file_path], oid: oid, mode: mode || DEFAULT_MODE)
|
|
|
|
rescue Rugged::IndexError => e
|
2017-04-19 20:37:44 -04:00
|
|
|
raise IndexError, e.message
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
2018-01-03 20:47:25 -05:00
|
|
|
|
|
|
|
def validate_action!(action)
|
|
|
|
unless ACTIONS.include?(action.to_s)
|
|
|
|
raise ArgumentError, "Unknown action '#{action}'"
|
|
|
|
end
|
|
|
|
end
|
2017-02-13 16:09:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|