Class: Rugged::BranchCollection

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

Instance Method Summary collapse

Constructor Details

#new(repo) ⇒ Object

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


83
84
85
86
87
# File 'ext/rugged/rugged_branch_collection.c', line 83

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

Instance Method Details

#[](name) ⇒ Object

Return the branch with the given name.

Branches can be looked up by their relative (development) or absolute (refs/heads/development) branch name.

If a local branch and a remote branch both share the same short name (e.g. refs/heads/origin/master and refs/remotes/origin/master), passing origin/master as the name will return the local branch. You can explicitly request the local branch by passing heads/origin/master, or the remote branch through remotes/origin/master.

Returns the looked up branch, or nil if the branch doesn't exist.


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/rugged/rugged_branch_collection.c', line 195

static VALUE rb_git_branch_collection_aref(VALUE self, VALUE rb_name) {
	git_reference *branch;
	git_repository *repo;

	VALUE rb_repo = rugged_owner(self);
	int error;

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

	Check_Type(rb_name, T_STRING);

	error = rugged_branch_lookup(&branch, repo, rb_name);
	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);
	return rugged_branch_new(rb_repo, branch);
}

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

Create a new branch with the given name, pointing to the target.

name needs to be a branch name, not an absolute reference path (e.g. development instead of refs/heads/development).

target needs to be an existing commit in the given repository.

The following options can be passed in the options Hash:

:force

Overwrites the branch 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 branch with the given name already exists and :force is not true, an exception will be raised.

Returns a Rugged::Branch for the newly created branch.


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

static VALUE rb_git_branch_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 *branch;
	git_commit *target;
	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);
	}

	target = (git_commit *)rugged_object_get(repo, rb_target, GIT_OBJ_COMMIT);

	error = git_branch_create(&branch, repo, StringValueCStr(rb_name), target, force, signature, log_message);

	git_commit_free(target);
	git_signature_free(signature);

	rugged_exception_check(error);

	return rugged_branch_new(rb_repo, branch);
}

#delete(branch) ⇒ nil #delete(name) ⇒ nil

Delete the specified branch.

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


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'ext/rugged/rugged_branch_collection.c', line 305

static VALUE rb_git_branch_collection_delete(VALUE self, VALUE rb_name_or_branch)
{
	git_reference *branch;
	git_repository *repo;

	VALUE rb_repo = rugged_owner(self);
	int error;

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

	error = rugged_branch_lookup(&branch, repo, rb_name_or_branch);
	rugged_exception_check(error);

	error = git_branch_delete(branch);
	git_reference_free(branch);
	rugged_exception_check(error);

	return Qnil;
}

#each([filter]) {|branch| ... } ⇒ Object #each([filter]) ⇒ Object

Iterate through the branches in the collection. Iteration can be optionally filtered to yield only :local or :remote branches.

The given block will be called once with a Rugged::Branch object for each branch in the repository. If no block is given, an enumerator will be returned.

Overloads:

  • #each([filter]) {|branch| ... } ⇒ Object

    Yields:

    • (branch)

274
275
276
277
# File 'ext/rugged/rugged_branch_collection.c', line 274

static VALUE rb_git_branch_collection_each(int argc, VALUE *argv, VALUE self)
{
	return each_branch(argc, argv, self, 0);
}

#each_name([filter]) {|branch_name| ... } ⇒ Object #each_name([filter]) ⇒ Object

Iterate through the names of the branches in the collection. Iteration can be optionally filtered to yield only :local or :remote branches.

The given block will be called once with the name of each branch as a String. If no block is given, an enumerator will be returned.

Overloads:

  • #each_name([filter]) {|branch_name| ... } ⇒ Object

    Yields:

    • (branch_name)

290
291
292
293
# File 'ext/rugged/rugged_branch_collection.c', line 290

static VALUE rb_git_branch_collection_each_name(int argc, VALUE *argv, VALUE self)
{
	return each_branch(argc, argv, self, 1);
}

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

Check if a branch exists with the given name.


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'ext/rugged/rugged_branch_collection.c', line 405

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

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

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

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

	return Qtrue;
}

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

Check if a branch exists with the given name.


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'ext/rugged/rugged_branch_collection.c', line 405

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

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

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

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

	return Qtrue;
}

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

Rename a branch to new_name.

new_name needs to be a branch name, not an absolute reference path (e.g. development instead of refs/heads/development).

The following options can be passed in the options Hash:

:force

Overwrites the branch 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 branch with the given new_name already exists and :force is not true, an exception will be raised.

A new Rugged::Branch object for the renamed branch will be returned.


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

static VALUE rb_git_branch_collection_move(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_repo = rugged_owner(self), rb_name_or_branch, rb_new_branch_name, rb_options;
	git_reference *old_branch = NULL, *new_branch = 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_branch, &rb_new_branch_name, &rb_options);
	Check_Type(rb_new_branch_name, T_STRING);

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);
	
	error = rugged_branch_lookup(&old_branch, repo, rb_name_or_branch);
	rugged_exception_check(error);

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

	error = git_branch_move(&new_branch, old_branch, StringValueCStr(rb_new_branch_name), force, signature, log_message);

	git_signature_free(signature);
	git_reference_free(old_branch);

	rugged_exception_check(error);

	return rugged_branch_new(rugged_owner(self), new_branch);
}

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

Rename a branch to new_name.

new_name needs to be a branch name, not an absolute reference path (e.g. development instead of refs/heads/development).

The following options can be passed in the options Hash:

:force

Overwrites the branch 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 branch with the given new_name already exists and :force is not true, an exception will be raised.

A new Rugged::Branch object for the renamed branch will be returned.


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

static VALUE rb_git_branch_collection_move(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_repo = rugged_owner(self), rb_name_or_branch, rb_new_branch_name, rb_options;
	git_reference *old_branch = NULL, *new_branch = 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_branch, &rb_new_branch_name, &rb_options);
	Check_Type(rb_new_branch_name, T_STRING);

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);
	
	error = rugged_branch_lookup(&old_branch, repo, rb_name_or_branch);
	rugged_exception_check(error);

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

	error = git_branch_move(&new_branch, old_branch, StringValueCStr(rb_new_branch_name), force, signature, log_message);

	git_signature_free(signature);
	git_reference_free(old_branch);

	rugged_exception_check(error);

	return rugged_branch_new(rugged_owner(self), new_branch);
}