Class: Rugged::Remote

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(repository, name, url) ⇒ Object

Add a new remote with name and url to repository

  • url: a valid remote url

  • name: a valid remote name

Returns a new Rugged::Remote object

Rugged::Remote.add(@repo, 'origin', 'git://github.com/libgit2/rugged.git') #=> #<Rugged::Remote:0x00000001fbfa80>


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/rugged/rugged_remote.c', line 97

static VALUE rb_git_remote_add(VALUE klass, VALUE rb_repo,VALUE rb_name, VALUE rb_url)
{
	git_remote *remote;
	git_repository *repo;
	int error;

	Check_Type(rb_name, T_STRING);
	rugged_validate_remote_url(rb_url);
	rugged_check_repo(rb_repo);

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_remote_create(
			&remote,
			repo,
			StringValueCStr(rb_name),
			StringValueCStr(rb_url));

	rugged_exception_check(error);

	return rugged_remote_new(klass, rb_repo, remote);
}

.each(rb_repo) ⇒ Object

:nodoc:



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'ext/rugged/rugged_remote.c', line 610

static VALUE rb_git_remote_each(VALUE klass, VALUE rb_repo)
{
	git_repository *repo;
	git_strarray remotes;
	size_t i;
	int error = 0;
	int exception = 0;

	if (!rb_block_given_p())
		return rb_funcall(klass, rb_intern("to_enum"), 2, CSTR2SYM("each"), rb_repo);

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

	error = git_remote_list(&remotes, repo);
	rugged_exception_check(error);

	for (i = 0; !exception && !error && i < remotes.count; ++i) {
		git_remote *remote;
		error = git_remote_load(&remote, repo, remotes.strings[i]);

		if (!error) {
			rb_protect(
				rb_yield, rugged_remote_new(klass, rb_repo, remote),
				&exception);
		}
	}

	git_strarray_free(&remotes);

	if (exception)
		rb_jump_tag(exception);

	rugged_exception_check(error);
	return Qnil;
}

.lookup(repository, name) ⇒ nil

Return an existing remote with name in repository:

  • name: a valid remote name

Returns a new Rugged::Remote object or nil if the remote doesn’t exist

Rugged::Remote.lookup(@repo, 'origin') #=> #<Rugged::Remote:0x00000001fbfa80>

Returns:

  • (nil)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/rugged/rugged_remote.c', line 132

static VALUE rb_git_remote_lookup(VALUE klass, VALUE rb_repo, VALUE rb_name)
{
	git_remote *remote;
	git_repository *repo;
	int error;

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

	error = git_remote_load(&remote, repo, StringValueCStr(rb_name));

	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);

	return rugged_remote_new(klass, rb_repo, remote);
}

.names(repository) ⇒ Array

Returns the names of all remotes in repository

Rugged::Remote.names(@repo) #=> ['origin', 'upstream']

Returns:

  • (Array)


590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'ext/rugged/rugged_remote.c', line 590

static VALUE rb_git_remote_names(VALUE klass, VALUE rb_repo)
{
	git_repository *repo;
	git_strarray remotes;
	VALUE rb_remote_names;
	int error;

	rugged_check_repo(rb_repo);

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_remote_list(&remotes, repo);
	rugged_exception_check(error);

	rb_remote_names = rugged_strarray_to_rb_ary(&remotes);
	git_strarray_free(&remotes);
	return rb_remote_names;
}

.new(repository, url) ⇒ Object

Return a new remote with url in repository , the remote is not persisted:

  • url: a valid remote url

Returns a new Rugged::Remote object

Rugged::Remote.new(@repo, 'git://github.com/libgit2/libgit2.git') #=> #<Rugged::Remote:0x00000001fbfa80>


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/rugged/rugged_remote.c', line 63

static VALUE rb_git_remote_new(VALUE klass, VALUE rb_repo, VALUE rb_url)
{
	git_remote *remote;
	git_repository *repo;
	int error;

	rugged_check_repo(rb_repo);
	rugged_validate_remote_url(rb_url);

	Data_Get_Struct(rb_repo, git_repository, repo);

	error = git_remote_create_inmemory(
			&remote,
			repo,
			NULL,
			StringValueCStr(rb_url));

	rugged_exception_check(error);

	return rugged_remote_new(klass, rb_repo, remote);
}

