Class: Rugged::ReferenceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/rugged/rugged_reference_collection.c

Instance Method Summary collapse

Constructor Details

#new(repo) ⇒ Object

Creates and returns a new collection of references for the given repo.



39
40
41
42
43
# File 'ext/rugged/rugged_reference_collection.c', line 39

static VALUE rb_git_reference_collection_initialize(VALUE self, VALUE repo)
{
	rugged_set_owner(self, repo);
	return self;
}

Instance Method Details

#[](name) ⇒ Object

Lookup a reference in the collection with the given name.

Returns a new Rugged::Reference object.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/rugged/rugged_reference_collection.c', line 127

static VALUE rb_git_reference_collection_aref(VALUE self, VALUE rb_name) {
	VALUE rb_repo = rugged_owner(self);
	git_repository *repo;
	git_reference *ref;
	int error;

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name));

	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, rb_repo, ref);
}

#create(name, oid, options = {}) ⇒ Object #create(name, target, options = {}) ⇒ Object

Create a symbolic or direct reference on the collection’s repository with the given name.

If the second argument is a valid OID, the reference will be created as direct. Otherwise, it will be assumed the target is the name of another reference.

The following options can be passed in the options Hash:

:force

Overwrites the reference with the given name, if it already exists, instead of raising an exception.

:message

A single line log message to be appended to the reflog.

:signature

The signature to be used for populating the reflog entry.

If a reference with the given name already exists and :force is not true, an exception will be raised.

