Skip to content

Commit 0728fa3

Browse files
author
Vicent Marti
committed
Merge pull request #514 from libgit2/brianmario/add-valid-full-oid-helper
Add Rugged.valid_full_oid? helper method
2 parents 51eb13f + e849be6 commit 0728fa3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ext/rugged/rugged.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ static VALUE rb_git_features(VALUE self)
109109
return ret_arr;
110110
}
111111

112+
/*
113+
* call-seq:
114+
* Rugged.valid_full_oid?(oid) -> true or false
115+
*
116+
* Checks to see if a string contains a full 40-character sha1.
117+
*
118+
* Rugged.valid_full_oid?('d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f')
119+
* #=> true
120+
*/
121+
static VALUE rb_git_valid_full_oid(VALUE self, VALUE hex)
122+
{
123+
git_oid oid;
124+
int errorcode;
125+
126+
Check_Type(hex, T_STRING);
127+
errorcode = git_oid_fromstr(&oid, StringValueCStr(hex));
128+
if (errorcode < 0) {
129+
return Qfalse;
130+
} else {
131+
return Qtrue;
132+
}
133+
}
134+
112135
/*
113136
* call-seq:
114137
* Rugged.hex_to_raw(oid) -> raw_buffer
@@ -421,6 +444,7 @@ void Init_rugged(void)
421444

422445
rb_define_module_function(rb_mRugged, "libgit2_version", rb_git_libgit2_version, 0);
423446
rb_define_module_function(rb_mRugged, "features", rb_git_features, 0);
447+
rb_define_module_function(rb_mRugged, "valid_full_oid?", rb_git_valid_full_oid, 1);
424448
rb_define_module_function(rb_mRugged, "hex_to_raw", rb_git_hex_to_raw, 1);
425449
rb_define_module_function(rb_mRugged, "raw_to_hex", rb_git_raw_to_hex, 1);
426450
rb_define_module_function(rb_mRugged, "minimize_oid", rb_git_minimize_oid, -1);

test/lib_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def test_features
4444
assert features.is_a? Array
4545
end
4646

47+
def test_valid_full_oid
48+
assert Rugged.valid_full_oid?("ce08fe4884650f067bd5703b6a59a8b3b3c99a09")
49+
refute Rugged.valid_full_oid?("nope")
50+
refute Rugged.valid_full_oid?("ce08fe4884650f067bd5703b6a59a8b3b3c99a0")
51+
end
52+
4753
def test_hex_to_raw_oid
4854
raw = Rugged::hex_to_raw("ce08fe4884650f067bd5703b6a59a8b3b3c99a09")
4955
b64raw = Base64.encode64(raw).strip

0 commit comments

Comments
 (0)