From 487352912971301f598e1bdd5f52683688435d82 Mon Sep 17 00:00:00 2001
From: Nico von Geyso <Nico.Geyso@FU-Berlin.de>
Date: Sat, 16 Feb 2013 20:46:21 +0100
Subject: [PATCH] added setter for remote fetchspec

---
 src/remote.c        | 31 +++++++++++++++++++++++++++++--
 test/test_remote.py |  6 ++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/remote.c b/src/remote.c
index dc553f3..1aae29e 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -123,7 +123,7 @@ Remote_url__set__(Remote *self, PyObject* py_url)
 PyDoc_STRVAR(Remote_fetchspec__doc__,
   "= (source:str, destination:str)\n"
   "\n"
-  "Name of the remote source and destination refspecs\n");
+  "Name of the remote source and destination fetch refspecs\n");
 
 
 PyObject *
@@ -146,12 +146,39 @@ Remote_fetchspec__get__(Remote *self)
     return Error_set(GIT_ENOTFOUND);
 }
 
+int
+Remote_fetchspec__set__(Remote *self, PyObject* py_tuple)
+{
+    int err;
+    size_t length = 0;
+    char* src = NULL, *dst = NULL, *buf = NULL;
+
+    if (!PyArg_ParseTuple(py_tuple, "ss", &src, &dst))
+        return -1;
+
+    // length is strlen('+' + src + ':' + dst) and Null-Byte
+    length = strlen(src) + strlen(dst) + 3; 
+    buf = (char*) calloc(length, sizeof(char));
+    if (buf != NULL) {
+        sprintf(buf, "+%s:%s", src, dst);
+        err = git_remote_set_fetchspec(self->remote, buf);
+        free(buf);
+
+        if (err == GIT_OK)
+          return 0;
+
+        Error_set_exc(PyExc_ValueError);
+    }
+
+    return -1;
+}
+
 
 PyGetSetDef Remote_getseters[] = {
     GETSET(Remote, name),
     GETSET(Remote, url),
     GETSET(Remote, url),
-    GETTER(Remote, fetchspec),
+    GETSET(Remote, fetchspec),
     {NULL}
 };
 
diff --git a/test/test_remote.py b/test/test_remote.py
index b7595eb..7ac7aac 100644
--- a/test/test_remote.py
+++ b/test/test_remote.py
@@ -77,6 +77,12 @@ class RepositoryTest(utils.RepoTestCase):
         self.assertEqual(REMOTE_FETCHSPEC_SRC, remote.fetchspec[0])
         self.assertEqual(REMOTE_FETCHSPEC_DST, remote.fetchspec[1])
 
+        new_fetchspec = ('refs/foo/*','refs/remotes/foo/*')
+        remote.fetchspec = new_fetchspec
+        self.assertEqual(new_fetchspec[0], remote.fetchspec[0])
+        self.assertEqual(new_fetchspec[1], remote.fetchspec[1])
+
+
     def test_remote_list(self):
         self.assertEqual(1, len(self.repo.remotes))
         remote = self.repo.remotes[0]