The :message and :signature options are ignored if the reference does not belong to the standard set (HEAD, refs/heads/*, refs/remotes/* or refs/notes/*) and it does not have a reflog.



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

static VALUE rb_git_reference_collection_create(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_repo = rugged_owner(self), rb_name, rb_target, rb_options;
	git_repository *repo;
	git_reference *ref;
	git_oid oid;
	git_signature *signature = NULL;
	char *log_message = NULL;
	int error, force = 0;

	rb_scan_args(argc, argv, "20:", &rb_name, &rb_target, &rb_options);

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);
	Check_Type(rb_name, T_STRING);
	Check_Type(rb_target, T_STRING);

	if (!NIL_P(rb_options)) {
		VALUE rb_val;

		force = RTEST(rb_hash_aref(rb_options, CSTR2SYM("force")));

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("signature"));
		if (!NIL_P(rb_val))
			signature = rugged_signature_get(rb_val, repo);

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
		if (!NIL_P(rb_val))
			log_message = StringValueCStr(rb_val);
	}

	if (git_oid_fromstr(&oid, StringValueCStr(rb_target)) == GIT_OK) {
		error = git_reference_create(
			&ref, repo, StringValueCStr(rb_name), &oid, force, signature, log_message);
	} else {
		error = git_reference_symbolic_create(
			&ref, repo, StringValueCStr(rb_name), StringValueCStr(rb_target), force, signature, log_message);
	}

	git_signature_free(signature);
	rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, rb_repo, ref);
}

#delete(ref) ⇒ nil #delete(name) ⇒ nil

Delete specified reference.

If a Rugged::Reference object was passed, the object will become invalidated and won’t be able to be used for any other operations.

repo.references.delete("HEAD")
# Reference no longer exists on disk

Overloads:

  • #delete(ref) ⇒ nil

    Returns:

    • (nil)
  • #delete(name) ⇒ nil

    Returns:

    • (nil)


444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'ext/rugged/rugged_reference_collection.c', line 444

static VALUE rb_git_reference_collection_delete(VALUE self, VALUE rb_name_or_ref)
{
	VALUE rb_repo = rugged_owner(self);
	git_reference *ref;
	git_repository *repo;
	int error;

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
	rugged_exception_check(error);

	error = git_reference_delete(ref);
	git_reference_free(ref);
	rugged_exception_check(error);

	return Qnil;
}

#each(glob = nil) {|ref| ... } ⇒ nil #each(glob = nil) ⇒ Object

Iterate through all the references in the collection’s repository. Iteration can be optionally filtered to the ones matching the given glob, a standard Unix filename glob.

The given block will be called once with a Rugged::Reference instance for each reference.

If no block is given, an enumerator will be returned.

Overloads:

  • #each(glob = nil) {|ref| ... } ⇒ nil

    Yields:

    • (ref)

    Returns:

    • (nil)


211
212
213
214
# File 'ext/rugged/rugged_reference_collection.c', line 211

static VALUE rb_git_reference_collection_each(int argc, VALUE *argv, VALUE self)
{
	return rb_git_reference_collection__each(argc, argv, self, 0);
}

#each_name(glob = nil) {|ref_name| ... } ⇒ nil #each_name(glob = nil) ⇒ Object

Iterate through all the reference names in the collection’s repository. Iteration can be optionally filtered to the ones matching the given glob, a standard Unix filename glob.

The given block will be called once with the name of each reference.

If no block is given, an enumerator will be returned.

Overloads:

  • #each_name(glob = nil) {|ref_name| ... } ⇒ nil

    Yields:

    • (ref_name)

    Returns:

    • (nil)


229
230
231
232
# File 'ext/rugged/rugged_reference_collection.c', line 229

static VALUE rb_git_reference_collection_each_name(int argc, VALUE *argv, VALUE self)
{
	return rb_git_reference_collection__each(argc, argv, self, 1);
}

#exist?(name) ⇒ Boolean #exists?(name) ⇒ Boolean

Check if a given reference exists with the given name.

Overloads:

  • #exist?(name) ⇒ Boolean

    Returns:

    • (Boolean)
  • #exists?(name) ⇒ Boolean

    Returns:

    • (Boolean)


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

static VALUE rb_git_reference_collection_exist_p(VALUE self, VALUE rb_name_or_ref)
{
	VALUE rb_repo = rugged_owner(self);
	git_repository *repo;
	git_reference *ref;
	int error;

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
	git_reference_free(ref);

	if (error == GIT_ENOTFOUND)
		return Qfalse;
	else
		rugged_exception_check(error);

	return Qtrue;
}

#exist?(name) ⇒ Boolean #exists?(name) ⇒ Boolean

Check if a given reference exists with the given name.

Overloads:

  • #exist?(name) ⇒ Boolean

    Returns:

    • (Boolean)
  • #exists?(name) ⇒ Boolean

    Returns:

    • (Boolean)


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

static VALUE rb_git_reference_collection_exist_p(VALUE self, VALUE rb_name_or_ref)
{
	VALUE rb_repo = rugged_owner(self);
	git_repository *repo;
	git_reference *ref;
	int error;

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
	git_reference_free(ref);

	if (error == GIT_ENOTFOUND)
		return Qfalse;
	else
		rugged_exception_check(error);

	return Qtrue;
}

#rename(old_name, new_name, options = {}) ⇒ Object #rename(ref, new_name, options = {}) ⇒ Object

Change the name of a reference. If force is true, any previously existing references will be overwritten when renaming.

Return a new reference object with the new object

reference.name #=> 'refs/heads/master'
new_ref = references.rename(ref, 'refs/heads/development') #=> <Reference>
new_ref.name #=> 'refs/heads/development'

The following options can be passed in the options Hash:

:force

Overwrites the reference with the given name, if it already exists, instead of raising an exception.

:message

A single line log message to be appended to the reflog.

:signature

The signature to be used for populating the reflog entry.

If a reference with the given new_name already exists and :force is not true, an exception will be raised.

The :message and :signature options are ignored if the reference does not belong to the standard set (HEAD, refs/heads/*, refs/remotes/* or refs/notes/*) and it does not have a reflog.



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

static VALUE rb_git_reference_collection_rename(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_new_name, rb_name_or_ref, rb_options;
	VALUE rb_repo = rugged_owner(self);
	git_reference *ref, *out = NULL;
	git_repository *repo;
	git_signature *signature = NULL;
	char *log_message = NULL;
	int error, force = 0;

	rb_scan_args(argc, argv, "20:", &rb_name_or_ref, &rb_new_name, &rb_options);
	Check_Type(rb_new_name, T_STRING);

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	if (!NIL_P(rb_options)) {
		VALUE rb_val;

		force = RTEST(rb_hash_aref(rb_options, CSTR2SYM("force")));

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("signature"));
		if (!NIL_P(rb_val))
			signature = rugged_signature_get(rb_val, repo);

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
		if (!NIL_P(rb_val))
			log_message = StringValueCStr(rb_val);
	}

	if ((error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref))) == GIT_OK)
		error = git_reference_rename(&out, ref, StringValueCStr(rb_new_name), force, signature, log_message);

	git_reference_free(ref);
	git_signature_free(signature);

	rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, rugged_owner(self), out);
}

#rename(old_name, new_name, options = {}) ⇒ Object #rename(ref, new_name, options = {}) ⇒ Object

Change the name of a reference. If force is true, any previously existing references will be overwritten when renaming.

Return a new reference object with the new object

reference.name #=> 'refs/heads/master'
new_ref = references.rename(ref, 'refs/heads/development') #=> <Reference>
new_ref.name #=> 'refs/heads/development'

The following options can be passed in the options Hash:

:force

Overwrites the reference with the given name, if it already exists, instead of raising an exception.

:message

A single line log message to be appended to the reflog.

:signature

The signature to be used for populating the reflog entry.

If a reference with the given new_name already exists and :force is not true, an exception will be raised.

The :message and :signature options are ignored if the reference does not belong to the standard set (HEAD, refs/heads/*, refs/remotes/* or refs/notes/*) and it does not have a reflog.



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

static VALUE rb_git_reference_collection_rename(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_new_name, rb_name_or_ref, rb_options;
	VALUE rb_repo = rugged_owner(self);
	git_reference *ref, *out = NULL;
	git_repository *repo;
	git_signature *signature = NULL;
	char *log_message = NULL;
	int error, force = 0;

	rb_scan_args(argc, argv, "20:", &rb_name_or_ref, &rb_new_name, &rb_options);
	Check_Type(rb_new_name, T_STRING);

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	if (!NIL_P(rb_options)) {
		VALUE rb_val;

		force = RTEST(rb_hash_aref(rb_options, CSTR2SYM("force")));

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("signature"));
		if (!NIL_P(rb_val))
			signature = rugged_signature_get(rb_val, repo);

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
		if (!NIL_P(rb_val))
			log_message = StringValueCStr(rb_val);
	}

	if ((error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref))) == GIT_OK)
		error = git_reference_rename(&out, ref, StringValueCStr(rb_new_name), force, signature, log_message);

	git_reference_free(ref);
	git_signature_free(signature);

	rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, rugged_owner(self), out);
}

#update(ref, oid) ⇒ Object #update(name, oid) ⇒ Object #update(ref, other_ref) ⇒ Object #update(name, other_ref_name) ⇒ Object

Set the target of a reference. If ref is a direct reference, the new target must be a String representing a SHA1 OID.

If reference is symbolic, the new target must be a String with the name of another reference.

The original reference is unaltered; a new reference object is returned with the new target, and the changes are persisted to disk.

r1.type #=> :symbolic
references.update(r1, "refs/heads/master") #=> <Reference>

r2.type #=> :direct
references.update(r2, "de5ba987198bcf2518885f0fc1350e5172cded78") #=> <Reference>


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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'ext/rugged/rugged_reference_collection.c', line 370

static VALUE rb_git_reference_collection_update(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_repo = rugged_owner(self), rb_name_or_ref, rb_target, rb_options;
	git_repository *repo = NULL;
	git_signature *signature = NULL;
	git_reference *ref = NULL, *out = NULL;
	char *log_message = NULL;
	int error;

	rb_scan_args(argc, argv, "20:", &rb_name_or_ref, &rb_target, &rb_options);

	if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
		rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);

	if (TYPE(rb_name_or_ref) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	if (rb_obj_is_kind_of(rb_target, rb_cRuggedReference))
		rb_target = rb_funcall(rb_target, rb_intern("canonical_name"), 0);

	if (TYPE(rb_target) != T_STRING)
		rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");

	if (!NIL_P(rb_options)) {
		VALUE rb_val;

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("signature"));
		if (!NIL_P(rb_val))
			signature = rugged_signature_get(rb_val, repo);

		rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
		if (!NIL_P(rb_val))
			log_message = StringValueCStr(rb_val);
	}

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
	rugged_exception_check(error);

	if (git_reference_type(ref) == GIT_REF_OID) {
		git_oid target;

		error = git_oid_fromstr(&target, StringValueCStr(rb_target));
		if (error) goto cleanup;

		error = git_reference_set_target(&out, ref, &target, signature, log_message);
	} else {
		error = git_reference_symbolic_set_target(&out, ref, StringValueCStr(rb_target), signature, log_message);
	}

cleanup:

	git_reference_free(ref);
	git_signature_free(signature);
	rugged_exception_check(error);

	return rugged_ref_new(rb_cRuggedReference, rb_repo, out);
}