Skip to content

Commit 644091c

Browse files
Add a base class for custom Refdb backends.
1 parent 72d783b commit 644091c

File tree

6 files changed

+143
-29
lines changed

6 files changed

+143
-29
lines changed

ext/rugged/rugged.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ void Init_rugged(void)
484484

485485
Init_rugged_refdb();
486486
Init_rugged_refdb_backend();
487+
Init_rugged_refdb_backend_custom();
487488
Init_rugged_refdb_backend_fs();
488489

489490
Init_rugged_odb();

ext/rugged/rugged.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ void Init_rugged_cred(void);
7979
void Init_rugged_backend(void);
8080
void Init_rugged_refdb(void);
8181
void Init_rugged_refdb_backend(void);
82+
void Init_rugged_refdb_backend_custom(void);
8283
void Init_rugged_refdb_backend_fs(void);
8384
void Init_rugged_odb(void);
8485
void Init_rugged_odb_backend(void);

ext/rugged/rugged_refdb.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ VALUE rb_cRuggedRefdb;
3737
* needs to be assigned using `Refdb#backend=`.
3838
*/
3939
static VALUE rb_git_refdb_new(VALUE klass, VALUE rb_repo) {
40-
git_refdb *refdb;
41-
git_repository *repo;
40+
git_refdb *refdb;
41+
git_repository *repo;
4242

43-
Data_Get_Struct(rb_repo, git_repository, repo);
43+
Data_Get_Struct(rb_repo, git_repository, repo);
4444

45-
rugged_exception_check(git_refdb_new(&refdb, repo));
45+
rugged_exception_check(git_refdb_new(&refdb, repo));
4646

47-
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
47+
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
4848
}
4949

5050
/*
@@ -55,14 +55,14 @@ static VALUE rb_git_refdb_new(VALUE klass, VALUE rb_repo) {
5555
* backend.
5656
*/
5757
static VALUE rb_git_refdb_open(VALUE klass, VALUE rb_repo) {
58-
git_refdb *refdb;
59-
git_repository *repo;
58+
git_refdb *refdb;
59+
git_repository *repo;
6060

61-
Data_Get_Struct(rb_repo, git_repository, repo);
61+
Data_Get_Struct(rb_repo, git_repository, repo);
6262

63-
rugged_exception_check(git_refdb_open(&refdb, repo));
63+
rugged_exception_check(git_refdb_open(&refdb, repo));
6464

65-
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
65+
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
6666
}
6767

6868
/*
@@ -77,30 +77,46 @@ static VALUE rb_git_refdb_new(VALUE klass, VALUE rb_repo) {
7777
*/
7878
static VALUE rb_git_refdb_set_backend(VALUE self, VALUE rb_backend)
7979
{
80-
git_refdb *refdb;
81-
git_refdb_backend *backend;
80+
git_refdb *refdb;
81+
git_refdb_backend *backend;
8282

83-
Data_Get_Struct(self, git_refdb, refdb);
84-
Data_Get_Struct(rb_backend, git_refdb_backend, backend);
83+
Data_Get_Struct(self, git_refdb, refdb);
84+
Data_Get_Struct(rb_backend, git_refdb_backend, backend);
8585

86-
if (!backend)
87-
rb_exc_raise(rb_exc_new_cstr(rb_eRuntimeError, "Can not reuse refdb backend instances"));
86+
if (!backend)
87+
rb_exc_raise(rb_exc_new_cstr(rb_eRuntimeError, "Can not reuse refdb backend instances"));
8888

89-
rugged_exception_check(git_refdb_set_backend(refdb, backend));
89+
rugged_exception_check(git_refdb_set_backend(refdb, backend));
9090

91-
// libgit2 has taken ownership of the backend, so we should make sure
92-
// we don't try to free it.
93-
((struct RData *)rb_backend)->data = NULL;
91+
// libgit2 has taken ownership of the backend, so we should make sure
92+
// we don't try to free it.
93+
((struct RData *)rb_backend)->data = NULL;
9494

95-
return Qnil;
95+
return Qnil;
96+
}
97+
98+
/*
99+
* call-seq:
100+
* refdb.compress -> nil
101+
*/
102+
static VALUE rb_git_refdb_compress(VALUE self)
103+
{
104+
git_refdb *refdb;
105+
106+
Data_Get_Struct(self, git_refdb, refdb);
107+
108+
rugged_exception_check(git_refdb_compress(refdb));
109+
110+
return Qnil;
96111
}
97112

