Skip to content

Commit e1d6bab

Browse files
Implement ref lookups for custom refdb backends.
1 parent 862198e commit e1d6bab

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

ext/rugged/rugged_refdb_backend_custom.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ static int rugged_refdb_backend_custom__exists(
4545
return GIT_OK;
4646
}
4747

48+
static int rugged_refdb_backend_custom__lookup(
49+
git_reference **out,
50+
git_refdb_backend *_backend,
51+
const char *ref_name)
52+
{
53+
rugged_refdb_backend_custom *backend = (rugged_refdb_backend_custom *)_backend;
54+
git_oid oid;
55+
VALUE rb_result;
56+
57+
// TODO: Proper exception handling
58+
rb_result = rb_funcall(backend->self, rb_intern("lookup"), 1, rb_str_new2(ref_name));
59+
if (TYPE(rb_result) == T_STRING) {
60+
// TODO: Handle symbolic refs as well
61+
// TODO: Proper exception handling
62+
rugged_exception_check(git_oid_fromstr(&oid, StringValueCStr(rb_result)));
63+
*out = git_reference__alloc(ref_name, &oid, NULL);
64+
return GIT_OK;
65+
} else {
66+
// TODO: Better error handling
67+
*out = NULL;
68+
return GIT_ENOTFOUND;
69+
}
70+
}
71+
4872
static int rugged_refdb_backend_custom__compress(git_refdb_backend *_backend)
4973
{
5074
rugged_refdb_backend_custom *backend = (rugged_refdb_backend_custom *)_backend;
@@ -68,6 +92,7 @@ static VALUE rb_git_refdb_backend_custom_new(VALUE self, VALUE rb_repo)
6892

6993
backend->parent.exists = &rugged_refdb_backend_custom__exists;
7094
backend->parent.compress = &rugged_refdb_backend_custom__compress;
95+
backend->parent.lookup = &rugged_refdb_backend_custom__lookup;
7196
backend->parent.free = xfree;
7297

7398
backend->self = Data_Wrap_Struct(self, NULL, rb_git_refdb_backend__free, backend);

test/refdb_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,26 @@ def test_custom_backend
4040
assert_equal 1, compress_calls
4141
end
4242
end
43+
44+
class RefdbBackendCustomTest < Rugged::TestCase
45+
def setup
46+
@repo = FixtureRepo.from_rugged("testrepo.git")
47+
@refdb = Rugged::Refdb.new(@repo)
48+
@backend = Rugged::Refdb::Backend::Custom.new(@repo)
49+
@refdb.backend = @backend
50+
@repo.refdb = @refdb
51+
end
52+
53+
def test_lookup
54+
@backend.send(:define_singleton_method, :lookup) do |ref_name|
55+
"1385f264afb75a56a5bec74243be9b367ba4ca08" if ref_name == "refs/heads/master"
56+
end
57+
58+
ref = @repo.references["refs/heads/master"]
59+
assert ref
60+
assert_equal "refs/heads/master", ref.name
61+
assert_equal "1385f264afb75a56a5bec74243be9b367ba4ca08", ref.target_id
62+
63+
assert_nil @repo.references["refs/heads/development"]
64+
end
65+
end

0 commit comments

Comments
 (0)