File: rugged_object.c

package info (click to toggle)
ruby-rugged 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,752 kB
  • sloc: ansic: 8,722; ruby: 7,473; sh: 99; makefile: 5
file content (413 lines) | stat: -rw-r--r-- 10,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (C) the Rugged contributors.  All rights reserved.
 *
 * This file is part of Rugged, distributed under the MIT license.
 * For full terms see the included LICENSE file.
 */

#include "rugged.h"

extern VALUE rb_mRugged;
extern VALUE rb_cRuggedTagAnnotation;
extern VALUE rb_cRuggedTree;
extern VALUE rb_cRuggedCommit;
extern VALUE rb_cRuggedBlob;
extern VALUE rb_cRuggedRepo;

VALUE rb_cRuggedObject;

const rb_data_type_t rugged_object_type;

git_otype rugged_otype_get(VALUE self)
{
	git_otype type = GIT_OBJ_BAD;

	if (NIL_P(self))
		return GIT_OBJ_ANY;

	switch (TYPE(self)) {
	case T_STRING:
		type = git_object_string2type(StringValueCStr(self));
		break;

	case T_FIXNUM:
		type = FIX2INT(self);
		break;

	case T_SYMBOL: {
		ID t = SYM2ID(self);

		if (t == rb_intern("commit"))
			type = GIT_OBJ_COMMIT;
		else if (t == rb_intern("tree"))
			type = GIT_OBJ_TREE;
		else if (t == rb_intern("tag"))
			type = GIT_OBJ_TAG;
		else if (t == rb_intern("blob"))
			type = GIT_OBJ_BLOB;
	   }
	}

	if (!git_object_typeisloose(type))
		rb_raise(rb_eTypeError, "Invalid Git object type specifier");

	return type;
}

VALUE rugged_otype_new(git_otype t)
{
	switch (t) {
		case GIT_OBJ_COMMIT:
			return CSTR2SYM("commit");
		case GIT_OBJ_TAG:
			return CSTR2SYM("tag");
		case GIT_OBJ_TREE:
			return CSTR2SYM("tree");
		case GIT_OBJ_BLOB:
			return CSTR2SYM("blob");
		default:
			return Qnil;
	}
}

int rugged_oid_get(git_oid *oid, git_repository *repo, VALUE p)
{
	git_object *object;
	int error;

	if (rb_obj_is_kind_of(p, rb_cRuggedObject)) {
		TypedData_Get_Struct(p, git_object, &rugged_object_type, object);
		git_oid_cpy(oid, git_object_id(object));
	} else {
		Check_Type(p, T_STRING);

		/* Fast path: see if the 40-char string is an OID */
		if (RSTRING_LEN(p) == 40 &&
			git_oid_fromstr(oid, RSTRING_PTR(p)) == 0)
			return GIT_OK;

		if ((error = git_revparse_single(&object, repo, StringValueCStr(p))))
			return error;

		git_oid_cpy(oid, git_object_id(object));
		git_object_free(object);
	}

	return GIT_OK;
}

git_object *rugged_object_get(git_repository *repo, VALUE object_value, git_otype type)
{
	git_object *object = NULL;

	if (rb_obj_is_kind_of(object_value, rb_cRuggedObject)) {
		git_object *owned_obj = NULL;
		TypedData_Get_Struct(object_value, git_object, &rugged_object_type, owned_obj);
		git_object_dup(&object, owned_obj);
	} else {
		int error;

		Check_Type(object_value, T_STRING);

		/* Fast path: if we have a 40-char string, just perform the lookup directly */
		if (RSTRING_LEN(object_value) == 40) {
			git_oid oid;

			/* If it's not an OID, we can still try the revparse */
			if (git_oid_fromstr(&oid, RSTRING_PTR(object_value)) == 0) {
				error = git_object_lookup(&object, repo, &oid, type);
				rugged_exception_check(error);
				return object;
			}
		}

		/* Otherwise, assume the string is a revlist and try to parse it */
		error = git_revparse_single(&object, repo, StringValueCStr(object_value));
		rugged_exception_check(error);
	}

	assert(object);

	if (type != GIT_OBJ_ANY && git_object_type(object) != type)
		rb_raise(rb_eArgError, "Object is not of the required type");

	return object;
}

static void rb_git_object__free(void *data)
{
	git_object *object = (git_object *) data;
	git_object_free(object);
}

static size_t rb_git_object__size(const void *data)
{
	git_object *object = (git_object *) data;
	size_t size = 0;

	/*
	 * We cannot always be accurate cheaply, but we can give some numbers
	 * which are closer than zero by taking an average/guessed size.
	 */
	switch (git_object_type(object)) {
	case GIT_OBJ_BLOB:
	{
		git_blob *blob = (git_blob *) object;
		size = git_blob_rawsize(blob);
		break;
	}
	case GIT_OBJ_TREE:
	{
		git_tree *tree = (git_tree *) object;
		size = git_tree_entrycount(tree) * 64;
		break;
	}
	case GIT_OBJ_COMMIT:
	case GIT_OBJ_TAG:
		size = 256;
		break;
	default:
		break;
	}

	return size;
}

const rb_data_type_t rugged_object_type = {
	.wrap_struct_name = "Rugged::Object",
	.function = {
		.dmark = NULL,
		.dfree = rb_git_object__free,
		.dsize = rb_git_object__size,
	},
	.data = NULL,
	.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

VALUE rugged_object_new(VALUE owner, git_object *object)
{
	VALUE klass, rb_object;

	switch (git_object_type(object))
	{
		case GIT_OBJ_COMMIT:
			klass = rb_cRuggedCommit;
			break;

		case GIT_OBJ_TAG:
			klass = rb_cRuggedTagAnnotation;
			break;

		case GIT_OBJ_TREE:
			klass = rb_cRuggedTree;
			break;

		case GIT_OBJ_BLOB:
			klass = rb_cRuggedBlob;
			break;

		default:
			rb_raise(rb_eTypeError, "Invalid type for Rugged::Object");
			return Qnil; /* never reached */
	}

	rb_object = TypedData_Wrap_Struct(klass, &rugged_object_type, object);
	rugged_set_owner(rb_object, owner);
	return rb_object;
}


static git_otype class2otype(VALUE klass)
{
	if (RTEST(rb_class_inherited_p(klass, rb_cRuggedCommit)))
        return GIT_OBJ_COMMIT;

	if (RTEST(rb_class_inherited_p(klass, rb_cRuggedTagAnnotation)))
		return GIT_OBJ_TAG;

	if (RTEST(rb_class_inherited_p(klass, rb_cRuggedBlob)))
		return GIT_OBJ_BLOB;

	if (RTEST(rb_class_inherited_p(klass, rb_cRuggedTree)))
		return GIT_OBJ_TREE;

	return GIT_OBJ_BAD;
}

/*
 *  call-seq:
 *    Object.new(repo, oid) -> object
 *    Object.lookup(repo, oid) -> object
 *
 *  Find and return the git object inside +repo+ with the given +oid+.
 *
 *  +oid+ can either have be the complete, 40 character string or any
 *  unique prefix.
 */
VALUE rb_git_object_lookup(VALUE klass, VALUE rb_repo, VALUE rb_hex)
{
	git_object *object;
	git_otype type;
	git_oid oid;
	int error;
	int oid_length;

	git_repository *repo;

	type = class2otype(klass);

	if (type == GIT_OBJ_BAD)
		type = GIT_OBJ_ANY;

	Check_Type(rb_hex, T_STRING);
	oid_length = (int)RSTRING_LEN(rb_hex);

	rugged_check_repo(rb_repo);

	if (oid_length > GIT_OID_HEXSZ)
		rb_raise(rb_eTypeError, "The given OID is too long");

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_oid_fromstrn(&oid, RSTRING_PTR(rb_hex), oid_length);
	rugged_exception_check(error);

	if (oid_length < GIT_OID_HEXSZ)
		error = git_object_lookup_prefix(&object, repo, &oid, oid_length, type);
	else
		error = git_object_lookup(&object, repo, &oid, type);

	rugged_exception_check(error);

	return rugged_object_new(rb_repo, object);
}

VALUE rugged_object_rev_parse(VALUE rb_repo, VALUE rb_spec, int as_obj)
{
	git_object *object;
	const char *spec;
	int error;
	git_repository *repo;
	VALUE ret;

	Check_Type(rb_spec, T_STRING);
	spec = RSTRING_PTR(rb_spec);

	rugged_check_repo(rb_repo);

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_revparse_single(&object, repo, spec);
	rugged_exception_check(error);

	if (as_obj) {
		return rugged_object_new(rb_repo, object);
	}

	ret = rugged_create_oid(git_object_id(object));
	git_object_free(object);
	return ret;
}

/*
 *  call-seq: Object.rev_parse(repo, str) -> object
 *
 *  Find and return a single object inside +repo+ as specified by the
 *  git revision string +str+.
 *
 *  See http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions or
 *  <code>man gitrevisions</code> for information on the accepted syntax.
 *
 *  Raises a Rugged::InvalidError if +str+ does not contain a valid revision string.
 */
VALUE rb_git_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec)
{
	return rugged_object_rev_parse(rb_repo, rb_spec, 1);
}

