Remote: make renaming take a method call

Renaming a remote in pygit2 has been done via Remote.name= up to now,
but this is inherently unsafe, as it provides no way to pass up the
refspecs that libgit2 was unable to remap.

In fact, if there ever was such problem, we would have segfaulted.

libgit2 now provides a much more direct way of getting back the results,
so expose it as the return value of Remote.rename(). This also removes
the hint that a rename might be something that happens only to the
in-memory structure.
This commit is contained in:
Carlos Martín Nieto
2014-06-08 20:35:21 +02:00
parent 130fff6f2c
commit bde58d9727
3 changed files with 25 additions and 13 deletions

View File

@@ -116,11 +116,11 @@ int git_remote_create(
const char *url);
const char * git_remote_name(const git_remote *remote);
typedef int (*git_remote_rename_problem_cb)(const char *problematic_refspec, void *payload);
int git_remote_rename(git_remote *remote,
const char *new_name,
git_remote_rename_problem_cb callback,
void *payload);
int git_remote_rename(
git_strarray *problems,
git_remote *remote,
const char *new_name);
const char * git_remote_url(const git_remote *remote);
int git_remote_set_url(git_remote *remote, const char* url);
const char * git_remote_pushurl(const git_remote *remote);

View File

@@ -142,14 +142,25 @@ class Remote(object):
return maybe_string(C.git_remote_name(self._remote))
@name.setter
def name(self, value):
if not value:
def rename(self, new_name):
"""Rename this remote
Returns a list of fetch refspecs which were not in the standard format
and thus could not be remapped
"""
if not new_name:
raise ValueError("New remote name must be a non-empty string")
err = C.git_remote_rename(self._remote, to_str(value), ffi.NULL, ffi.NULL)
problems = ffi.new('git_strarray *')
err = C.git_remote_rename(problems, self._remote, to_str(new_name))
check_error(err)
ret = strarray_to_strings(problems)
C.git_strarray_free(problems)
return ret
@property
def url(self):
"""Url of the remote"""

View File

@@ -61,11 +61,12 @@ class RepositoryTest(utils.RepoTestCase):
remote = self.repo.remotes[0]
self.assertEqual(REMOTE_NAME, remote.name)
remote.name = 'new'
problems = remote.rename('new')
self.assertEqual([], problems)
self.assertEqual('new', remote.name)
self.assertRaisesAssign(ValueError, remote, 'name', '')
self.assertRaisesAssign(ValueError, remote, 'name', None)
self.assertRaises(ValueError, remote.rename, '')
self.assertRaises(ValueError, remote.rename, None)
def test_remote_set_url(self):
@@ -153,7 +154,7 @@ class RepositoryTest(utils.RepoTestCase):
def test_remote_save(self):
remote = self.repo.remotes[0]
remote.name = 'new-name'
remote.rename('new-name')
remote.url = 'http://example.com/test.git'
remote.save()