Skip to content

Commit

Permalink
raw to hex now working, too
Browse files Browse the repository at this point in the history
  • Loading branch information
schacon committed May 8, 2010
1 parent a029af9 commit a94b1c0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 0 additions & 6 deletions Rakefile
Expand Up @@ -86,12 +86,6 @@ end
# Documentation
#

begin
require 'sdoc_helpers'
rescue LoadError
warn "sdoc support not enabled. Please gem install sdoc-helpers."
end

desc "Publish to GitHub Pages"
task :pages => [ "man:build" ] do
Dir['man/*.html'].each do |f|
Expand Down
18 changes: 15 additions & 3 deletions ext/ribbit/ribbit.c
Expand Up @@ -8,9 +8,20 @@ static VALUE
hex_to_oid(VALUE self, VALUE hex)
{
Check_Type(hex, T_STRING);
git_oid *oid;
git_oid_mkstr(oid, RSTRING_PTR(hex));
return rb_str_new2(oid->id);
git_oid oid;
git_oid_mkstr(&oid, RSTRING_PTR(hex));
return rb_str_new((&oid)->id, 20);
}

static VALUE
oid_to_hex(VALUE self, VALUE raw)
{
Check_Type(raw, T_STRING);
git_oid oid;
git_oid_mkraw(&oid, RSTRING_PTR(raw));
char out[40];
git_oid_fmt(out, &oid);
return rb_str_new(out, 40);
}

// git_oid_mkstr(&id1, hello_id);
Expand All @@ -23,5 +34,6 @@ Init_ribbit()
rb_cRibbitLib = rb_define_class_under(rb_cRibbit, "Lib", rb_cObject);

rb_define_module_function(rb_cRibbitLib, "hex_to_oid", hex_to_oid, 1);
rb_define_module_function(rb_cRibbitLib, "oid_to_hex", oid_to_hex, 1);
}

8 changes: 7 additions & 1 deletion test/oid_test.rb
Expand Up @@ -8,7 +8,13 @@
test "can convert hex into raw sha" do
oid = Ribbit::Lib.hex_to_oid("ce08fe4884650f067bd5703b6a59a8b3b3c99a09")
b64oid = Base64.encode64(oid).strip
assert_equal b64oid, "zgj+SIRlDwZ71XA7almos7PJmgk="
assert_equal "zgj+SIRlDwZ71XA7almos7PJmgk=", b64oid
end

test "can convert raw sha into hex" do
oid = Base64.decode64("FqASNFZ4mrze9Ld1ITwjqL109eA=")
hex = Ribbit::Lib.oid_to_hex(oid)
assert_equal "16a0123456789abcdef4b775213c23a8bd74f5e0", hex
end

end

0 comments on commit a94b1c0

Please sign in to comment.