Skip to content

Instantly share code, notes, and snippets.

@lak
Created March 11, 2009 23:11
Show Gist options
  • Save lak/77811 to your computer and use it in GitHub Desktop.
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
#!/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