Instance Method Details

#add_fetch(refspec) ⇒ nil

Add a fetch refspec to the remote

Returns:

  • (nil)


443
444
445
446
# File 'ext/rugged/rugged_remote.c', line 443

static VALUE rb_git_remote_add_fetch(VALUE self, VALUE rb_refspec)
{
	return rb_git_remote_add_refspec(self, rb_refspec, GIT_DIRECTION_FETCH);
}

#add_push(refspec) ⇒ nil

Add a push refspec to the remote

Returns:

  • (nil)


454
455
456
457
# File 'ext/rugged/rugged_remote.c', line 454

static VALUE rb_git_remote_add_push(VALUE self, VALUE rb_refspec)
{
	return rb_git_remote_add_refspec(self, rb_refspec, GIT_DIRECTION_PUSH);
}

#clear_refspecsnil

Remove all configured fetch and push refspecs from the remote.

Returns:

  • (nil)


465
466
467
468
469
470
471
472
473
474
# File 'ext/rugged/rugged_remote.c', line 465

static VALUE rb_git_remote_clear_refspecs(VALUE self)
{
	git_remote *remote;

	Data_Get_Struct(self, git_remote, remote);

	git_remote_clear_refspecs(remote);

	return Qnil;
}

#connect(direction) ⇒ nil #connect(direction) {|connected_remote| ... } ⇒ Object

Open a connection to a remote:

  • direction: :fetch or :pull

If a block is given it will be passed the connected remote as argument and the remote will be disconnected when the block terminates.

The transport is selected based on the URL.

remote.connect(:fetch) #=> nil

remote.connect(:fetch) do |r|
  r.connected?  #=> true
end

Overloads:

  • #connect(direction) ⇒ nil

    Returns:

    • (nil)
  • #connect(direction) {|connected_remote| ... } ⇒ Object

    Yields:

    • (connected_remote)


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

static VALUE rb_git_remote_connect(VALUE self, VALUE rb_direction)
{
	int error, direction = 0, exception = 0;
	git_remote *remote;
	ID id_direction;

	Data_Get_Struct(self, git_remote, remote);

	Check_Type(rb_direction, T_SYMBOL);
	id_direction = SYM2ID(rb_direction);

	if (id_direction == rb_intern("fetch"))
		direction = GIT_DIRECTION_FETCH;
	else if (id_direction == rb_intern("push"))
		direction = GIT_DIRECTION_PUSH;
	else
		rb_raise(rb_eTypeError,
			"Invalid remote direction. Expected `:fetch` or `:push`");

	error = git_remote_connect(remote, direction);
	rugged_exception_check(error);

	if (rb_block_given_p()) {
		rb_protect(rb_yield, self, &exception);
		git_remote_disconnect(remote);

		if (exception)
			rb_jump_tag(exception);
	}

	return Qnil;
}

#connected?Boolean

Returns if the remote is connected

remote.connected? #=> false
remote.connect(:fetch)
remote.connected? #=> true

Returns:

  • (Boolean)


486
487
488
489
490
491
492
# File 'ext/rugged/rugged_remote.c', line 486

static VALUE rb_git_remote_connected(VALUE self)
{
	git_remote *remote;
	Data_Get_Struct(self, git_remote, remote);

	return git_remote_connected(remote) ? Qtrue : Qfalse;
}

#disconnectnil

Disconnect from the remote, closes the connection to the remote

Returns:

  • (nil)


158
159
160
161
162
163
164
165
# File 'ext/rugged/rugged_remote.c', line 158

static VALUE rb_git_remote_disconnect(VALUE self)
{
	git_remote *remote;
	Data_Get_Struct(self, git_remote, remote);

	git_remote_disconnect(remote);
	return Qnil;
}

#downloadnil

Download the packfile from a connected remote

Negotiate what objects should be downloaded and download the packfile with those objects.

remote.connect(:fetch) do |r|
  r.download
end

Returns:

  • (nil)


507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/rugged/rugged_remote.c', line 507

