From c4b96913246fcb0c4adeb5a440fc0e7df70d7897 Mon Sep 17 00:00:00 2001 From: Andrew Armenia Date: Wed, 28 Dec 2011 13:47:22 -0500 Subject: [PATCH] add "hash_file" method to repository objects This allows blobs to be created from arbitrary files. (note that this currently requires an absolute path) --- lib/git/base.rb | 4 ++++ lib/git/lib.rb | 4 ++++ tests/units/test_hash_object.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/units/test_hash_object.rb diff --git a/lib/git/base.rb b/lib/git/base.rb index 5ad8906a..f2147d3a 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -468,6 +468,10 @@ def cat_file(objectish) self.lib.object_contents(objectish) end + def hash_file(path) + self.lib.hash_file(path) + end + # returns the name of the branch the working directory is currently on def current_branch self.lib.branch_current diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 52fb2e6c..7a62ccd5 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -167,6 +167,10 @@ def object_contents(sha, &block) command('cat-file', ['-p', sha], &block) end + def hash_file(path, &block) + command('hash-object', ['-w', path], &block) + end + def ls_tree(sha) data = {'blob' => {}, 'tree' => {}} diff --git a/tests/units/test_hash_object.rb b/tests/units/test_hash_object.rb new file mode 100644 index 00000000..dd92624a --- /dev/null +++ b/tests/units/test_hash_object.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestHashObject < Test::Unit::TestCase + + def setup + set_file_paths + @git = Git.open(@wdir) + end + + def test_hash_file + + in_temp_dir do + g = Git.clone(@wbare, 'test') + + File.open('test_file', 'w') do |f| + f.write('asdfasdf') + end + + hash = g.hash_file(File.expand_path('test_file')) + + # from git-hash-object + assert(hash == '23a3a9d599131d7f61b7383e5814c63e63ed186d') + # make sure file was created in repository + assert(g.cat_file(hash) == 'asdfasdf'); + end + end + +end