class Bringit::Index

Constants

DEFAULT_MODE

Attributes

raw_index[R]
repository[R]

Public Class Methods

new(repository) click to toggle source
# File lib/bringit/index.rb, line 7
def initialize(repository)
  @repository = repository
  @raw_index = repository.rugged.index
end

Public Instance Methods

create(options) click to toggle source
# File lib/bringit/index.rb, line 22
def create(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  if file_entry
    raise Bringit::Repository::InvalidBlobName.new("Filename already exists")
  end

  add_blob(options)
end
create_dir(options) click to toggle source
# File lib/bringit/index.rb, line 33
def create_dir(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  if file_entry
    raise Bringit::Repository::InvalidBlobName.new("Directory already exists as a file")
  end

  if dir_exists?(options[:file_path])
    raise Bringit::Repository::InvalidBlobName.new("Directory already exists")
  end

  options = options.dup
  options[:file_path] += '/.gitkeep'
  options[:content] = ''

  add_blob(options)
end
delete(options) click to toggle source
# File lib/bringit/index.rb, line 80
def delete(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  raw_index.remove(options[:file_path])
end
dir_exists?(path) click to toggle source
# File lib/bringit/index.rb, line 18
def dir_exists?(path)
  raw_index.find { |entry| entry[:path].start_with?("#{path}/") }
end
move(options) click to toggle source
# File lib/bringit/index.rb, line 63
def move(options)
  options = normalize_options(options)

  file_entry = get(options[:previous_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  if get(options[:file_path])
    raise IndexError, "A file with this name already exists"
  end

  raw_index.remove(options[:previous_path])

  add_blob(options, mode: file_entry[:mode])
end
update(options) click to toggle source
# File lib/bringit/index.rb, line 52
def update(options)
  options = normalize_options(options)

  file_entry = get(options[:file_path])
  unless file_entry
    raise Bringit::Repository::InvalidBlobName.new("File doesn't exist")
  end

  add_blob(options, mode: file_entry[:mode])
end
write_tree() click to toggle source
# File lib/bringit/index.rb, line 14
def write_tree
  raw_index.write_tree(repository.rugged)
end

Private Instance Methods

add_blob(options, mode: nil) click to toggle source
# File lib/bringit/index.rb, line 110
def add_blob(options, mode: nil)
  content = options[:content]
  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
  raise Bringit::Repository::InvalidBlobName.new(e.message)
end
normalize_options(options) click to toggle source
# File lib/bringit/index.rb, line 93
def normalize_options(options)
  options = options.dup
  options[:file_path] = normalize_path(options[:file_path]) if options[:file_path]
  options[:previous_path] = normalize_path(options[:previous_path]) if options[:previous_path]
  options
end
normalize_path(path) click to toggle source
# File lib/bringit/index.rb, line 100
def normalize_path(path)
  pathname = Bringit::PathHelper.normalize_path(path.dup)

  if pathname.each_filename.include?('..')
    raise Bringit::Repository::InvalidBlobName.new('Invalid path')
  end

  pathname.to_s
end