Skip to content

Commit 130535a

Browse files
committed
repo: Rewrite expand_oids to use the new API
1 parent d405356 commit 130535a

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

ext/rugged/rugged_repo.c

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,59 +1058,64 @@ static VALUE rb_git_repo_expand_oids(int argc, VALUE *argv, VALUE self)
10581058
{
10591059
VALUE rb_result, rb_oids, rb_expected_type;
10601060

1061-
git_otype expected_type = GIT_OBJ_ANY;
1062-
10631061
git_repository *repo;
1064-
git_oid oid;
10651062
git_odb *odb;
1066-
int i, error;
1063+
git_odb_expand_id *expand;
1064+
long i, expand_count;
1065+
int error;
10671066

10681067
Data_Get_Struct(self, git_repository, repo);
1069-
10701068
rb_scan_args(argc, argv, "11", &rb_oids, &rb_expected_type);
10711069

10721070
Check_Type(rb_oids, T_ARRAY);
1073-
expected_type = rugged_otype_get(rb_expected_type);
1071+
expand_count = RARRAY_LEN(rb_oids);
1072+
expand = alloca(expand_count * sizeof(git_odb_expand_id));
10741073

1075-
error = git_repository_odb(&odb, repo);
1076-
rugged_exception_check(error);
1074+
for (i = 0; i < expand_count; ++i) {
1075+
VALUE rb_hex = rb_ary_entry(rb_oids, i);
1076+
Check_Type(rb_hex, T_STRING);
10771077

1078-
rb_result = rb_hash_new();
1078+
rugged_exception_check(
1079+
git_oid_fromstrn(&expand[i].id, RSTRING_PTR(rb_hex), RSTRING_LEN(rb_hex))
1080+
);
1081+
expand[i].length = RSTRING_LEN(rb_hex);
1082+
}
10791083

1080-
for (i = 0; i < RARRAY_LEN(rb_oids); ++i) {
1081-
VALUE hex_oid = rb_ary_entry(rb_oids, i);
1082-
git_oid found_oid;
1084+
if (TYPE(rb_expected_type) == T_ARRAY) {
1085+
if (RARRAY_LEN(rb_expected_type) != expand_count)
1086+
rb_raise(rb_eRuntimeError,
1087+
"the `object_type` array must be the same length as the `oids` array");
10831088

1084-
if (TYPE(hex_oid) != T_STRING) {
1085-
git_odb_free(odb);
1086-
rb_raise(rb_eTypeError, "Expected a SHA1 OID");
1089+
for (i = 0; i < expand_count; ++i) {
1090+
VALUE rb_type = rb_ary_entry(rb_expected_type, i);
1091+
expand[i].type = rugged_otype_get(rb_type);
10871092
}
1093+
} else {
1094+
git_otype expected_type = GIT_OBJ_ANY;
10881095

1089-
error = git_oid_fromstrn(&oid, RSTRING_PTR(hex_oid), RSTRING_LEN(hex_oid));
1090-
if (error < 0) {
1091-
git_odb_free(odb);
1092-
rugged_exception_check(error);
1093-
}
1096+
if (!NIL_P(rb_expected_type))
1097+
expected_type = rugged_otype_get(rb_expected_type);
10941098

1095-
error = git_odb_exists_prefix(&found_oid, odb, &oid, RSTRING_LEN(hex_oid));
1099+
for (i = 0; i < expand_count; ++i)
1100+
expand[i].type = expected_type;
1101+
}
10961102

1097-
if (!error) {
1098-
if (expected_type != GIT_OBJ_ANY) {
1099-
size_t found_size;
1100-
git_otype found_type;
1103+
error = git_repository_odb(&odb, repo);
1104+
rugged_exception_check(error);
11011105

1102-
if (git_odb_read_header(&found_size, &found_type, odb, &found_oid) < 0)
1103-
continue;
1106+
error = git_odb_expand_ids(odb, expand, (size_t)expand_count);
1107+
git_odb_free(odb);
1108+
rugged_exception_check(error);
11041109

1105-
if (found_type != expected_type)
1106-
continue;
1107-
}
1110+
rb_result = rb_hash_new();
11081111

1109-
rb_hash_aset(rb_result, hex_oid, rugged_create_oid(&found_oid));
1112+
for (i = 0; i < expand_count; ++i) {
1113+
if (expand[i].length) {
1114+
rb_hash_aset(rb_result,
1115+
rb_ary_entry(rb_oids, i), rugged_create_oid(&expand[i].id));
11101116
}
11111117
}
11121118

1113-
git_odb_free(odb);
11141119
return rb_result;
11151120
}
11161121

test/repo_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ def test_expand_objects
256256
def test_expand_and_filter_objects
257257
assert_equal 2, @repo.expand_oids(['a4a7dce8', '1385f264af']).size
258258
assert_equal 1, @repo.expand_oids(['a4a7dce8', '1385f264af'], :commit).size
259+
assert_equal 2, @repo.expand_oids(['a4a7dce8', '1385f264af'], ['commit', 'blob']).size
260+
assert_equal 1, @repo.expand_oids(['a4a7dce8', '1385f264af'], [:commit, :tag]).size
261+
262+
assert_raises RuntimeError do
263+
@repo.expand_oids(['a4a7dce8', '1385f264af'], [:commit, :tag, :commit]).size
264+
end
265+
266+
assert_raises RuntimeError do
267+
@repo.expand_oids(['a4a7dce8', '1385f264af'], [:commit]).size
268+
end
259269
end
260270

261271
def test_descendant_of

0 commit comments

Comments
 (0)