Skip to content

Loading…

Wrap and introduce get_entry_by_oid #151

Merged
merged 2 commits into from

2 participants

@tnm
libgit2 member

This adds get_entry_by_oid, which returns a hash entry from a tree, based off the oid sha (instead of index or filename, like plain get_entry). I set this as its own method for clarity and especially because this method has different performance characteristics from get_entry; in theory we could fold it into get_entry if we wanted, but I do like this approach for now.

Added three unit tests, confirming correct behavior when given a non-existent oid or when given a non-string argument.

cc @vmg

tnm added some commits
@tnm tnm Introduce get_entry_by_oid.
Based on non-trivial differences in performance
between this method and ordinary get_entry, it
has been set as its own method.
4968725
@tnm tnm Add a test for the nil case c5de6a9
@vmg vmg merged commit 47678c7 into libgit2:development

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. @tnm

    Introduce get_entry_by_oid.

    tnm committed
    Based on non-trivial differences in performance
    between this method and ordinary get_entry, it
    has been set as its own method.
  2. @tnm

    Add a test for the nil case

    tnm committed
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.