98113
void Init_rugged_refdb(void)
99114
{
100115
rb_cRuggedRefdb = rb_define_class_under(rb_mRugged, "Refdb", rb_cObject);
101116

102-
rb_define_singleton_method(rb_cRuggedRefdb, "new", rb_git_refdb_new, 1);
117+
rb_define_singleton_method(rb_cRuggedRefdb, "new", rb_git_refdb_new, 1);
103118
rb_define_singleton_method(rb_cRuggedRefdb, "open", rb_git_refdb_open, 1);
104119

105120
rb_define_method(rb_cRuggedRefdb, "backend=", rb_git_refdb_set_backend, 1);
121+
rb_define_method(rb_cRuggedRefdb, "compress", rb_git_refdb_compress, 0);
106122
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2015 GitHub, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "rugged.h"
26+
27+
extern VALUE rb_cRuggedRefdbBackend;
28+
VALUE rb_cRuggedRefdbBackendCustom;
29+
30+
typedef struct {
31+
git_refdb_backend parent;
32+
VALUE self;
33+
} rugged_refdb_backend_custom;
34+
35+
static int rugged_refdb_backend_custom__exists(
36+
int *exists,
37+
git_refdb_backend *_backend,
38+
const char *ref_name)
39+
{
40+
rugged_refdb_backend_custom *backend = (rugged_refdb_backend_custom *)_backend;
41+
42+
// TODO: Proper exception handling
43+
*exists = RTEST(rb_funcall(backend->self, rb_intern("exists"), 1, rb_str_new2(ref_name)));
44+
45+
return GIT_OK;
46+
}
47+
48+
static int rugged_refdb_backend_custom__compress(git_refdb_backend *_backend)
49+
{
50+
rugged_refdb_backend_custom *backend = (rugged_refdb_backend_custom *)_backend;
51+
52+
// TODO: Proper exception handling
53+
rb_funcall(backend->self, rb_intern("compress"), 0);
54+
55+
return GIT_OK;
56+
}
57+
58+
static void rb_git_refdb_backend__free(git_refdb_backend *backend)
59+
{
60+
if (backend) backend->free(backend);
61+
}
62+
63+
static VALUE rb_git_refdb_backend_custom_new(VALUE self, VALUE rb_repo)
64+
{
65+
rugged_refdb_backend_custom *backend;
66+
67+
backend = xcalloc(1, sizeof(rugged_refdb_backend_custom));
68+
69+
backend->parent.exists = &rugged_refdb_backend_custom__exists;
70+
backend->parent.compress = &rugged_refdb_backend_custom__compress;
71+
backend->parent.free = xfree;
72+
73+
backend->self = Data_Wrap_Struct(self, NULL, rb_git_refdb_backend__free, backend);
74+
return backend->self;
75+
}
76+
77+
void Init_rugged_refdb_backend_custom(void)
78+
{
79+
rb_cRuggedRefdbBackendCustom = rb_define_class_under(rb_cRuggedRefdbBackend, "Custom", rb_cRuggedRefdbBackend);
80+
81+
rb_define_singleton_method(rb_cRuggedRefdbBackendCustom, "new", rb_git_refdb_backend_custom_new, 1);
82+
}

ext/rugged/rugged_refdb_backend_fs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ VALUE rb_cRuggedRefdbBackendFileSystem;
2929

3030
void rb_git_refdb_backend__free(git_refdb_backend *backend)
3131
{
32-
if (backend) backend->free(backend);
32+
if (backend) backend->free(backend);
3333
}
3434

3535
static VALUE rb_git_refdb_backend_fs_new(VALUE self, VALUE rb_repo)
3636
{
37-
git_repository *repo;
38-
git_refdb_backend *backend;
37+
git_repository *repo;
38+
git_refdb_backend *backend;
3939

40-
Data_Get_Struct(rb_repo, git_repository, repo);
40+
Data_Get_Struct(rb_repo, git_repository, repo);
4141

42-
rugged_exception_check(git_refdb_backend_fs(&backend, repo));
42+
rugged_exception_check(git_refdb_backend_fs(&backend, repo));
4343

44-
return Data_Wrap_Struct(self, NULL, rb_git_refdb_backend__free, backend);
44+
return Data_Wrap_Struct(self, NULL, rb_git_refdb_backend__free, backend);
4545
}
4646

4747
void Init_rugged_refdb_backend_fs(void)
4848
{
4949
rb_cRuggedRefdbBackendFileSystem = rb_define_class_under(rb_cRuggedRefdbBackend, "FileSystem", rb_cRuggedRefdbBackend);
5050

51-
rb_define_singleton_method(rb_cRuggedRefdbBackendFileSystem, "new", rb_git_refdb_backend_fs_new, 1);
51+
rb_define_singleton_method(rb_cRuggedRefdbBackendFileSystem, "new", rb_git_refdb_backend_fs_new, 1);
5252
}

test/refdb_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
require "test_helper"
22

3+
class TestBackend < Rugged::Refdb::Backend::Custom
4+
def compress
5+
puts "compress!"
6+
end
7+
end
8+
39
class RefdbTest < Rugged::TestCase
410
def setup
511
@repo = FixtureRepo.from_rugged("testrepo.git")
@@ -24,4 +30,12 @@ def test_set_backend_reuse_error
2430
refdb.backend = backend
2531
end
2632
end
33+
34+
def test_custom_backend
35+
refdb = Rugged::Refdb.new(@repo)
36+
backend = TestBackend.new(@repo)
37+
38+
refdb.backend = backend
39+
refdb.compress
40+
end
2741
end

0 commit comments

Comments
 (0)