Skip to content

Commit 72d783b

Browse files
Start working on better Odb, Refdb and custom backend support.
1 parent 7d00d84 commit 72d783b

File tree

10 files changed

+472
-0
lines changed

10 files changed

+472
-0
lines changed

ext/rugged/rugged.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ void Init_rugged(void)
482482
Init_rugged_cred();
483483
Init_rugged_backend();
484484

485+
Init_rugged_refdb();
486+
Init_rugged_refdb_backend();
487+
Init_rugged_refdb_backend_fs();
488+
489+
Init_rugged_odb();
490+
Init_rugged_odb_backend();
491+
Init_rugged_odb_backend_loose();
492+
485493
/*
486494
* Sort the repository contents in no particular ordering;
487495
* this sorting is arbitrary, implementation-specific

ext/rugged/rugged.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include <assert.h>
4141
#include <git2.h>
4242
#include <git2/odb_backend.h>
43+
#include <git2/sys/refdb_backend.h>
44+
#include <git2/sys/odb_backend.h>
4345

4446
#define rb_str_new_utf8(str) rb_enc_str_new(str, strlen(str), rb_utf8_encoding())
4547
#define CSTR2SYM(s) (ID2SYM(rb_intern((s))))
@@ -75,6 +77,12 @@ void Init_rugged_diff_line(void);
7577
void Init_rugged_blame(void);
7678
void Init_rugged_cred(void);
7779
void Init_rugged_backend(void);
80+
void Init_rugged_refdb(void);
81+
void Init_rugged_refdb_backend(void);
82+
void Init_rugged_refdb_backend_fs(void);
83+
void Init_rugged_odb(void);
84+
void Init_rugged_odb_backend(void);
85+
void Init_rugged_odb_backend_loose(void);
7886

7987
VALUE rb_git_object_init(git_otype type, int argc, VALUE *argv, VALUE self);
8088

ext/rugged/rugged_odb.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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_mRugged;
28+
VALUE rb_cRuggedOdb;
29+
30+
/*
31+
* call-seq:
32+
* Odb.new() -> odb
33+
*
34+
* Create a new object database with no backend.
35+
*
36+
* Before the object database can be used, backends
37+
* needs to be assigned using `Odb#add_backend`.
38+
*/
39+
static VALUE rb_git_odb_new(VALUE klass) {
40+
git_odb *odb;
41+
rugged_exception_check(git_odb_new(&odb));
42+
return Data_Wrap_Struct(klass, NULL, git_odb_free, odb);
43+
}
44+
45+
/*
46+
* call-seq:
47+
* Odb.open(dir) -> odb
48+
*
49+
* Create a new object database with the default filesystem
50+
* backends.
51+
*
52+
* `dir` needs to boint to the objects folder to be used
53+
* by the filesystem backends.
54+
*/
55+
static VALUE rb_git_odb_open(VALUE klass, VALUE rb_path) {
56+
git_odb *odb;
57+
58+
rugged_exception_check(git_odb_open(&odb, StringValueCStr(rb_path)));
59+
60+
return Data_Wrap_Struct(klass, NULL, git_odb_free, odb);
61+
}
62+
63+
/*
64+
* call-seq:
65+
* odb.add_backend(backend) -> odb
66+
*
67+
* Set the backend to be used by the reference db.
68+
*
69+
* A backend can only be assigned once, and becomes unusable from that
70+
* point on. Trying to assign a backend a second time will raise an
71+
* exception.
72+
*/
73+
static VALUE rb_git_odb_add_backend(VALUE self, VALUE rb_backend, VALUE rb_priority)
74+
{
75+
git_odb *odb;
76+
git_odb_backend *backend;
77+
78+
Data_Get_Struct(self, git_odb, odb);
79+
Data_Get_Struct(rb_backend, git_odb_backend, backend);
80+
81+
if (!backend)
82+
rb_exc_raise(rb_exc_new_cstr(rb_eRuntimeError, "Can not reuse odb backend instances"));
83+
84+
rugged_exception_check(git_odb_add_backend(odb, backend, NUM2INT(rb_priority)));
85+
86+
// libgit2 has taken ownership of the backend, so we should make sure
87+
// we don't try to free it.
88+
((struct RData *)rb_backend)->data = NULL;
89+
90+
return self;
91+
}
92+
93+
static int cb_odb__each(const git_oid *id, void *data)
94+
{
95+
char out[40];
96+
struct rugged_cb_payload *payload = data;
97+
98+
git_oid_fmt(out, id);
99+
rb_protect(rb_yield, rb_str_new(out, 40), &payload->exception);
100+
101+
return payload->exception ? GIT_ERROR : GIT_OK;
102+
}
103+
104+
static VALUE rb_git_odb_each(VALUE self)
105+
{
106+
git_odb *odb;
107+
int error;
108+
struct rugged_cb_payload payload = { self, 0 };
109+
110+
Data_Get_Struct(self, git_odb, odb);
111+
112+
error = git_odb_foreach(odb, &cb_odb__each, &payload);
113+
114+
if (payload.exception)
115+
rb_jump_tag(payload.exception);
116+
rugged_exception_check(error);
117+
118+
return Qnil;
119+
}
120+
121+
void Init_rugged_odb(void)
122+
{
123+
rb_cRuggedOdb = rb_define_class_under(rb_mRugged, "Odb", rb_cObject);
124+
125+
rb_define_singleton_method(rb_cRuggedOdb, "new", rb_git_odb_new, 0);
126+
rb_define_singleton_method(rb_cRuggedOdb, "open", rb_git_odb_open, 1);
127+
128+
rb_define_method(rb_cRuggedOdb, "add_backend", rb_git_odb_add_backend, 2);
129+
rb_define_method(rb_cRuggedOdb, "each", rb_git_odb_each, 0);
130+
}

