Parent

Files

Grit::GitRuby::Internal::LooseStorage

Public Class Methods

calculate_sha(content, type) click to toggle source

simply figure out the sha

# File lib/grit/git-ruby/internal/loose.rb, line 87
def self.calculate_sha(content, type)
  size = content.length.to_s
  verify_header(type, size)
  header = "#{type} #{size}\00""
  store = header + content

  Digest::SHA1.hexdigest(store)
end
new(directory) click to toggle source
# File lib/grit/git-ruby/internal/loose.rb, line 23
def initialize(directory)
  @directory = directory
end
verify_header(type, size) click to toggle source
# File lib/grit/git-ruby/internal/loose.rb, line 96
def self.verify_header(type, size)
  if !%(blob tree commit tag).include?(type) || size !~ /^\d+$/
    raise LooseObjectError, "invalid object header"
  end
end

Public Instance Methods

[](sha1) click to toggle source
# File lib/grit/git-ruby/internal/loose.rb, line 27
def [](sha1)
  sha1 = sha1.unpack("H*")[0]
  begin
    return nil unless sha1[0...2] && sha1[2..39]
    path = @directory + '/' + sha1[0...2] + '/' + sha1[2..39]
    get_raw_object(open(path, 'rb') { |f| f.read })
  rescue Errno::ENOENT
    nil
  end
end
get_raw_object(buf) click to toggle source
# File lib/grit/git-ruby/internal/loose.rb, line 38
def get_raw_object(buf)
  if buf.length < 2
    raise LooseObjectError, "object file too small"
  end

  if legacy_loose_object?(buf)
    content = Zlib::Inflate.inflate(buf)
    header, content = content.split(/\00//, 2)
    if !header || !content
      raise LooseObjectError, "invalid object header"
    end
    type, size = header.split(/ /, 2)
    if !%(blob tree commit tag).include?(type) || size !~ /^\d+$/
      raise LooseObjectError, "invalid object header"
    end
    type = type.to_sym
    size = size.to_i
  else
    type, size, used = unpack_object_header_gently(buf)
    content = Zlib::Inflate.inflate(buf[used..-1])
  end
  raise LooseObjectError, "size mismatch" if content.length != size
  return RawObject.new(type, content)
end
put_raw_object(content, type) click to toggle source

currently, I’m using the legacy format because it’s easier to do this function takes content and a type and writes out the loose object and returns a sha

# File lib/grit/git-ruby/internal/loose.rb, line 65
def put_raw_object(content, type)
  size = content.length.to_s
  LooseStorage.verify_header(type, size)

  header = "#{type} #{size}\00""
  store = header + content

  sha1 = Digest::SHA1.hexdigest(store)
  path = @directory+'/'+sha1[0...2]+'/'+sha1[2..40]

  if !File.exists?(path)
    content = Zlib::Deflate.deflate(store)

    FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
    File.open(path, 'wb') do |f|
      f.write content
    end
  end
  return sha1
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.