Created
March 11, 2009 23:11
-
-
Save lak/77811 to your computer and use it in GitHub Desktop.
A simple script that behaves like a filebucket but with git as a backend
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
#!/usr/bin/ruby -w | |
require 'getoptlong' | |
class FileBucket | |
def sum | |
require 'digest/sha1' | |
raise "Must specify file" unless file = ARGV.shift | |
size = File.stat(file).size | |
puts Digest::SHA1.hexdigest("blob " + size.to_s + "\0" + File.read(file)) | |
end | |
def read | |
raise "Must specify sum" unless sum = ARGV.shift | |
print %x{git cat-file -p #{sum}} | |
end | |
def write | |
raise "Must specify file" unless file = ARGV.shift | |
IO.popen("git hash-object --stdin -w", "w") do |git| | |
git.print File.read(file) | |
end | |
end | |
end | |
result = GetoptLong.new( | |
[ "--help", "-h", GetoptLong::NO_ARGUMENT ] | |
) | |
result.each { |opt,arg| | |
case opt | |
when "--help" | |
puts "There is no help yet" | |
exit | |
else | |
raise "Invalid option '#{opt}'" | |
end | |
} | |
command = ARGV.shift | |
bucket = FileBucket.new | |
unless bucket.respond_to?(command) | |
raise ArgumentError, "No command '%s' command" | |
end | |
bucket.send(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment