Skip to content

Commit

Permalink
Add Rugged::Repository#remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac committed Mar 24, 2012
1 parent 6573997 commit 90cb029
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ext/rugged/rugged_remote.c
Expand Up @@ -198,11 +198,40 @@ static VALUE rb_git_remote_update_tips(VALUE self)
return Qnil;
}

static VALUE rb_git_remote_each(int argc, VALUE *argv, VALUE self)
{
git_repository *repo;
git_strarray remotes;
size_t i;
int error;
VALUE rb_repo;

rb_scan_args(argc, argv, "11", &rb_repo);

if (!rb_block_given_p())
return rb_funcall(self, rb_intern("to_enum"), 3, CSTR2SYM("each"), rb_repo);

if (!rb_obj_is_kind_of(rb_repo, rb_cRuggedRepo))
rb_raise(rb_eTypeError, "Expeting a Rugged::Repository instance");

Data_Get_Struct(rb_repo, git_repository, repo);

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

for (i = 0; i < remotes.count; ++i)
rb_yield(rugged_str_new2(remotes.strings[i], NULL));

git_strarray_free(&remotes);
return Qnil;
}

void Init_rugged_remote()
{
rb_cRuggedRemote = rb_define_class_under(rb_mRugged, "Remote", rb_cObject);

rb_define_singleton_method(rb_cRuggedRemote, "new", rb_git_remote__new, -1);
rb_define_singleton_method(rb_cRuggedRemote, "each", rb_git_remote_each, -1);

rb_define_method(rb_cRuggedRemote, "connect", rb_git_remote_connect, 1);
rb_define_method(rb_cRuggedRemote, "disconnect", rb_git_remote_disconnect, 0);
Expand Down
4 changes: 4 additions & 0 deletions lib/rugged/repository.rb
Expand Up @@ -48,6 +48,10 @@ def ref_names
def tags(pattern="")
Rugged::Tag.each(self, pattern)
end

def remotes
Rugged::Remote.each(self)
end
end

end
2 changes: 2 additions & 0 deletions test/fixtures/testrepo.git/config
Expand Up @@ -4,3 +4,5 @@
bare = false
logallrefupdates = true
ignorecase = true
[remote "libgit2"]
url = git://github.com/libgit2/libgit2.git
6 changes: 6 additions & 0 deletions test/remote_test.rb
Expand Up @@ -16,4 +16,10 @@
assert !remote.connected?
end

test "can list remotes" do
remotes = @repo.remotes
assert remotes.kind_of? Enumerable
assert_equal [ "libgit2" ], remotes.to_a
end

end

0 comments on commit 90cb029

Please sign in to comment.