Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #151 from tnm/100-percent-tree

Wrap and introduce get_entry_by_oid
  • Loading branch information...
commit 47678c7ebfa9c9098131bcd3efd91b293617eeab 2 parents 704c000 + c5de6a9
@vmg vmg authored
Showing with 45 additions and 0 deletions.
  1. +28 −0 ext/rugged/rugged_tree.c
  2. +17 −0 test/tree_test.rb
View
28 ext/rugged/rugged_tree.c
@@ -120,6 +120,33 @@ static VALUE rb_git_tree_get_entry(VALUE self, VALUE entry_id)
/*
* call-seq:
+ * tree.get_entry_by_oid(rb_oid) -> entry
+ *
+ * Return one of the entries from a tree as a +Hash+, based off the oid SHA.
+ *
+ * If the entry doesn't exist, +nil+ will be returned.
+ *
+ * This does a full traversal of the every element in the tree, so this method
+ * is not especially fast.
+ *
+ * tree.get_entry_by_oid("d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f")
+ * #=> {:name => "foo.txt", :type => :blob, :oid => "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f", :filemode => 0}
+ *
+ */
+static VALUE rb_git_tree_get_entry_by_oid(VALUE self, VALUE rb_oid)
+{
+ git_tree *tree;
+ git_oid oid;
+ Data_Get_Struct(self, git_tree, tree);
+
+ Check_Type(rb_oid, T_STRING);
+ rugged_exception_check(git_oid_fromstr(&oid, StringValueCStr(rb_oid)));
+
+ return rb_git_treeentry_fromC(git_tree_entry_byoid(tree, &oid));
+}
+
+/*
+ * call-seq:
* tree.each { |entry| block }
* tree.each -> Iterator
*
@@ -374,6 +401,7 @@ void Init_rugged_tree()
rb_define_method(rb_cRuggedTree, "count", rb_git_tree_entrycount, 0);
rb_define_method(rb_cRuggedTree, "length", rb_git_tree_entrycount, 0);
rb_define_method(rb_cRuggedTree, "get_entry", rb_git_tree_get_entry, 1);
+ rb_define_method(rb_cRuggedTree, "get_entry_by_oid", rb_git_tree_get_entry_by_oid, 1);
rb_define_method(rb_cRuggedTree, "path", rb_git_tree_path, 1);
rb_define_method(rb_cRuggedTree, "[]", rb_git_tree_get_entry, 1);
rb_define_method(rb_cRuggedTree, "each", rb_git_tree_each, 0);
View
17 test/tree_test.rb
@@ -31,6 +31,23 @@ def test_read_tree_entry_data
assert_equal :tree, @repo.lookup(tent[:oid]).type
end
+ def test_get_entry_by_oid
+ bent = @tree.get_entry_by_oid("1385f264afb75a56a5bec74243be9b367ba4ca08")
+ assert_equal "README", bent[:name]
+ assert_equal :blob, bent[:type]
+ end
+
+ def test_get_entry_by_oid_returns_nil_if_no_oid
+ nada = @tree.get_entry_by_oid("1385f264afb75a56a5bec74243be9b367ba4ca07")
+ assert_equal nil, nada
+ end
+
+ def test_get_entry_by_oid_throws_error_if_wrong_type
+ assert_raises TypeError do
+ @tree.get_entry_by_oid(:not_a_string)
+ end
+ end
+
def test_tree_iteration
enum_test = @tree.sort { |a, b| a[:oid] <=> b[:oid] }.map { |e| e[:name] }.join(':')
assert_equal "README:subdir:new.txt", enum_test
Please sign in to comment.
Something went wrong with that request. Please try again.