Exemple #1
0
int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
    int err;
    char *c_key;
    char *py_str;

    if (!(c_key = py_str_to_c_str(py_key,NULL)))
        return -1;

    if (!py_value) {
        err = git_config_delete_entry(self->config, c_key);
    } else if (PyBool_Check(py_value)) {
        err = git_config_set_bool(self->config, c_key,
                (int)PyObject_IsTrue(py_value));
    } else if (PyInt_Check(py_value)) {
        err = git_config_set_int64(self->config, c_key,
                (int64_t)PyInt_AsLong(py_value));
    } else {
        py_value = PyObject_Str(py_value);
        py_str = py_str_to_c_str(py_value,NULL);
        err = git_config_set_string(self->config, c_key, py_str);
        free(py_str);
    }

    free(c_key);
    if (err < 0) {
        Error_set(err);
        return -1;
    }
    return 0;
}
Exemple #2
0
int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
    int err;
    char *key, *value;

    key = py_str_to_c_str(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_to_c_str(py_value, NULL);
        err = git_config_set_string(self->config, key, value);
        free(value);
    }

    free(key);
    if (err < 0) {
        Error_set(err);
        return -1;
    }
    return 0;
}
Exemple #3
0
PyObject *
Refspec_rtransform(Refspec *self, PyObject *py_str)
{
    char *str, *trans;
    int err, len, alen;
    PyObject *py_trans;

    str = py_str_to_c_str(py_str, NULL);
    alen = len = strlen(str);

    do {
        alen *= alen;
        trans = malloc(alen);
        if (!trans) {
            free(str);
            return PyErr_NoMemory();
        }

        err = git_refspec_rtransform(trans, alen, self->refspec, str);
    } while(err == GIT_EBUFS);
    free(str);

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

    py_trans = to_unicode(trans, NULL, NULL);

    free(trans);

    return py_trans;
}
Exemple #4
0
PyObject *
Repository_revparse_single(Repository *self, PyObject *py_spec)
{
    git_object *c_obj;
    char *c_spec;
    char *encoding = "ascii";
    int err;

    /* 1- Get the C revision spec */
    c_spec = py_str_to_c_str(py_spec, encoding);
    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);
        free(c_spec);
        return err_obj;
    }
    free(c_spec);

    return wrap_object(c_obj, self);
}
Exemple #5
0
PyObject *
Config_getitem(Config *self, PyObject *py_key)
{
    int err;
    int64_t       c_intvalue;
    int           c_boolvalue;
    const char   *c_charvalue;
    const char   *c_key;

    if (!(c_key = py_str_to_c_str(py_key,NULL)))
        return NULL;

    err = git_config_get_int64(&c_intvalue, self->config, c_key);
    if (err == GIT_OK) {
        return PyInt_FromLong((long)c_intvalue);
    }

    err = git_config_get_bool(&c_boolvalue, self->config, c_key);
    if (err == GIT_OK) {
        return PyBool_FromLong((long)c_boolvalue);
    }

    err = git_config_get_string(&c_charvalue, self->config, c_key);
    if (err < 0) {
        if (err == GIT_ENOTFOUND) {
            PyErr_SetObject(PyExc_KeyError, py_key);
            return NULL;
        }
        return Error_set(err);
    }

    return PyUnicode_FromString(c_charvalue);
}
Exemple #6
0
int
IndexEntry_path__set__(IndexEntry *self, PyObject *py_path)
{
    char *c_path;

    c_path = py_str_to_c_str(py_path, NULL);
    if (!c_path)
        return -1;

    free(self->entry.path);
    self->entry.path = c_path;

    return 0;
}
Exemple #7
0
PyObject *
reference_is_valid_name(PyObject *self, PyObject *py_refname)
{
    char* refname;
    int result;

    refname = py_str_to_c_str(py_refname, NULL);
    if (refname == NULL) {
        return NULL;
    }

    result = git_reference_is_valid_name(refname);
    free(refname);
    return PyBool_FromLong(result);
}
Exemple #8
0
PyObject *
Refspec_dst_matches(Refspec *self, PyObject *py_str)
{
    char *str;
    int res;

    str = py_str_to_c_str(py_str, NULL);
    if (!str)
        return NULL;

    res = git_refspec_dst_matches(self->refspec, str);
    free(str);

    if (res)
        Py_RETURN_TRUE;

    Py_RETURN_FALSE;
}
Exemple #9
0
int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
{
    Py_ssize_t index, n;
    PyObject *item;
    void *ptr;

    if (!PyList_Check(pylist)) {
        PyErr_SetString(PyExc_TypeError, "Value must be a list");
        return -1;
    }

    n = PyList_Size(pylist);

    /* allocate new git_strarray */
    ptr = calloc(n, sizeof(char *));
    if (!ptr) {
        PyErr_SetNone(PyExc_MemoryError);
        return -1;
    }

    array->strings = ptr;
    array->count = n;

    for (index = 0; index < n; index++) {
        item = PyList_GetItem(pylist, index);
        char *str = py_str_to_c_str(item, NULL);
        if (!str)
            goto on_error;

        array->strings[index] = str;
    }

    return 0;

on_error:
    n = index;
    for (index = 0; index < n; index++) {
        free(array->strings[index]);
    }
    free(array->strings);

    return -1;
}
Exemple #10
0
int
Remote_push_url__set__(Remote *self, PyObject* py_url)
{
    int err;
    char* url = NULL;

    url = py_str_to_c_str(py_url, NULL);
    if (url != NULL) {
        err = git_remote_set_pushurl(self->remote, url);
        free(url);

        if (err == GIT_OK)
            return 0;

        Error_set(err);
    }

    return -1;
}
Exemple #11
0
int
Remote_name__set__(Remote *self, PyObject* py_name)
{
    int err;
    char* name;

    name = py_str_to_c_str(py_name, NULL);
    if (name != NULL) {
        err = git_remote_rename(self->remote, name, NULL, NULL);
        free(name);

        if (err == GIT_OK)
            return 0;

        Error_set(err);
    }

    return -1;
}
Exemple #12
0
int
Repository_head__set__(Repository *self, PyObject *py_refname)
{
    int err;
    char *refname;

    refname = py_str_to_c_str(py_refname, NULL);
    if (refname == NULL)
        return -1;

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

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

    if (!(c_key = py_str_to_c_str(py_key,NULL)))
        return -1;

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

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

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

    key = py_str_to_c_str(py_key, NULL);
    if (key == NULL)
        return NULL;

    err = git_config_get_string(&value_str, self->config, key);
    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:
    free(key);

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

        return Error_set(err);
    }

    return py_value;
}
Exemple #15
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;
    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)
        goto out;

    message = py_str_to_c_str(py_message, encoding);
    if (message == NULL)
        goto out;

    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:
    free(message);
    git_tree_free(tree);
    while (i > 0) {
        i--;
        git_commit_free(parents[i]);
    }
    free(parents);
    return py_result;
}
Exemple #16
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;
}