Skip to content

Commit 29312a4

Browse files
authored
Merge pull request #998 from libgit2/cmn/typeddata
Use `TypedData` instead of `Data`
2 parents 74c4ba8 + 0dc3534 commit 29312a4

24 files changed

+440
-264
lines changed

ext/rugged/rugged_blame.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
extern VALUE rb_mRugged;
1111
VALUE rb_cRuggedBlame;
1212

13+
extern const rb_data_type_t rugged_repository_type;
14+
1315
static VALUE rb_git_blame_hunk_fromC(const git_blame_hunk *hunk)
1416
{
1517
VALUE rb_hunk;
@@ -82,6 +84,20 @@ static void rugged_parse_blame_options(git_blame_options *opts, git_repository *
8284
}
8385
}
8486

87+
static void rb_git_blame__free(void *data)
88+
{
89+
git_blame *blame = (git_blame *) data;
90+
git_blame_free(blame);
91+
}
92+
93+
const rb_data_type_t rugged_blame_type = {
94+
.wrap_struct_name = "Rugged::Blame",
95+
.function = {
96+
.dfree = rb_git_blame__free,
97+
},
98+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
99+
};
100+
85101
/*
86102
* call-seq:
87103
* Blame.new(repo, path, options = {}) -> blame
@@ -135,7 +151,7 @@ static VALUE rb_git_blame_new(int argc, VALUE *argv, VALUE klass)
135151
rb_scan_args(argc, argv, "20:", &rb_repo, &rb_path, &rb_options);
136152

137153
rugged_check_repo(rb_repo);
138-
Data_Get_Struct(rb_repo, git_repository, repo);
154+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
139155

140156
Check_Type(rb_path, T_STRING);
141157

@@ -145,7 +161,7 @@ static VALUE rb_git_blame_new(int argc, VALUE *argv, VALUE klass)
145161
&blame, repo, StringValueCStr(rb_path), &opts
146162
));
147163

148-
return Data_Wrap_Struct(klass, NULL, &git_blame_free, blame);
164+
return TypedData_Wrap_Struct(klass, &rugged_blame_type, blame);
149165
}
150166

151167
/*
@@ -160,7 +176,7 @@ static VALUE rb_git_blame_for_line(VALUE self, VALUE rb_line_no)
160176
git_blame *blame;
161177
int line_no;
162178

163-
Data_Get_Struct(self, git_blame, blame);
179+
TypedData_Get_Struct(self, git_blame, &rugged_blame_type, blame);
164180
Check_Type(rb_line_no, T_FIXNUM);
165181

166182
line_no = NUM2INT(rb_line_no);
@@ -184,7 +200,7 @@ static VALUE rb_git_blame_for_line(VALUE self, VALUE rb_line_no)
184200
static VALUE rb_git_blame_count(VALUE self)
185201
{
186202
git_blame *blame;
187-
Data_Get_Struct(self, git_blame, blame);
203+
TypedData_Get_Struct(self, git_blame, &rugged_blame_type, blame);
188204
return UINT2NUM(git_blame_get_hunk_count(blame));
189205
}
190206

@@ -210,7 +226,7 @@ static VALUE rb_git_blame_get_by_index(VALUE self, VALUE rb_index)
210226
int index;
211227
uint32_t blame_count;
212228

213-
Data_Get_Struct(self, git_blame, blame);
229+
TypedData_Get_Struct(self, git_blame, &rugged_blame_type, blame);
214230
Check_Type(rb_index, T_FIXNUM);
215231

216232
index = NUM2INT(rb_index);
@@ -249,7 +265,7 @@ static VALUE rb_git_blame_each(VALUE self)
249265

250266
RETURN_SIZED_ENUMERATOR(self, 0, 0, rugged_blame_enum_size);
251267

252-
Data_Get_Struct(self, git_blame, blame);
268+
TypedData_Get_Struct(self, git_blame, &rugged_blame_type, blame);
253269

254270
blame_count = git_blame_get_hunk_count(blame);
255271
for (i = 0; i < blame_count; ++i) {

ext/rugged/rugged_blob.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ VALUE rb_cRuggedBlob;
1818
VALUE rb_cRuggedBlobSig;
1919

2020
extern const rb_data_type_t rugged_object_type;
21+
extern const rb_data_type_t rugged_repository_type;
2122

2223
/*
2324
* call-seq:
@@ -143,7 +144,7 @@ static VALUE rb_git_blob_from_buffer(VALUE self, VALUE rb_repo, VALUE rb_buffer)
143144
Check_Type(rb_buffer, T_STRING);
144145
rugged_check_repo(rb_repo);
145146

146-
Data_Get_Struct(rb_repo, git_repository, repo);
147+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
147148

148149
error = git_blob_create_frombuffer(&oid, repo, RSTRING_PTR(rb_buffer), RSTRING_LEN(rb_buffer));
149150
rugged_exception_check(error);
@@ -170,7 +171,7 @@ static VALUE rb_git_blob_from_workdir(VALUE self, VALUE rb_repo, VALUE rb_path)
170171
FilePathValue(rb_path);
171172
rugged_check_repo(rb_repo);
172173

173-
Data_Get_Struct(rb_repo, git_repository, repo);
174+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
174175

175176
error = git_blob_create_fromworkdir(&oid, repo, StringValueCStr(rb_path));
176177
rugged_exception_check(error);
@@ -198,7 +199,7 @@ static VALUE rb_git_blob_from_disk(VALUE self, VALUE rb_repo, VALUE rb_path)
198199
FilePathValue(rb_path);
199200
rugged_check_repo(rb_repo);
200201

201-
Data_Get_Struct(rb_repo, git_repository, repo);
202+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
202203

203204
error = git_blob_create_fromdisk(&oid, repo, StringValueCStr(rb_path));
204205
rugged_exception_check(error);
@@ -253,7 +254,7 @@ static VALUE rb_git_blob_from_io(int argc, VALUE *argv, VALUE klass)
253254
rb_scan_args(argc, argv, "21", &rb_repo, &rb_io, &rb_hint_path);
254255

255256
rugged_check_repo(rb_repo);
256-
Data_Get_Struct(rb_repo, git_repository, repo);
257+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
257258

258259
if (!NIL_P(rb_hint_path)) {
259260
FilePathValue(rb_hint_path);
@@ -507,7 +508,7 @@ static VALUE rb_git_blob_to_buffer(int argc, VALUE *argv, VALUE self)
507508
rb_scan_args(argc, argv, "21", &rb_repo, &rb_sha1, &rb_max_bytes);
508509

509510
rugged_check_repo(rb_repo);
510-
Data_Get_Struct(rb_repo, git_repository, repo);
511+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
511512

512513
blob = (git_blob *)rugged_object_get(repo, rb_sha1, GIT_OBJ_BLOB);
513514

@@ -607,7 +608,7 @@ static VALUE rb_git_blob_merge_files(int argc, VALUE *argv, VALUE klass)
607608

608609
if (!NIL_P(rb_repo)) {
609610
rugged_check_repo(rb_repo);
610-
Data_Get_Struct(rb_repo, git_repository, repo);
611+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
611612
}
612613

613614
if (!NIL_P(rb_options))
@@ -638,6 +639,20 @@ static VALUE rb_git_blob_merge_files(int argc, VALUE *argv, VALUE klass)
638639
return rb_result;
639640
}
640641

642+
static void rb_git_hashsig__free(void *data)
643+
{
644+
git_hashsig *sig = (git_hashsig *) data;
645+
git_hashsig_free(sig);
646+
}
647+
648+
const rb_data_type_t rugged_hashsig_type = {
649+
.wrap_struct_name = "Rugged::Blob::HashSignature",
650+
.function = {
651+
.dfree = rb_git_hashsig__free,
652+
},
653+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
654+
};
655+
641656
static VALUE rb_git_blob_sig_new(int argc, VALUE *argv, VALUE klass)
642657
{
643658
int error, opts = 0;
@@ -664,7 +679,7 @@ static VALUE rb_git_blob_sig_new(int argc, VALUE *argv, VALUE klass)
664679

665680
rugged_exception_check(error);
666681

667-
return Data_Wrap_Struct(klass, NULL, &git_hashsig_free, sig);
682+
return TypedData_Wrap_Struct(klass, &rugged_hashsig_type, sig);
668683
}
669684

670685
static VALUE rb_git_blob_sig_compare(VALUE self, VALUE rb_sig_a, VALUE rb_sig_b)
@@ -678,8 +693,8 @@ static VALUE rb_git_blob_sig_compare(VALUE self, VALUE rb_sig_a, VALUE rb_sig_b)
678693
rb_raise(rb_eTypeError, "Expected Rugged::Blob::HashSignature");
679694
}
680695

681-
Data_Get_Struct(rb_sig_a, git_hashsig, sig_a);
682-
Data_Get_Struct(rb_sig_b, git_hashsig, sig_b);
696+
TypedData_Get_Struct(rb_sig_a, git_hashsig, &rugged_hashsig_type, sig_a);
697+
TypedData_Get_Struct(rb_sig_b, git_hashsig, &rugged_hashsig_type, sig_b);
683698

684699
result = git_hashsig_compare(sig_a, sig_b);
685700

ext/rugged/rugged_branch.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ extern VALUE rb_cRuggedObject;
1313
extern VALUE rb_cRuggedReference;
1414
VALUE rb_cRuggedBranch;
1515

16+
extern const rb_data_type_t rugged_reference_type;
17+
extern const rb_data_type_t rugged_repository_type;
18+
1619
static inline VALUE rugged_branch_new(VALUE owner, git_reference *ref)
1720
{
1821
return rugged_ref_new(rb_cRuggedBranch, owner, ref);
@@ -27,7 +30,7 @@ static inline VALUE rugged_branch_new(VALUE owner, git_reference *ref)
2730
static VALUE rb_git_branch_head_p(VALUE self)
2831
{
2932
git_reference *branch;
30-
Data_Get_Struct(self, git_reference, branch);
33+
TypedData_Get_Struct(self, git_reference, &rugged_reference_type, branch);
3134
return git_branch_is_head(branch) ? Qtrue : Qfalse;
3235
}
3336

@@ -44,7 +47,7 @@ static VALUE rb_git_branch_name(VALUE self)
4447
{
4548
git_reference *branch;
4649
const char *branch_name;
47-
Data_Get_Struct(self, git_reference, branch);
50+
TypedData_Get_Struct(self, git_reference, &rugged_reference_type, branch);
4851

4952
rugged_exception_check(git_branch_name(&branch_name, branch));
5053

@@ -58,7 +61,7 @@ static VALUE rb_git_branch__remote_name(VALUE rb_repo, const char *canonical_nam
5861
int error;
5962
VALUE result = Qnil;
6063

61-
Data_Get_Struct(rb_repo, git_repository, repo);
64+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
6265

6366
if ((error = git_branch_remote_name(&remote_name, repo, canonical_name)) == GIT_OK)
6467
result = rb_enc_str_new(remote_name.ptr, remote_name.size, rb_utf8_encoding());
@@ -86,7 +89,7 @@ static VALUE rb_git_branch_remote_name(VALUE self)
8689
git_reference *branch, *remote_ref;
8790
int error = 0;
8891

89-
Data_Get_Struct(self, git_reference, branch);
92+
TypedData_Get_Struct(self, git_reference, &rugged_reference_type, branch);
9093

9194
if (git_reference_is_remote(branch)) {
9295
remote_ref = branch;
@@ -116,7 +119,7 @@ static VALUE rb_git_branch_upstream(VALUE self)
116119
git_reference *branch, *upstream_branch;
117120
int error;
118121

119-
Data_Get_Struct(self, git_reference, branch);
122+
TypedData_Get_Struct(self, git_reference, &rugged_reference_type, branch);
120123

121124
if (git_reference_is_remote(branch))
122125
return Qnil;
@@ -145,12 +148,12 @@ static VALUE rb_git_branch_set_upstream(VALUE self, VALUE rb_branch)
145148
git_reference *branch, *target_branch;
146149
const char *target_branch_name;
147150

148-
Data_Get_Struct(self, git_reference, branch);
151+
TypedData_Get_Struct(self, git_reference, &rugged_reference_type, branch);
149152
if (!NIL_P(rb_branch)) {
150153
if (!rb_obj_is_kind_of(rb_branch, rb_cRuggedReference))
151154
rb_raise(rb_eTypeError, "Expecting a Rugged::Reference instance");
152155

153-
Data_Get_Struct(rb_branch, git_reference, target_branch);
156+
TypedData_Get_Struct(rb_branch, git_reference, &rugged_reference_type, target_branch);
154157

155158
rugged_exception_check(
156159
git_branch_name(&target_branch_name, target_branch)

ext/rugged/rugged_branch_collection.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ extern VALUE rb_cRuggedBranch;
1313

1414
VALUE rb_cRuggedBranchCollection;
1515

16+
extern const rb_data_type_t rugged_repository_type;
17+
1618
static inline VALUE rugged_branch_new(VALUE owner, git_reference *ref)
1719
{
1820
return rugged_ref_new(rb_cRuggedBranch, owner, ref);
@@ -119,7 +121,7 @@ static VALUE rb_git_branch_collection_create(int argc, VALUE *argv, VALUE self)
119121
rb_scan_args(argc, argv, "20:", &rb_name, &rb_target, &rb_options);
120122

121123
rugged_check_repo(rb_repo);
122-
Data_Get_Struct(rb_repo, git_repository, repo);
124+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
123125

124126
Check_Type(rb_name, T_STRING);
125127
Check_Type(rb_target, T_STRING);
@@ -164,7 +166,7 @@ static VALUE rb_git_branch_collection_aref(VALUE self, VALUE rb_name) {
164166
int error;
165167

166168
rugged_check_repo(rb_repo);
167-
Data_Get_Struct(rb_repo, git_repository, repo);
169+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
168170

169171
Check_Type(rb_name, T_STRING);
170172

@@ -193,7 +195,7 @@ static VALUE each_branch(int argc, VALUE *argv, VALUE self, int branch_names_onl
193195
if (!NIL_P(rb_filter))
194196
filter = parse_branch_type(rb_filter);
195197

196-
Data_Get_Struct(rb_repo, git_repository, repo);
198+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
197199

198200
error = git_branch_iterator_new(&iter, repo, filter);
199201
rugged_exception_check(error);
@@ -273,7 +275,7 @@ static VALUE rb_git_branch_collection_delete(VALUE self, VALUE rb_name_or_branch
273275
int error;
274276

275277
rugged_check_repo(rb_repo);
276-
Data_Get_Struct(rb_repo, git_repository, repo);
278+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
277279

278280
error = rugged_branch_lookup(&branch, repo, rb_name_or_branch);
279281
rugged_exception_check(error);
@@ -320,7 +322,7 @@ static VALUE rb_git_branch_collection_move(int argc, VALUE *argv, VALUE self)
320322
Check_Type(rb_new_branch_name, T_STRING);
321323

322324
rugged_check_repo(rb_repo);
323-
Data_Get_Struct(rb_repo, git_repository, repo);
325+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
324326

325327
error = rugged_branch_lookup(&old_branch, repo, rb_name_or_branch);
326328
rugged_exception_check(error);
@@ -353,7 +355,7 @@ static VALUE rb_git_branch_collection_exist_p(VALUE self, VALUE rb_name)
353355
int error;
354356

355357
Check_Type(rb_name, T_STRING);
356-
Data_Get_Struct(rb_repo, git_repository, repo);
358+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
357359

358360
error = rugged_branch_lookup(&branch, repo, rb_name);
359361
git_reference_free(branch);

ext/rugged/rugged_commit.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern VALUE rb_cRuggedSignature;
1616
VALUE rb_cRuggedCommit;
1717

1818
extern const rb_data_type_t rugged_object_type;
19+
extern const rb_data_type_t rugged_repository_type;
1920

2021
/*
2122
* call-seq:
@@ -357,7 +358,7 @@ static VALUE rb_git_commit_amend(VALUE self, VALUE rb_data)
357358
TypedData_Get_Struct(self, git_commit, &rugged_object_type, commit_to_amend);
358359

359360
owner = rugged_owner(self);
360-
Data_Get_Struct(owner, git_repository, repo);
361+
TypedData_Get_Struct(owner, git_repository, &rugged_repository_type, repo);
361362

362363
rb_ref = rb_hash_aref(rb_data, CSTR2SYM("update_ref"));
363364
if (!NIL_P(rb_ref)) {
@@ -549,7 +550,7 @@ static VALUE rb_git_commit_create(VALUE self, VALUE rb_repo, VALUE rb_data)
549550
Check_Type(rb_data, T_HASH);
550551

551552
rugged_check_repo(rb_repo);
552-
Data_Get_Struct(rb_repo, git_repository, repo);
553+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
553554

554555
if ((error = parse_commit_options(&commit_data, repo, rb_data)) < 0)
555556
goto cleanup;
@@ -614,7 +615,7 @@ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)
614615
rb_scan_args(argc, argv, ":", &rb_options);
615616

616617
rugged_check_repo(rb_repo);
617-
Data_Get_Struct(rb_repo, git_repository, repo);
618+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
618619

619620
TypedData_Get_Struct(self, git_commit, &rugged_object_type, commit);
620621

@@ -735,7 +736,7 @@ static VALUE rb_git_commit_extract_signature(int argc, VALUE *argv, VALUE self)
735736
rb_scan_args(argc, argv, "21", &rb_repo, &rb_commit, &rb_field);
736737

737738
rugged_check_repo(rb_repo);
738-
Data_Get_Struct(rb_repo, git_repository, repo);
739+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
739740

740741
error = git_oid_fromstr(&commit_id, StringValueCStr(rb_commit));
741742
rugged_exception_check(error);
@@ -799,7 +800,7 @@ static VALUE rb_git_commit_create_to_s(VALUE self, VALUE rb_repo, VALUE rb_data)
799800
Check_Type(rb_data, T_HASH);
800801

801802
rugged_check_repo(rb_repo);
802-
Data_Get_Struct(rb_repo, git_repository, repo);
803+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
803804

804805
if ((error = parse_commit_options(&commit_data, repo, rb_data)) < 0)
805806
goto cleanup;
@@ -850,7 +851,7 @@ static VALUE rb_git_commit_create_with_signature(int argc, VALUE *argv, VALUE se
850851
rb_scan_args(argc, argv, "31", &rb_repo, &rb_content, &rb_signature, &rb_field);
851852

852853
rugged_check_repo(rb_repo);
853-
Data_Get_Struct(rb_repo, git_repository, repo);
854+
TypedData_Get_Struct(rb_repo, git_repository, &rugged_repository_type, repo);
854855

855856
Check_Type(rb_content, T_STRING);
856857
Check_Type(rb_signature, T_STRING);

0 commit comments

Comments
 (0)