Example #1
0
int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
    int err;
    const char *key, *value;
    PyObject *tkey, *tvalue;

    key = py_str_borrow_c_str(&tkey, py_key, NULL);
    if (key == NULL)
        return -1;

    if (py_value == NULL)
        err = git_config_delete_entry(self->config, key);
    else if (PyBool_Check(py_value)) {
        err = git_config_set_bool(self->config, key,
                (int)PyObject_IsTrue(py_value));
    } else if (PyLong_Check(py_value)) {
        err = git_config_set_int64(self->config, key,
                (int64_t)PyLong_AsLong(py_value));
    } else {
        value = py_str_borrow_c_str(&tvalue, py_value, NULL);
        err = git_config_set_string(self->config, key, value);
        Py_DECREF(tvalue);
    }

    Py_DECREF(tkey);
    if (err < 0) {
        Error_set(err);
        return -1;
    }
    return 0;
}
Example #2
0
int
Repository_head__set__(Repository *self, PyObject *py_val)
{
    int err;
    if (PyObject_TypeCheck(py_val, &OidType)) {
        git_oid oid;
        py_oid_to_git_oid(py_val, &oid);
        err = git_repository_set_head_detached(self->repo, &oid, NULL, NULL);
        if (err < 0) {
            Error_set(err);
            return -1;
        }
    } else {
        const char *refname;
        PyObject *trefname;

        refname = py_str_borrow_c_str(&trefname, py_val, NULL);
        if (refname == NULL)
            return -1;

        err = git_repository_set_head(self->repo, refname, NULL, NULL);
        Py_DECREF(trefname);
        if (err < 0) {
            Error_set_str(err, refname);
            return -1;
        }
    }

    return 0;
}
Example #3
0
PyObject *
Repository_revparse_single(Repository *self, PyObject *py_spec)
{
    git_object *c_obj;
    const char *c_spec;
    PyObject *tspec;
    int err;

    /* 1- Get the C revision spec */
    c_spec = py_str_borrow_c_str(&tspec, py_spec, NULL);
    if (c_spec == NULL)
        return NULL;

    /* 2- Lookup */
    err = git_revparse_single(&c_obj, self->repo, c_spec);

    if (err < 0) {
        PyObject *err_obj = Error_set_str(err, c_spec);
        Py_DECREF(tspec);
        return err_obj;
    }
    Py_DECREF(tspec);

    return wrap_object(c_obj, self);
}
Example #4
0
int
Signature_init(Signature *self, PyObject *args, PyObject *kwds)
{
    char *keywords[] = {"name", "email", "time", "offset", "encoding", NULL};
    PyObject *py_name, *tname;
    char *email, *encoding = "utf-8";
    const char *name;
    long long time = -1;
    int offset = 0;
    int err;
    git_signature *signature;

    if (!PyArg_ParseTupleAndKeywords(
                args, kwds, "Os|Lis", keywords,
                &py_name, &email, &time, &offset, &encoding))
        return -1;

    name = py_str_borrow_c_str(&tname, py_name, encoding);
    if (name == NULL)
        return -1;

    if (time == -1) {
        err = git_signature_now(&signature, name, email);
    } else {
        err = git_signature_new(&signature, name, email, time, offset);
    }
    Py_DECREF(tname);

    if (err < 0) {
        Error_set(err);
        return -1;
    }

    self->obj = NULL;
    self->signature = signature;

    if (encoding) {
        self->encoding = strdup(encoding);
        if (self->encoding == NULL) {
            PyErr_NoMemory();
            return -1;
        }
    }

    return 0;
}
Example #5
0
int
Repository_workdir__set__(Repository *self, PyObject *py_workdir)
{
    int err;
    const char *workdir;
    PyObject *tworkdir;

    workdir = py_str_borrow_c_str(&tworkdir, py_workdir, NULL);
    if (workdir == NULL)
        return -1;

    err = git_repository_set_workdir(self->repo, workdir, 0 /* update_gitlink */);
    Py_DECREF(tworkdir);
    if (err < 0) {
        Error_set_str(err, workdir);
        return -1;
    }

    return 0;
}
Example #6
0
int
Repository_head__set__(Repository *self, PyObject *py_refname)
{
    int err;
    const char *refname;
    PyObject *trefname;

    refname = py_str_borrow_c_str(&trefname, py_refname, NULL);
    if (refname == NULL)
        return -1;

    err = git_repository_set_head(self->repo, refname);
    Py_DECREF(trefname);
    if (err < 0) {
        Error_set_str(err, refname);
        return -1;
    }

    return 0;
}
Example #7
0
int
Config_contains(Config *self, PyObject *py_key) {
    int err;
    const char *c_value, *c_key;
    PyObject *tkey;

    c_key = py_str_borrow_c_str(&tkey, py_key, NULL);
    if (c_key == NULL)
        return -1;

    err = git_config_get_string(&c_value, self->config, c_key);
    Py_DECREF(tkey);

    if (err < 0) {
        if (err == GIT_ENOTFOUND)
            return 0;

        Error_set(err);
        return -1;
    }

    return 1;
}
Example #8
0
PyObject *
Config_getitem(Config *self, PyObject *py_key)
{
    int64_t value_int;
    int err, value_bool;
    const char *value_str;
    const char *key;
    PyObject* py_value, *tmp;

    key = py_str_borrow_c_str(&tmp, py_key, NULL);
    if (key == NULL)
        return NULL;

    err = git_config_get_string(&value_str, self->config, key);
    Py_CLEAR(tmp);
    if (err < 0)
        goto cleanup;

    if (git_config_parse_int64(&value_int, value_str) == 0)
        py_value = PyLong_FromLongLong(value_int);
    else if(git_config_parse_bool(&value_bool, value_str) == 0)
        py_value = PyBool_FromLong(value_bool);
    else
        py_value = to_unicode(value_str, NULL, NULL);

cleanup:
    if (err < 0) {
        if (err == GIT_ENOTFOUND) {
            PyErr_SetObject(PyExc_KeyError, py_key);
            return NULL;
        }

        return Error_set(err);
    }

    return py_value;
}
Example #9
0
PyObject *
Repository_create_commit(Repository *self, PyObject *args)
{
    Signature *py_author, *py_committer;
    PyObject *py_oid, *py_message, *py_parents, *py_parent;
    PyObject *py_result = NULL;
    PyObject *tmessage;
    const char *message = NULL;
    char *update_ref = NULL;
    char *encoding = NULL;
    git_oid oid;
    git_tree *tree = NULL;
    int parent_count;
    git_commit **parents = NULL;
    int err = 0, i = 0;
    size_t len;

    if (!PyArg_ParseTuple(args, "zO!O!OOO!|s",
                          &update_ref,
                          &SignatureType, &py_author,
                          &SignatureType, &py_committer,
                          &py_message,
                          &py_oid,
                          &PyList_Type, &py_parents,
                          &encoding))
        return NULL;

    len = py_oid_to_git_oid(py_oid, &oid);
    if (len == 0)
        return NULL;

    message = py_str_borrow_c_str(&tmessage, py_message, encoding);
    if (message == NULL)
        return NULL;

    err = git_tree_lookup_prefix(&tree, self->repo, &oid, len);
    if (err < 0) {
        Error_set(err);
        goto out;
    }

    parent_count = (int)PyList_Size(py_parents);
    parents = malloc(parent_count * sizeof(git_commit*));
    if (parents == NULL) {
        PyErr_SetNone(PyExc_MemoryError);
        goto out;
    }
    for (; i < parent_count; i++) {
        py_parent = PyList_GET_ITEM(py_parents, i);
        len = py_oid_to_git_oid(py_parent, &oid);
        if (len == 0)
            goto out;
        err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);
        if (err < 0) {
            Error_set(err);
            goto out;
        }
    }

    err = git_commit_create(&oid, self->repo, update_ref,
                            py_author->signature, py_committer->signature,
                            encoding, message, tree, parent_count,
                            (const git_commit**)parents);
    if (err < 0) {
        Error_set(err);
        goto out;
    }

    py_result = git_oid_to_python(&oid);

out:
    Py_DECREF(tmessage);
    git_tree_free(tree);
    while (i > 0) {
        i--;
        git_commit_free(parents[i]);
    }
    free(parents);
    return py_result;
}
Example #10
0
PyObject *
option(PyObject *self, PyObject *args)
{
    long option;
    int error;
    PyObject *py_option;

    py_option = PyTuple_GetItem(args, 0);
    if (!py_option)
        return NULL;

    if (!PyLong_Check(py_option))
        goto on_non_integer;

    option = PyLong_AsLong(py_option);

    switch (option) {
        case GIT_OPT_GET_SEARCH_PATH:
        {
            PyObject *py_level;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            if (!PyLong_Check(py_level))
                goto on_non_integer;

            return get_search_path(PyLong_AsLong(py_level));
            break;
        }

        case GIT_OPT_SET_SEARCH_PATH:
        {
            PyObject *py_level, *py_path, *tpath;
            const char *path;
            int err;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            py_path = PyTuple_GetItem(args, 2);
            if (!py_path)
                return NULL;

            if (!PyLong_Check(py_level))
                goto on_non_integer;

            path = py_str_borrow_c_str(&tpath, py_path, NULL);
            if (!path)
                return NULL;

            err = git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, PyLong_AsLong(py_level), path);
            Py_DECREF(tpath);

            if (err < 0) {
                Error_set(err);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_GET_MWINDOW_SIZE:
        {
            size_t size;

            error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &size);
            if (error < 0) {
                Error_set(error);
                return NULL;
            }

            return PyLong_FromSize_t(size);

            break;
        }

        case GIT_OPT_SET_MWINDOW_SIZE:
        {
            size_t size;
            PyObject *py_size;

            py_size = PyTuple_GetItem(args, 1);
            if (!py_size)
                return NULL;

            if (!PyLong_Check(py_size))
                goto on_non_integer;

            size = PyLong_AsSize_t(py_size);
            error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, size);
            if (error  < 0) {
                Error_set(error);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT:
        {
            size_t limit;

            error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, &limit);
            if (error < 0) {
                Error_set(error);
                return NULL;
            }

            return PyLong_FromSize_t(limit);

            break;
        }

        case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT:
        {
            size_t limit;
            PyObject *py_limit;

            py_limit = PyTuple_GetItem(args, 1);
            if (!py_limit)
                return NULL;

            if (!PyLong_Check(py_limit))
                goto on_non_integer;

            limit = PyLong_AsSize_t(py_limit);
            error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, limit);
            if (error  < 0) {
                Error_set(error);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
        {
            size_t limit;
            int object_type;
            PyObject *py_object_type, *py_limit;

            py_object_type = PyTuple_GetItem(args, 1);
            if (!py_object_type)
                return NULL;

            py_limit = PyTuple_GetItem(args, 2);
            if (!py_limit)
                return NULL;

            if (!PyLong_Check(py_limit))
                goto on_non_integer;

            object_type = PyLong_AsLong(py_object_type);
            limit = PyLong_AsSize_t(py_limit);
            error = git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, limit);

            if (error < 0) {
                Error_set(error);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_SET_CACHE_MAX_SIZE:
        {
          size_t max_size;
          PyObject *py_max_size;

          py_max_size = PyTuple_GetItem(args, 1);
          if (!py_max_size)
              return NULL;

          if (!PyLong_Check(py_max_size))
              goto on_non_integer;

          max_size = PyLong_AsSize_t(py_max_size);
          error = git_libgit2_opts(GIT_OPT_SET_CACHE_MAX_SIZE, max_size);
          if (error  < 0) {
              Error_set(error);
              return NULL;
          }

          Py_RETURN_NONE;
          break;
        }

        case GIT_OPT_ENABLE_CACHING:
        {
            int flag;
            PyObject *py_flag;

            py_flag = PyTuple_GetItem(args, 1);

            if (!PyLong_Check(py_flag))
                goto on_non_integer;

            flag = PyLong_AsSize_t(py_flag);
            error = git_libgit2_opts(GIT_OPT_ENABLE_CACHING, flag);
            if (error  < 0) {
                Error_set(error);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_GET_CACHED_MEMORY:
        {
            size_t current;
            size_t allowed;
            PyObject* tup = PyTuple_New(2);

            error = git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &current, &allowed);
            if (error < 0) {
                Error_set(error);
                return NULL;
            }
            PyTuple_SetItem(tup, 0, PyLong_FromLong(current));
            PyTuple_SetItem(tup, 1, PyLong_FromLong(allowed));

            return tup;

            break;
        }

    }

    PyErr_SetString(PyExc_ValueError, "unknown/unsupported option value");
    return NULL;

on_non_integer:
    PyErr_SetString(PyExc_TypeError, "option is not an integer");
    return NULL;
}
Example #11
0
PyObject *
option(PyObject *self, PyObject *args)
{
    long option;
    int error;
    PyObject *py_option;

    py_option = PyTuple_GetItem(args, 0);
    if (!py_option)
        return NULL;

    if (!PyLong_Check(py_option))
        goto on_non_integer;

    option = PyLong_AsLong(py_option);

    switch (option) {
        case GIT_OPT_GET_SEARCH_PATH:
        {
            PyObject *py_level;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            if (!PyLong_Check(py_level))
                goto on_non_integer;

            return get_search_path(PyLong_AsLong(py_level));
            break;
        }

        case GIT_OPT_SET_SEARCH_PATH:
        {
            PyObject *py_level, *py_path, *tpath;
            const char *path;
            int err;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            py_path = PyTuple_GetItem(args, 2);
            if (!py_path)
                return NULL;

            if (!PyLong_Check(py_level))
                goto on_non_integer;

            path = py_str_borrow_c_str(&tpath, py_path, NULL);
            if (!path)
                return NULL;

            err = git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, PyLong_AsLong(py_level), path);
            Py_DECREF(tpath);

            if (err < 0) {
                Error_set(err);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }

        case GIT_OPT_GET_MWINDOW_SIZE:
        {
            size_t size;

            error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &size);
            if (error < 0) {
                Error_set(error);
                return NULL;
            }

            return PyLong_FromSize_t(size);

            break;
        }

        case GIT_OPT_SET_MWINDOW_SIZE:
        {
            size_t size;
            PyObject *py_size;

            py_size = PyTuple_GetItem(args, 1);
            if (!py_size)
                return NULL;

            if (!PyLong_Check(py_size))
                goto on_non_integer;

            size = PyLong_AsSize_t(py_size);
            error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, size);
            if (error  < 0) {
                Error_set(error);
                return NULL;
            }

            Py_RETURN_NONE;
            break;
        }
    }

    PyErr_SetString(PyExc_ValueError, "unknown/unsupported option value");
    return NULL;

on_non_integer:
    PyErr_SetString(PyExc_TypeError, "option is not an integer");
    return NULL;
}
Example #12
0
PyObject *
option(PyObject *self, PyObject *args)
{
    long option;
    int error;
    PyObject *py_option;

    py_option = PyTuple_GetItem(args, 0);
    if (!py_option)
        return NULL;

    if (!PyInt_Check(py_option))
        return Error_type_error(
            "option should be an integer, got %.200s", py_option);

    option = PyInt_AsLong(py_option);

    switch (option) {

        case GIT_OPT_GET_MWINDOW_SIZE:
        {
            size_t size;

            error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &size);
            if (error < 0)
                return Error_set(error);

            return PyInt_FromSize_t(size);
        }

        case GIT_OPT_SET_MWINDOW_SIZE:
        {
            size_t size;
            PyObject *py_size;

            py_size = PyTuple_GetItem(args, 1);
            if (!py_size)
                return NULL;

            if (!PyInt_Check(py_size))
                return Error_type_error(
                    "size should be an integer, got %.200s", py_size);

            size = PyInt_AsSize_t(py_size);
            error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, size);
            if (error  < 0)
                return Error_set(error);

            Py_RETURN_NONE;
        }

        case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT:
        {
            size_t limit;

            error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, &limit);
            if (error < 0)
                return Error_set(error);

            return PyInt_FromSize_t(limit);
        }

        case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT:
        {
            size_t limit;
            PyObject *py_limit;

            py_limit = PyTuple_GetItem(args, 1);
            if (!py_limit)
                return NULL;

            if (PyInt_Check(py_limit)) {
                limit = PyInt_AsSize_t(py_limit);
            } else if (PyLong_Check(py_limit)) {
                limit = PyLong_AsSize_t(py_limit);
            } else {
                return Error_type_error(
                    "limit should be an integer, got %.200s", py_limit);
            }

            error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, limit);
            if (error < 0)
                return Error_set(error);

            Py_RETURN_NONE;
        }

        case GIT_OPT_GET_SEARCH_PATH:
        {
            PyObject *py_level;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            if (!PyInt_Check(py_level))
                return Error_type_error(
                    "level should be an integer, got %.200s", py_level);

            return get_search_path(PyInt_AsLong(py_level));
        }

        case GIT_OPT_SET_SEARCH_PATH:
        {
            PyObject *py_level, *py_path, *tpath;
            const char *path;
            int err;

            py_level = PyTuple_GetItem(args, 1);
            if (!py_level)
                return NULL;

            py_path = PyTuple_GetItem(args, 2);
            if (!py_path)
                return NULL;

            if (!PyInt_Check(py_level))
                return Error_type_error(
                    "level should be an integer, got %.200s", py_level);

            path = py_str_borrow_c_str(&tpath, py_path, NULL);
            if (!path)
                return NULL;

            err = git_libgit2_opts(
                GIT_OPT_SET_SEARCH_PATH, PyInt_AsLong(py_level), path);
            Py_DECREF(tpath);

            if (err < 0)
                return Error_set(err);

            Py_RETURN_NONE;
        }

        case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
        {
            size_t limit;
            int object_type;
            PyObject *py_object_type, *py_limit;

            py_object_type = PyTuple_GetItem(args, 1);
            if (!py_object_type)
                return NULL;

            py_limit = PyTuple_GetItem(args, 2);
            if (!py_limit)
                return NULL;

            if (!PyInt_Check(py_limit))
                return Error_type_error(
                    "limit should be an integer, got %.200s", py_limit);

            object_type = PyInt_AsLong(py_object_type);
            limit = PyInt_AsSize_t(py_limit);
            error = git_libgit2_opts(
                GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, limit);

            if (error < 0)
                return Error_set(error);

            Py_RETURN_NONE;
        }

        case GIT_OPT_SET_CACHE_MAX_SIZE:
        {
            size_t max_size;
            PyObject *py_max_size;

            py_max_size = PyTuple_GetItem(args, 1);
            if (!py_max_size)
                return NULL;

            if (!PyInt_Check(py_max_size))
                return Error_type_error(
                    "max_size should be an integer, got %.200s", py_max_size);

            max_size = PyInt_AsSize_t(py_max_size);
            error = git_libgit2_opts(GIT_OPT_SET_CACHE_MAX_SIZE, max_size);
            if (error < 0)
                return Error_set(error);

            Py_RETURN_NONE;
        }

        case GIT_OPT_GET_CACHED_MEMORY:
        {
            size_t current;
            size_t allowed;
            PyObject* tup = PyTuple_New(2);

            error = git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &current, &allowed);
            if (error < 0)
                return Error_set(error);

            PyTuple_SetItem(tup, 0, PyInt_FromLong(current));
            PyTuple_SetItem(tup, 1, PyInt_FromLong(allowed));

            return tup;
        }

        case GIT_OPT_SET_SSL_CERT_LOCATIONS:
        {
            PyObject *py_file, *py_dir;
            const char *file_path, *dir_path;
            int err;

            py_file = PyTuple_GetItem(args, 1);
            py_dir = PyTuple_GetItem(args, 2);

            /* py_file and py_dir are only valid if they are strings */
            if (PyUnicode_Check(py_file) || PyBytes_Check(py_file)) {
                file_path = py_str_to_c_str(py_file, Py_FileSystemDefaultEncoding);
            } else {
                file_path = NULL;
            }

            if (PyUnicode_Check(py_dir) || PyBytes_Check(py_dir)) {
                dir_path = py_str_to_c_str(py_dir, Py_FileSystemDefaultEncoding);
            } else {
                dir_path = NULL;
            }

            err = git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, file_path, dir_path);

            if (err < 0)
                return Error_set(err);

            Py_RETURN_NONE;
        }

        // int enabled
        case GIT_OPT_ENABLE_CACHING:
        case GIT_OPT_ENABLE_STRICT_OBJECT_CREATION:
        case GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION:
        case GIT_OPT_ENABLE_OFS_DELTA:
        case GIT_OPT_ENABLE_FSYNC_GITDIR:
        case GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION:
        case GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY:
        {
            PyObject *py_enabled;
            int enabled;

            py_enabled = PyTuple_GetItem(args, 1);
            if (!py_enabled)
                return NULL;

            if (!PyInt_Check(py_enabled))
                return Error_type_error("expected integer, got %.200s", py_enabled);

            enabled = PyInt_AsSize_t(py_enabled);
            error = git_libgit2_opts(option, enabled);
            if (error < 0)
                return Error_set(error);

            Py_RETURN_NONE;
        }

        // Not implemented
        case GIT_OPT_GET_TEMPLATE_PATH:
        case GIT_OPT_SET_TEMPLATE_PATH:
        case GIT_OPT_SET_USER_AGENT:
        case GIT_OPT_SET_SSL_CIPHERS:
        case GIT_OPT_GET_USER_AGENT:
        case GIT_OPT_GET_WINDOWS_SHAREMODE:
        case GIT_OPT_SET_WINDOWS_SHAREMODE:
        case GIT_OPT_SET_ALLOCATOR:
        case GIT_OPT_GET_PACK_MAX_OBJECTS:
        case GIT_OPT_SET_PACK_MAX_OBJECTS:
        {
            Py_INCREF(Py_NotImplemented);
            return Py_NotImplemented;
        }

    }

    PyErr_SetString(PyExc_ValueError, "unknown/unsupported option value");
    return NULL;
}