/*
 *  call-seq: Object.rev_parse_oid(repo, str) -> oid
 *
 *  Find and return the id of the object inside +repo+ as specified by the
 *  git revision string +str+.
 *
 *  See http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions or
 *  <code>man gitrevisions</code> for information on the accepted syntax.
 *
 *  Raises a Rugged::InvalidError if +str+ does not contain a valid revision string.
 */
VALUE rb_git_object_rev_parse_oid(VALUE klass, VALUE rb_repo, VALUE rb_spec)
{
	return rugged_object_rev_parse(rb_repo, rb_spec, 0);
}

/*
 *  call-seq: object == other
 *
 *  Returns true only if +object+ and other are both instances or subclasses of
 *  Rugged::Object and have the same object id, false otherwise.
 */
static VALUE rb_git_object_equal(VALUE self, VALUE other)
{
	git_object *a, *b;

	if (!rb_obj_is_kind_of(other, rb_cRuggedObject))
		return Qfalse;

	TypedData_Get_Struct(self, git_object, &rugged_object_type, a);
	TypedData_Get_Struct(other, git_object, &rugged_object_type, b);

	return git_oid_cmp(git_object_id(a), git_object_id(b)) == 0 ? Qtrue : Qfalse;
}

/*
 *  call-seq: object.oid -> oid
 *
 *  Return the Object ID (a 40 character SHA1 hash) for +object+.
 */
static VALUE rb_git_object_oid_GET(VALUE self)
{
	git_object *object;
	TypedData_Get_Struct(self, git_object, &rugged_object_type, object);
	return rugged_create_oid(git_object_id(object));
}

/*
 *  call-seq: object.type -> type
 *
 *  Returns the object's type. Can be one of +:commit+, +:tag+, +:tree+ or +:blob+.
 */
static VALUE rb_git_object_type_GET(VALUE self)
{
	git_object *object;
	TypedData_Get_Struct(self, git_object, &rugged_object_type, object);

	return rugged_otype_new(git_object_type(object));
}

/*
 *  call-seq: object.read_raw -> raw_object
 *
 *  Returns the git object as a Rugged::OdbObject instance.
 */
static VALUE rb_git_object_read_raw(VALUE self)
{
	git_object *object;
	TypedData_Get_Struct(self, git_object, &rugged_object_type, object);

	return rugged_raw_read(git_object_owner(object), git_object_id(object));
}

void Init_rugged_object(void)
{
	rb_cRuggedObject = rb_define_class_under(rb_mRugged, "Object", rb_cObject);
	rb_define_singleton_method(rb_cRuggedObject, "lookup", rb_git_object_lookup, 2);
	rb_define_singleton_method(rb_cRuggedObject, "rev_parse", rb_git_object_rev_parse, 2);
	rb_define_singleton_method(rb_cRuggedObject, "rev_parse_oid", rb_git_object_rev_parse_oid, 2);
	rb_define_singleton_method(rb_cRuggedObject, "new", rb_git_object_lookup, 2);

	rb_define_method(rb_cRuggedObject, "read_raw", rb_git_object_read_raw, 0);
	rb_define_method(rb_cRuggedObject, "==", rb_git_object_equal, 1);
	rb_define_method(rb_cRuggedObject, "oid", rb_git_object_oid_GET, 0);
	rb_define_method(rb_cRuggedObject, "type", rb_git_object_type_GET, 0);
}