Method: Rugged::ReferenceCollection#rename

Defined in:
ext/rugged/rugged_reference_collection.c

#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.

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



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

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;
	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 = rb_hash_aref(rb_options, CSTR2SYM("message"));
		if (!NIL_P(rb_val))
			log_message = StringValueCStr(rb_val);

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

	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, log_message);

	git_reference_free(ref);

	rugged_exception_check(error);

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