Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifyouseewendy/ec7a4d8df55a2de70af1 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/ec7a4d8df55a2de70af1 to your computer and use it in GitHub Desktop.
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