Class: Rugged::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/rugged/object.rb,
ext/rugged/rugged_object.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(repo, oid) ⇒ 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.

Overloads:



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
# File 'ext/rugged/rugged_object.c', line 200

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);
}

.new(repo, oid) ⇒ 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.

Overloads:



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
# File 'ext/rugged/rugged_object.c', line 200

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);
}

.rev_parse(repo, str) ⇒ Object

Find and return a single object inside repo as specified by the git revision string str.

See git-scm.com/docs/git-rev-parse.html#_specifying_revisions or man gitrevisions for information on the accepted syntax.

Raises a Rugged::InvalidError if str does not contain a valid revision string.

Returns:



276
277
278
279
# File 'ext/rugged/rugged_object.c', line 276

VALUE rb_git_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec)
{
	return rugged_object_rev_parse(rb_repo, rb_spec, 1);
}

.rev_parse_oid(repo, str) ⇒ Object

Find and return the id of the object inside repo as specified by the git revision string str.

See git-scm.com/docs/git-rev-parse.html#_specifying_revisions or man gitrevisions for information on the accepted syntax.

Raises a Rugged::InvalidError if str does not contain a valid revision string.



292
293
294
295
# File 'ext/rugged/rugged_object.c', line 292

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);
}

Instance Method Details

#<=>(other) ⇒ Object



8
9
10
# File 'lib/rugged/object.rb', line 8

def <=>(other)
  self.oid <=> other.oid
end

#==(other) ⇒ Object

Returns true only if object and other are both instances or subclasses of Rugged::Object and have the same object id, false otherwise.



303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/rugged/rugged_object.c', line 303

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;

	Data_Get_Struct(self, git_object, a);
	Data_Get_Struct(other, git_object, b);

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

#create_note(data = {}) ⇒ Object

Write a new note to object, with the given data arguments, passed as a Hash:

  • :message: the content of the note to add to the object

  • :committer: (optional) a hash with the signature for the committer defaults to the signature from the configuration

  • :author: (optional) a hash with the signature for the author defaults to the signature from the configuration

  • :ref: (optional): canonical name of the reference to use, defaults to “refs/notes/commits”

  • :force: (optional): overwrite existing note (disabled by default)

When the note is successfully written to disk, its oid will be returned as a hex String.

author = {:email=>"[email protected]", :time=>Time.now, :name=>"Vicent Mart\303\255"}

obj.create_note(
  :author    => author,
  :committer => author,
  :message   => "Hello world\n\n",
  :ref       => 'refs/notes/builds'
)


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
# File 'ext/rugged/rugged_note.c', line 110

static VALUE rb_git_note_create(VALUE self, VALUE rb_data)
{
	VALUE rb_ref, rb_message, rb_force;
	git_repository *repo = NULL;
	const char *notes_ref = NULL;

	VALUE owner;

	git_signature *author, *committer;
	git_oid note_oid;

	git_object *target = NULL;
	int error = 0;
	int force = 0;

	Check_Type(rb_data, T_HASH);

	Data_Get_Struct(self, git_object, target);

	owner = rugged_owner(self);
	Data_Get_Struct(owner, git_repository, repo);

	rb_ref = rb_hash_aref(rb_data, CSTR2SYM("ref"));

	rb_force = rb_hash_aref(rb_data, CSTR2SYM("force"));
	if (!NIL_P(rb_force))
		force = rugged_parse_bool(rb_force);

	if (!NIL_P(rb_ref)) {
		Check_Type(rb_ref, T_STRING);
		notes_ref = StringValueCStr(rb_ref);
	}

	rb_message = rb_hash_aref(rb_data, CSTR2SYM("message"));
	Check_Type(rb_message, T_STRING);

	committer = rugged_signature_get(
		rb_hash_aref(rb_data, CSTR2SYM("committer")), repo
	);

	author = rugged_signature_get(
		rb_hash_aref(rb_data, CSTR2SYM("author")), repo
	);

	error = git_note_create(
			&note_oid,
			repo,
			notes_ref,
			author,
			committer,
			git_object_id(target),
			StringValueCStr(rb_message),
			force);


	git_signature_free(author);
	git_signature_free(committer);

	rugged_exception_check(error);

	return rugged_create_oid(&note_oid);
}

#oidObject

Return the Object ID (a 40 character SHA1 hash) for object.



321
322
323
324
325
326
# File 'ext/rugged/rugged_object.c', line 321

static VALUE rb_git_object_oid_GET(VALUE self)
{
	git_object *object;
	Data_Get_Struct(self, git_object, object);
	return rugged_create_oid(git_object_id(object));
}

#read_rawObject

Returns the git object as a Rugged::OdbObject instance.



346
347
348
349
350
351
352
# File 'ext/rugged/rugged_object.c', line 346

static VALUE rb_git_object_read_raw(VALUE self)
{
	git_object *object;
	Data_Get_Struct(self, git_object, object);

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

#remove_note(data = {}) ⇒ Boolean

Removes a note from object, with the given data arguments, passed as a Hash:

  • :committer (optional): a hash with the signature for the committer, defaults to the signature from the configuration

  • :author (optional): a hash with the signature for the author, defaults to the signature from the configuration

  • :ref: (optional): canonical name of the reference to use, defaults to “refs/notes/commits”

When the note is successfully removed true will be returned as a Boolean.

author = {:email=>"[email protected]", :time=>Time.now, :name=>"Vicent Mart\303\255"}

obj.remove_note(
  :author    => author,
  :committer => author,
  :ref       => 'refs/notes/builds'
)

Returns:

  • (Boolean)


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
# File 'ext/rugged/rugged_note.c', line 197

static VALUE rb_git_note_remove(int argc, VALUE *argv, VALUE self)
{
	int error = 0;
	const char *notes_ref = NULL;
	git_repository *repo = NULL;
	git_signature *author, *committer;
	git_object *target = NULL;
	VALUE rb_data;
	VALUE rb_ref = Qnil;
	VALUE rb_author = Qnil;
	VALUE rb_committer = Qnil;
	VALUE owner;

	Data_Get_Struct(self, git_object, target);

	owner = rugged_owner(self);
	Data_Get_Struct(owner, git_repository, repo);

	rb_scan_args(argc, argv, "01", &rb_data);

	if (!NIL_P(rb_data)) {
		Check_Type(rb_data, T_HASH);

		rb_ref = rb_hash_aref(rb_data, CSTR2SYM("ref"));
		if (!NIL_P(rb_ref)) {
			Check_Type(rb_ref, T_STRING);
			notes_ref = StringValueCStr(rb_ref);
		}

		rb_committer = rb_hash_aref(rb_data, CSTR2SYM("committer"));
		rb_author = rb_hash_aref(rb_data, CSTR2SYM("author"));
	}

	committer = rugged_signature_get(rb_committer, repo);
	author = rugged_signature_get(rb_author, repo);

	error = git_note_remove(
			repo,
			notes_ref,
			author,
			committer,
			git_object_id(target));

	git_signature_free(author);
	git_signature_free(committer);

	if (error == GIT_ENOTFOUND)
		return Qfalse;

	rugged_exception_check(error);

	return Qtrue;
}

#typeObject

Returns the object’s type. Can be one of :commit, :tag, :tree or :blob.



333
334
335
336
337
338
339
# File 'ext/rugged/rugged_object.c', line 333

static VALUE rb_git_object_type_GET(VALUE self)
{
	git_object *object;
	Data_Get_Struct(self, git_object, object);

	return rugged_otype_new(git_object_type(object));
}