Skip to content

Instantly share code, notes, and snippets.

@higuma
Created June 13, 2022 13:58
Show Gist options
  • Save higuma/9c8e041c417ea6473d0cb3303fc30e8c to your computer and use it in GitHub Desktop.
Save higuma/9c8e041c417ea6473d0cb3303fc30e8c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# git-id: calculates the git ID (sha-1) with the specified object type
require 'optparse'
require 'openssl'
type = 'blob' # as default
opt = OptionParser.new
opt.version = "0.1.0"
opt.banner = "Usage: #{opt.program_name} [options] FILE [MORE_FILES...]"
opt.on('-h', '--help', 'Shows help message') do
puts opt
exit
end
opt.on('-v', '--version', 'Shows version') do
puts opt.ver
exit
end
opt.on('-t TYPE', '--type TYPE', 'object type: blob(default), tree, commit, or tag') do |t|
if ['blob', 'tree', 'commit', 'tag'].member? t
type = t
else
STDERR.puts "Invalid type: #{t} (must be one of blob, tree, commit, or tag)"
exit
end
end
opt.parse! ARGV
if ARGV.size == 0
STDERR.puts "FILE is not spefified"
puts
puts opt
exit
end
for file in ARGV
if File.exist? file
bytes = File.binread file
digest = OpenSSL::Digest::SHA1::hexdigest "#{type} #{bytes.size}\0#{bytes}"
puts "#{digest} [#{type}] #{file}"
else
STDERR.puts "Error: file does not exist: #{file}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment