Permalink
Browse files

first commit object bindings

  • Loading branch information...
1 parent e3d0417 commit 20b2a100b25c170a959fb715475fef2de38cad5e @schacon committed Aug 18, 2010
Showing with 65 additions and 67 deletions.
  1. +0 −64 benchmarks.rb
  2. +48 −3 ext/ribbit/ribbit.c
  3. +17 −0 test/commit_test.rb
View
@@ -1,64 +0,0 @@
-require 'benchmark'
-require 'rubygems'
-require 'grit'
-require 'pp'
-
-dir = File.dirname(File.expand_path(__FILE__))
-$LOAD_PATH.unshift dir + '/lib'
-require 'ribbit'
-
-n = 5000
-
-sha = "8496071c1b46c854b31185ea97743be6a8774479"
-psha = "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"
-raw = Ribbit::Lib.hex_to_raw(sha)
-
-repo = File.dirname(File.expand_path(__FILE__)) + "/test/fixtures/testrepo.git"
-
-psha = "f7c7e7614af3d26a05aeb324528b6d42076c4e6d"
-sha = "78af21ad7104d6f64b46cbc87f6ca944b8f412da"
-repo = "/Users/schacon/projects/github/.git"
-
-Benchmark.bm do |x|
-
- @grit = Grit::Repo.new(repo).git
- @ribbit = Ribbit::Odb.new(repo + '/objects')
-
- x.report("Grit object_exists? loose ") do
- @grit = Grit::Repo.new(repo).git
- 1.upto(n) { @grit.object_exists?(sha) }
- end
- x.report("Grit object_exists? packed") do
- @grit = Grit::Repo.new(repo).git
- 1.upto(n) { @grit.object_exists?(psha) }
- end
- x.report("Ribbit object_exists? loose ") do
- @ribbit = Ribbit::Odb.new(repo + '/objects')
- 1.upto(n) { @ribbit.exists(sha) }
- end
- x.report("Ribbit object_exists? packed") do
- @ribbit = Ribbit::Odb.new(repo + '/objects')
- 1.upto(n) { @ribbit.exists(psha) }
- end
-
-end
-
-exit
-
-n = 50000
-
-Benchmark.bm do |x|
-
- x.report("Ribbit Hex2Raw:") do
- 1.upto(n) { Ribbit::Lib.hex_to_raw(sha) }
- end
- x.report("Ruby Hex2Raw :") do
- 1.upto(n) { [sha].pack("H*") }
- end
- x.report("Ribbit Raw2Hex:") do
- 1.upto(n) { Ribbit::Lib.raw_to_hex(raw) }
- end
- x.report("Ruby Raw2Hex :") do
- 1.upto(n) { raw.unpack("H*")[0] }
- end
-end
View
@@ -177,6 +177,49 @@ static VALUE rb_git_object_read(VALUE self) {
return Qfalse;
}
+/*
+ * Ribbit Commit
+ */
+
+static VALUE rb_cRibbitCommit;
+
+static VALUE rb_git_commit_init(VALUE self, VALUE rb_repo, VALUE hex) {
+ rb_iv_set(self, "@sha", hex);
+ rb_iv_set(self, "repo", rb_repo);
+
+ rb_git_object_init(self, rb_repo, hex);
+
+ git_repository *repo;
+ repo = (git_repository*)rb_iv_get(rb_repo, "repo");
+
+ git_oid oid;
+ git_oid_mkstr(&oid, RSTRING_PTR(hex));
+
+ git_commit *commit;
+ commit = git_commit_lookup(repo, &oid);
+ rb_iv_set(self, "commit", (VALUE)commit);
+}
+
+static VALUE rb_git_commit_message(VALUE self) {
+ git_commit *commit;
+ commit = (git_commit*)rb_iv_get(self, "commit");
+
+ const char *str_type;
+ str_type = git_commit_message(commit);
+
+ return rb_str_new2(str_type);
+}
+
+static VALUE rb_git_commit_message_short(VALUE self) {
+ git_commit *commit;
+ commit = (git_commit*)rb_iv_get(self, "commit");
+
+ const char *str_type;
+ str_type = git_commit_message_short(commit);
+
+ return rb_str_new2(str_type);
+}
+
/*
* Ribbit Revwalking
*/
@@ -289,9 +332,11 @@ Init_ribbit()
rb_attr(rb_cRibbitObject, rb_intern("object_type"), Qtrue, Qtrue, Qtrue);
rb_attr(rb_cRibbitObject, rb_intern("data"), Qtrue, Qtrue, Qtrue);
- //rb_cRibbitCommit = rb_define_class_under(rb_cRibbit, "Commit", rb_cObject);
- //rb_define_method(rb_cRibbitObject, "initialize", rb_git_object_init, 2);
- //rb_include_module(rb_cRibbitCommit, rb_cRibbitObject);
+ rb_cRibbitCommit = rb_define_class_under(rb_cRibbit, "Commit", rb_cObject);
+ rb_attr(rb_cRibbitCommit, rb_intern("sha"), Qtrue, Qtrue, Qtrue);
+ rb_define_method(rb_cRibbitCommit, "initialize", rb_git_commit_init, 2);
+ rb_define_method(rb_cRibbitCommit, "message", rb_git_commit_message, 0);
+ rb_define_method(rb_cRibbitCommit, "message_short", rb_git_commit_message_short, 0);
rb_cRibbitWalker = rb_define_class_under(rb_cRibbit, "Walker", rb_cObject);
rb_define_method(rb_cRibbitWalker, "initialize", rb_git_walker_init, 1);
View
@@ -0,0 +1,17 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+context "Ribbit::Commit tests" do
+ setup do
+ path = File.dirname(__FILE__) + '/fixtures/testrepo.git/objects'
+ @repo = Ribbit::Repository.new(path)
+ end
+
+ test "can read the commit data" do
+ sha = "8496071c1b46c854b31185ea97743be6a8774479"
+ obj = Ribbit::Commit.new(@repo, sha)
+
+ assert_equal obj.sha, sha
+ assert_equal obj.message, "testing\n"
+ assert_equal obj.message_short, "testing"
+ end
+end

0 comments on commit 20b2a10

Please sign in to comment.