Last active
August 29, 2015 14:12
-
-
Save ifyouseewendy/ec7a4d8df55a2de70af1 to your computer and use it in GitHub Desktop.
A demo script writing Git blob object from ProGit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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