Skip to content

Instantly share code, notes, and snippets.

@leifericf
Last active August 29, 2015 14:03
Show Gist options
  • Save leifericf/83f69fb1d631fcf55322 to your computer and use it in GitHub Desktop.
Save leifericf/83f69fb1d631fcf55322 to your computer and use it in GitHub Desktop.
Generate Git file (aka "blob") hash, without using Git.
#!/usr/bin/env ruby
require 'digest/sha1'
def git_hash(file)
data = File.read(file)
size = data.bytesize.to_s
Digest::SHA1.hexdigest('blob ' + size + "\0" + data) # Git's hashing algorithm
end
puts git_hash(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment