Skip to content

Commit 12d83a9

Browse files
committed
Implement update_submodules using ffi.
1 parent f512bd9 commit 12d83a9

File tree

2 files changed

+26
-69
lines changed

2 files changed

+26
-69
lines changed

pygit2/repository.py

+26
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ def lookup_submodule(self, path):
8080
check_error(err)
8181
return Submodule._from_c(self, csub[0])
8282

83+
def update_submodules(self, submodules=None, init=False, callbacks=None):
84+
if submodules is None:
85+
submodules = self.listall_submodules()
86+
87+
# prepare options
88+
opts = ffi.new('git_submodule_update_options *')
89+
C.git_submodule_update_init_options(
90+
opts,
91+
C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION)
92+
if callbacks is not None:
93+
callbacks._fill_fetch_options(opts.fetch_opts)
94+
if init:
95+
i = 1
96+
else:
97+
i = 0
98+
99+
for submodule in submodules:
100+
submodule_instance = self.lookup_submodule(submodule)
101+
err = C.git_submodule_update(
102+
submodule_instance._subm,
103+
i,
104+
opts)
105+
check_error(err)
106+
107+
return None
108+
83109
#
84110
# Mapping interface
85111
#

src/repository.c

-69
Original file line numberDiff line numberDiff line change
@@ -1357,74 +1357,6 @@ Repository_init_submodules(Repository* self, PyObject *args, PyObject *kwds)
13571357
Py_RETURN_NONE;
13581358
}
13591359

1360-
PyDoc_STRVAR(Repository_update_submodules__doc__,
1361-
"update_submodules(submodules=None, init=False)\n"
1362-
"\n"
1363-
"Updates the specified submodules, or all if None are specified\n"
1364-
"init: Flag indicating if submodules should be automatically initialized if necessary.\n");
1365-
1366-
static int foreach_sub_update_cb(git_submodule *submodule, const char *name, void *payload)
1367-
{
1368-
git_submodule_update_options opts = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
1369-
return git_submodule_update(submodule, *(int*)payload, &opts);
1370-
}
1371-
1372-
PyObject *
1373-
Repository_update_submodules(Repository *self, PyObject *args, PyObject *kwds)
1374-
{
1375-
PyObject *list = Py_None;
1376-
PyObject *py_init = Py_False;
1377-
PyObject *iter, *next, *subpath;
1378-
int init, err;
1379-
const char *c_subpath;
1380-
git_submodule *submodule;
1381-
git_submodule_update_options opts = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
1382-
1383-
char *kwlist[] = {"submodules", "init", NULL};
1384-
1385-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &list, &py_init))
1386-
return NULL;
1387-
1388-
init = PyObject_IsTrue(py_init);
1389-
1390-
if (init != 0 && init != 1)
1391-
init = 0;
1392-
1393-
if (list == Py_None) {
1394-
err = git_submodule_foreach(self->repo, foreach_sub_update_cb, &init);
1395-
if (err != 0)
1396-
return Error_set(err);
1397-
Py_RETURN_NONE;
1398-
}
1399-
1400-
iter = PyObject_GetIter(list);
1401-
if (!iter)
1402-
return NULL;
1403-
1404-
while (1) {
1405-
next = PyIter_Next(iter);
1406-
if (!next)
1407-
break;
1408-
1409-
c_subpath = py_str_borrow_c_str(&subpath, next, NULL);
1410-
1411-
git_submodule_lookup(&submodule, self->repo, c_subpath);
1412-
Py_DECREF(subpath);
1413-
if (!submodule) {
1414-
PyErr_SetString(PyExc_KeyError,
1415-
"Submodule does not exist");
1416-
return NULL;
1417-
}
1418-
1419-
err = git_submodule_update(submodule, init, &opts);
1420-
if (err != 0) {
1421-
return Error_set(err);
1422-
}
1423-
}
1424-
1425-
Py_RETURN_NONE;
1426-
}
1427-
14281360
PyDoc_STRVAR(Repository_lookup_reference__doc__,
14291361
"lookup_reference(name) -> Reference\n"
14301362
"\n"
@@ -1879,7 +1811,6 @@ PyMethodDef Repository_methods[] = {
18791811
METHOD(Repository, listall_reference_objects, METH_NOARGS),
18801812
METHOD(Repository, listall_submodules, METH_NOARGS),
18811813
METHOD(Repository, init_submodules, METH_VARARGS | METH_KEYWORDS),
1882-
METHOD(Repository, update_submodules, METH_VARARGS | METH_KEYWORDS),
18831814
METHOD(Repository, lookup_reference, METH_O),
18841815
METHOD(Repository, revparse_single, METH_O),
18851816
METHOD(Repository, status, METH_NOARGS),

0 commit comments

Comments
 (0)