Allow creating a remote with a particular fetch refspec

This makes it a lot more convenient to create a remote and override the
fetch refspec it gets created with.
This commit is contained in:
Carlos Martín Nieto
2015-04-28 13:39:15 +02:00
parent 3091c7aa87
commit 0ce4d3b9a8
3 changed files with 23 additions and 3 deletions

View File

@@ -193,6 +193,7 @@ int git_remote_create(
git_repository *repo,
const char *name,
const char *url);
int git_remote_create_with_fetchspec(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch);
int git_remote_delete(git_repository *repo, const char *name);
int git_repository_state_cleanup(git_repository *repo);

View File

@@ -518,15 +518,21 @@ class RemoteCollection(object):
return Remote(self._repo, cremote[0])
def create(self, name, url):
"""create(name, url) -> Remote
def create(self, name, url, fetch=None):
"""create(name, url [, fetch]) -> Remote
Create a new remote with the given name and url.
If 'fetch' is provided, this fetch refspec will be used instead of the default
"""
cremote = ffi.new('git_remote **')
if fetch:
err = C.git_remote_create_with_fetchspec(cremote, self._repo._repo, to_bytes(name), to_bytes(url), to_bytes(fetch))
else:
err = C.git_remote_create(cremote, self._repo._repo, to_bytes(name), to_bytes(url))
check_error(err)
return Remote(self._repo, cremote[0])

View File

@@ -62,6 +62,19 @@ class RepositoryTest(utils.RepoTestCase):
self.assertRaises(ValueError, self.repo.create_remote, *(name, url))
def test_remote_create_with_refspec(self):
name = 'upstream'
url = 'git://github.com/libgit2/pygit2.git'
fetch = "+refs/*:refs/*"
remote = self.repo.remotes.create(name, url, fetch)
self.assertEqual(type(remote), pygit2.Remote)
self.assertEqual(name, remote.name)
self.assertEqual(url, remote.url)
self.assertEqual([fetch], remote.fetch_refspecs)
self.assertEqual(None, remote.push_url)
def test_remote_delete(self):
name = 'upstream'
url = 'git://github.com/libgit2/pygit2.git'