ext/rugged/rugged_odb_backend.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_cRuggedOdb;
28+
VALUE rb_cRuggedOdbBackend;
29+
30+
void Init_rugged_odb_backend(void)
31+
{
32+
rb_cRuggedOdbBackend = rb_define_class_under(rb_cRuggedOdb, "Backend", rb_cObject);
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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_cRuggedOdbBackend;
28+
VALUE rb_cRuggedOdbBackendLoose;
29+
30+
void rb_git_odb_backend__free(git_odb_backend *backend)
31+
{
32+
if (backend) backend->free(backend);
33+
}
34+
35+
static VALUE rb_git_odb_backend_loose_new(VALUE self, VALUE rb_path, VALUE rb_compression_level, VALUE rb_do_fsync, VALUE rb_dir_mode, VALUE rb_file_mode)
36+
{
37+
git_odb_backend *backend;
38+
39+
rugged_exception_check(git_odb_backend_loose(&backend, StringValueCStr(rb_path), NUM2INT(rb_compression_level), NUM2INT(rb_do_fsync), NUM2INT(rb_dir_mode), NUM2INT(rb_file_mode)));
40+
41+
return Data_Wrap_Struct(self, NULL, rb_git_odb_backend__free, backend);
42+
}
43+
44+
void Init_rugged_odb_backend_loose(void)
45+
{
46+
rb_cRuggedOdbBackendLoose = rb_define_class_under(rb_cRuggedOdbBackend, "Loose", rb_cRuggedOdbBackend);
47+
48+
rb_define_singleton_method(rb_cRuggedOdbBackendLoose, "new", rb_git_odb_backend_loose_new, 5);
49+
}

ext/rugged/rugged_refdb.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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_mRugged;
28+
VALUE rb_cRuggedRefdb;
29+
30+
/*
31+
* call-seq:
32+
* Refdb.new(repository) -> refdb
33+
*
34+
* Create a new reference database with no backends.
35+
*
36+
* Before the reference database can be used, a backend
37+
* needs to be assigned using `Refdb#backend=`.
38+
*/
39+
static VALUE rb_git_refdb_new(VALUE klass, VALUE rb_repo) {
40+
git_refdb *refdb;
41+
git_repository *repo;
42+
43+
Data_Get_Struct(rb_repo, git_repository, repo);
44+
45+
rugged_exception_check(git_refdb_new(&refdb, repo));
46+
47+
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
48+
}
49+
50+
/*
51+
* call-seq:
52+
* Refdb.open(repository) -> refdb
53+
*
54+
* Create a new reference database with the default filesystem
55+
* backend.
56+
*/
57+
static VALUE rb_git_refdb_open(VALUE klass, VALUE rb_repo) {
58+
git_refdb *refdb;
59+
git_repository *repo;
60+
61+
Data_Get_Struct(rb_repo, git_repository, repo);
62+
63+
rugged_exception_check(git_refdb_open(&refdb, repo));
64+
65+
return Data_Wrap_Struct(klass, NULL, git_refdb_free, refdb);
66+
}
67+
68+
/*
69+
* call-seq:
70+
* refdb.backend = backend
71+
*
72+
* Set the backend to be used by the reference db.
73+
*
74+
* A backend can only be assigned once, and becomes unusable from that
75+
* point on. Trying to assign a backend a second time will raise an
76+
* exception.
77+
*/
78+
static VALUE rb_git_refdb_set_backend(VALUE self, VALUE rb_backend)
79+
{
80+
git_refdb *refdb;
81+
git_refdb_backend *backend;
82+
83+
Data_Get_Struct(self, git_refdb, refdb);
84+
Data_Get_Struct(rb_backend, git_refdb_backend, backend);
85+
86+
if (!backend)
87+
rb_exc_raise(rb_exc_new_cstr(rb_eRuntimeError, "Can not reuse refdb backend instances"));
88+
89+
rugged_exception_check(git_refdb_set_backend(refdb, backend));
90+
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;
94+
95+
return Qnil;
96+
}
97+
98+
void Init_rugged_refdb(void)
99+
{
100+
rb_cRuggedRefdb = rb_define_class_under(rb_mRugged, "Refdb", rb_cObject);
101+
102+
rb_define_singleton_method(rb_cRuggedRefdb, "new", rb_git_refdb_new, 1);
103+
rb_define_singleton_method(rb_cRuggedRefdb, "open", rb_git_refdb_open, 1);
104+
105+
rb_define_method(rb_cRuggedRefdb, "backend=", rb_git_refdb_set_backend, 1);
106+
}

ext/rugged/rugged_refdb_backend.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_cRuggedRefdb;
28+
VALUE rb_cRuggedRefdbBackend;
29+
30+
void Init_rugged_refdb_backend(void)
31+
{
32+
rb_cRuggedRefdbBackend = rb_define_class_under(rb_cRuggedRefdb, "Backend", rb_cObject);
33+
}

0 commit comments

Comments
 (0)