static VALUE rb_git_remote_download(VALUE self)
{
	int error;
	git_remote *remote;

	Data_Get_Struct(self, git_remote, remote);

	error = git_remote_download(remote, NULL, NULL);
	rugged_exception_check(error);

	return Qnil;
}

#fetch_refspecsObject

remote.fetch_refspecs -> array

Get the remote’s list of fetch refspecs as array



402
403
404
405
# File 'ext/rugged/rugged_remote.c', line 402

static VALUE rb_git_remote_fetch_refspecs(VALUE self)
{
	return rb_git_remote_refspecs(self, GIT_DIRECTION_FETCH);
}

#lsObject #ls {|remote_head_hash| ... } ⇒ Object

List references available in a connected remote repository along with the associated commit IDs.

Call the given block once for each remote head in the remote as a Hash. If no block is given an Enumerator is returned.

remote.connect(:fetch) do |r|
  r.ls.to_a #=> [{:local?=>false, :oid=>"b3ee97a91b02e91c35394950bda6ea622044baad", :loid=> nil, :name=>"refs/heads/development"}]
end

remote head hash includes:

:oid

oid of the remote head

:name

name of the remote head

Overloads:

  • #ls {|remote_head_hash| ... } ⇒ Object

    Yields:

    • (remote_head_hash)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'ext/rugged/rugged_remote.c', line 262

static VALUE rb_git_remote_ls(VALUE self)
{
	int error, exception = 0;
	git_remote *remote;
	Data_Get_Struct(self, git_remote, remote);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("ls"));

	error = git_remote_ls(remote, &cb_remote__ls, &exception);

	if (exception)
		rb_jump_tag(exception);
	rugged_exception_check(error);

	return Qnil;
}

#nameString

Returns the remote’s name

remote.name #=> "origin"

Returns:

  • (String)


287
288
289
290
291
292
293
294
295
296
# File 'ext/rugged/rugged_remote.c', line 287

static VALUE rb_git_remote_name(VALUE self)
{
	git_remote *remote;
	const char * name;
	Data_Get_Struct(self, git_remote, remote);

	name = git_remote_name(remote);

	return name ? rb_str_new_utf8(name) : Qnil;
}

#push(refspecs) ⇒ Object

Push a list of refspecs to the given remote.

refspecs - A list of refspecs that should be pushed to the remote.

Returns a hash containing the pushed refspecs as keys and any error messages or nil as values.



9
10
11
# File 'lib/rugged/remote.rb', line 9

def push(refspecs)
  @owner.push(self, refspecs)
end

#push_refspecsObject

remote.push_refspecs -> array

Get the remote’s list of push refspecs as array



413
414
415
416
# File 'ext/rugged/rugged_remote.c', line 413

static VALUE rb_git_remote_push_refspecs(VALUE self)
{
	return rb_git_remote_refspecs(self, GIT_DIRECTION_PUSH);
}

#push_urlString?

Returns the remote’s url for pushing or nil if no special url for pushing is set.

remote.push_url #=> "git://github.com/libgit2/rugged.git"

Returns:

  • (String, nil)


342
343
344
345
346
347
348
349
350
351
# File 'ext/rugged/rugged_remote.c', line 342

static VALUE rb_git_remote_push_url(VALUE self)
{
	git_remote *remote;
	const char * push_url;

	Data_Get_Struct(self, git_remote, remote);

	push_url = git_remote_pushurl(remote);
	return push_url ? rb_str_new_utf8(push_url) : Qnil;
}

#push_url=(url) ⇒ Object

Sets the remote’s url for pushing without persisting it in the config. Existing connections will not be updated.

remote.push_url = '[email protected]/libgit2/rugged.git' #=> "[email protected]/libgit2/rugged.git"


361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/rugged/rugged_remote.c', line 361

static VALUE rb_git_remote_set_push_url(VALUE self, VALUE rb_url)
{
	git_remote *remote;

	rugged_validate_remote_url(rb_url);
	Data_Get_Struct(self, git_remote, remote);

	rugged_exception_check(
		git_remote_set_pushurl(remote, StringValueCStr(rb_url))
	);

	return rb_url;
}

#rename!(new_name) ⇒ Array?

Renames a remote

All remote-tracking branches and configuration settings for the remote are updated.

Returns nil if everything was updated or array of fetch refspecs that haven’t been automatically updated and need potential manual tweaking.

A temporary in-memory remote, created with Remote.new cannot be given a name with this method.

remote = Rugged::Remote.lookup(@repo, 'origin')
remote.rename!('upstream') #=> nil

Returns:

  • (Array, nil)


693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'ext/rugged/rugged_remote.c', line 693

static VALUE rb_git_remote_rename(VALUE self, VALUE rb_new_name)
{
	git_remote *remote;
	int error = 0;
	VALUE rb_refspec_ary = rb_ary_new();

	Check_Type(rb_new_name, T_STRING);
	Data_Get_Struct(self, git_remote, remote);
	error = git_remote_rename(
			remote,
			StringValueCStr(rb_new_name),
			cb_remote__rename_problem, (void *)rb_refspec_ary);

	rugged_exception_check(error);

	return RARRAY_LEN(rb_refspec_ary) == 0 ? Qnil : rb_refspec_ary;
}

#savetrue

Saves the remote data ( url, fetchspecs, …) to the config

One can’t save a in-memory remote created with Remote.new. Doing so will result in an exception being raised.

Returns:

  • (true)


656
657
658
659
660
661
662
663
664
665
666
# File 'ext/rugged/rugged_remote.c', line 656

static VALUE rb_git_remote_save(VALUE self)
{
	git_remote *remote;

	Data_Get_Struct(self, git_remote, remote);

	rugged_exception_check(
		git_remote_save(remote)
	);
	return Qtrue;
}

#update_tips!nil #update_tips! {|reference, source, destination| ... } ⇒ Object

Update the tips to a new state from a connected remote. The target objects must be downloaded before the tips are updated.

r.update_tips! do |ref, src, dst|
  puts "#{ref}: #{src}..#{dst}"
end

Overloads:

  • #update_tips!nil

    Returns:

    • (nil)
  • #update_tips! {|reference, source, destination| ... } ⇒ Object

    Yields:

    • (reference, source, destination)


545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'ext/rugged/rugged_remote.c', line 545

static VALUE rb_git_remote_update_tips(VALUE self)
{
	git_remote *remote;

	Data_Get_Struct(self, git_remote, remote);

	if (rb_block_given_p()) {
		int exception = 0, error;
		git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;

		callbacks.payload = &exception;
		callbacks.update_tips = &cb_remote__update_tips;
		rugged_exception_check(
			git_remote_set_callbacks(remote, &callbacks)
		);

		error = git_remote_update_tips(remote);

		callbacks.update_tips = NULL;
		// Don't overwrite the first error we've seen
		if (!error) error = git_remote_set_callbacks(remote, &callbacks);
		else git_remote_set_callbacks(remote, &callbacks);

		if (exception)
			rb_jump_tag(exception);

		rugged_exception_check(error);
	} else {
		rugged_exception_check(
			git_remote_update_tips(remote)
		);
	}

	return Qnil;
}

#urlString

Returns the remote’s url

remote.url #=> "git://github.com/libgit2/rugged.git"

Returns:

  • (String)


305
306
307
308
309
310
311
# File 'ext/rugged/rugged_remote.c', line 305

static VALUE rb_git_remote_url(VALUE self)
{
	git_remote *remote;
	Data_Get_Struct(self, git_remote, remote);

	return rb_str_new_utf8(git_remote_url(remote));
}

#url=(url) ⇒ Object

Sets the remote’s url without persisting it in the config. Existing connections will not be updated.

remote.url = 'git://github.com/libgit2/rugged.git' #=> "git://github.com/libgit2/rugged.git"


321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rugged/rugged_remote.c', line 321

static VALUE rb_git_remote_set_url(VALUE self, VALUE rb_url)
{
	git_remote *remote;

	rugged_validate_remote_url(rb_url);
	Data_Get_Struct(self, git_remote, remote);

	rugged_exception_check(
		git_remote_set_url(remote, StringValueCStr(rb_url))
	);
	return rb_url;
}