Instantly share code, notes, and snippets.

Embed
What would you like to do?
A demo script writing Git blob object from ProGit
require 'digest/sha1'
require 'zlib'
require 'fileutils'
# put_raw_object("what is up, doc?", 'blob')
def put_raw_object(content, type)
# Generate SHA-1
header = "#{type} #{content.length}\0" # => "blob 16\000"
store = header + content # => "blob 16\000what is up, doc?"
sha1 = Digest::SHA1.hexdigest(store) # => "bd9dbf5aae1a3862dd1526723246b20206e5fc37"
# p sha1
# Compress with Zlib
zlib_content = Zlib::Deflate.deflate(store)
# Write to disk
path = '.git/objects/' + sha1[0,2] + '/' + sha1[2,38] # => ".git/objects/bd/9dbf5aae1a3862dd1526723246b20206e5fc37"
FileUtils.mkdir_p(File.dirname(path)) # => ".git/objects/bd"
File.open(path, 'w'){|f| f.write zlib_content }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment