Esempio n. 1
0
PyObject *
discover_repository(PyObject *self, PyObject *args)
{
    git_buf repo_path = {NULL};
    const char *path;
    PyObject *py_repo_path;
    int across_fs = 0;
    const char *ceiling_dirs = NULL;
    int err;

    if (!PyArg_ParseTuple(args, "s|Is", &path, &across_fs, &ceiling_dirs))
        return NULL;

    memset(&repo_path, 0, sizeof(git_buf));
    err = git_repository_discover(&repo_path, path, across_fs, ceiling_dirs);
    if (err == GIT_ENOTFOUND)
        Py_RETURN_NONE;
    if (err < 0)
        return Error_set_str(err, path);

    py_repo_path = to_path(repo_path.ptr);
    git_buf_dispose(&repo_path);

    return py_repo_path;
};
Esempio n. 2
0
PyObject *
init_file_backend(PyObject *self, PyObject *args)
{
    const char* path = NULL;
    int err = GIT_OK;
    git_repository *repository = NULL;
    if (!PyArg_ParseTuple(args, "s", &path)) {
        return NULL;
    }

    err = git_repository_open(&repository, path);
    if (err < 0) {
        Error_set_str(err, path);
        goto cleanup;
    }

    return PyCapsule_New(repository, "backend", NULL);

cleanup:
    if (repository) {
        git_repository_free(repository);
    }

    if (err == GIT_ENOTFOUND) {
        PyErr_Format(GitError, "Repository not found at %s", path);
    }

    return NULL;
}
Esempio n. 3
0
PyObject *
Index_add(Index *self, PyObject *args)
{
    int err;
    const char *path;
    IndexEntry *py_entry;

    if (PyArg_ParseTuple(args, "O!", &IndexEntryType, &py_entry)) {
        err = git_index_add(self->index, &py_entry->entry);
        if (err < 0)
            return Error_set(err);

        Py_RETURN_NONE;
    }
    PyErr_Clear();

    if (!PyArg_ParseTuple(args, "s", &path))
        return NULL;

    err = git_index_add_bypath(self->index, path);
    if (err < 0)
        return Error_set_str(err, path);

    Py_RETURN_NONE;
}
Esempio n. 4
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;
}
Esempio n. 5
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);
}
Esempio n. 6
0
File: index.c Progetto: guocb/pygit2
/* This is an internal function, used by Index_getitem and Index_setitem */
int
Index_get_position(Index *self, PyObject *value)
{
    char *path;
    int idx;

    /* Case 1: integer */
    if (PyInt_Check(value)) {
        idx = (int)PyInt_AsLong(value);
        if (idx == -1 && PyErr_Occurred())
            return -1;
        if (idx < 0) {
            PyErr_SetObject(PyExc_ValueError, value);
            return -1;
        }
        return idx;
    }

    /* Case 2: byte or text string */
    path = py_path_to_c_str(value);
    if (!path)
        return -1;
    idx = git_index_find(self->index, path);
    if (idx < 0) {
        Error_set_str(idx, path);
        free(path);
        return -1;
    }
    free(path);
    return idx;
}
Esempio n. 7
0
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
    char *path;
    int err;

    if (kwds) {
        PyErr_SetString(PyExc_TypeError,
                        "Repository takes no keyword arguments");
        return -1;
    }

    if (PySequence_Length(args) > 0) {
        if (!PyArg_ParseTuple(args, "s", &path)) {
            return -1;
        }
        err = git_config_open_ondisk(&self->config, path);
        if (err < 0) {
            Error_set_str(err, path);
            return -1;
        }
    } else {
        err = git_config_new(&self->config);
        if (err < 0) {
            Error_set(err);
            return -1;
        }
    }
    return 0;
}
Esempio n. 8
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);
}
Esempio n. 9
0
PyObject *
init_repository(PyObject *self, PyObject *args)
{
    git_repository *repo;
    Repository *py_repo;
    const char *path;
    unsigned int bare;
    int err;

    if (!PyArg_ParseTuple(args, "sI", &path, &bare))
        return NULL;

    err = git_repository_init(&repo, path, bare);
    if (err < 0)
        return Error_set_str(err, path);

    py_repo = PyObject_GC_New(Repository, &RepositoryType);
    if (py_repo) {
        py_repo->repo = repo;
        py_repo->index = NULL;
        PyObject_GC_Track(py_repo);
        return (PyObject*)py_repo;
    }

    git_repository_free(repo);
    return NULL;
};
Esempio n. 10
0
int
Repository_init(Repository *self, PyObject *args, PyObject *kwds)
{
    char *path;
    int err;

    if (kwds && PyDict_Size(kwds) > 0) {
        PyErr_SetString(PyExc_TypeError,
                        "Repository takes no keyword arguments");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "s", &path))
        return -1;

    err = git_repository_open(&self->repo, path);
    if (err < 0) {
        Error_set_str(err, path);
        return -1;
    }

    self->owned = 1;
    self->config = NULL;
    self->index = NULL;

    return 0;
}
Esempio n. 11
0
PyObject* Error_set_oid(int err, const git_oid *oid, size_t len)
{
    char hex[GIT_OID_HEXSZ + 1];

    git_oid_fmt(hex, oid);
    hex[len] = '\0';
    return Error_set_str(err, hex);
}
Esempio n. 12
0
File: index.c Progetto: guocb/pygit2
PyObject *
Index_add(Index *self, PyObject *args)
{
    int err;
    const char *path;

    if (!PyArg_ParseTuple(args, "s", &path))
        return NULL;

    err = git_index_add_from_workdir(self->index, path);
    if (err < 0)
        return Error_set_str(err, path);

    Py_RETURN_NONE;
}
Esempio n. 13
0
PyObject *
Index_add(Index *self, PyObject *args)
{
    int err;
    const char *path;
    int stage=0;

    if (!PyArg_ParseTuple(args, "s|i", &path, &stage))
        return NULL;

    err = git_index_add(self->index, path, stage);
    if (err < 0)
        return Error_set_str(err, path);

    Py_RETURN_NONE;
}
Esempio n. 14
0
File: index.c Progetto: guocb/pygit2
PyObject *
Index_find(Index *self, PyObject *py_path)
{
    char *path;
    long idx;

    path = PyString_AsString(py_path);
    if (!path)
        return NULL;

    idx = (long)git_index_find(self->index, path);
    if (idx < 0)
        return Error_set_str(idx, path);

    return PyInt_FromLong(idx);
}
Esempio n. 15
0
PyObject *
Index__find(Index *self, PyObject *py_path)
{
    char *path;
    size_t idx;
    int err;

    path = PyBytes_AsString(py_path);
    if (!path)
        return NULL;

    err = git_index_find(&idx, self->index, path);
    if (err < 0)
        return Error_set_str(err, path);

    return PyLong_FromSize_t(idx);
}
Esempio n. 16
0
PyObject *
init_repository(PyObject *self, PyObject *args) {
    git_repository *repo;
    const char *path;
    unsigned int bare;
    int err;

    if (!PyArg_ParseTuple(args, "sI", &path, &bare))
        return NULL;

    err = git_repository_init(&repo, path, bare);
    if (err < 0)
        return Error_set_str(err, path);

    git_repository_free(repo);
    Py_RETURN_NONE;
};
Esempio n. 17
0
PyObject *
Config_add_file(Config *self, PyObject *args)
{
    int err;
    char *path;
    int priority;

    if (!PyArg_ParseTuple(args, "si", &path, &priority))
        return NULL;

    err = git_config_add_file_ondisk(self->config, path, priority);
    if (err < 0) {
        Error_set_str(err, path);
        return NULL;
    }

    Py_RETURN_NONE;
}
Esempio n. 18
0
PyObject *
discover_repository(PyObject *self, PyObject *args)
{
    const char *path;
    int across_fs = 0;
    const char *ceiling_dirs = NULL;
    char repo_path[MAXPATHLEN];
    int err;

    if (!PyArg_ParseTuple(args, "s|Is", &path, &across_fs, &ceiling_dirs))
        return NULL;

    err = git_repository_discover(repo_path, sizeof(repo_path),
            path, across_fs, ceiling_dirs);
    if (err < 0)
        return Error_set_str(err, path);

    return to_path(repo_path);
};
Esempio n. 19
0
PyObject *
Config_add_file(Config *self, PyObject *args, PyObject *kwds)
{
    char *keywords[] = {"path", "level", "force", NULL};
    int err;
    char *path;
    unsigned int level = 0;
    int force = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|Ii", keywords,
                                     &path, &level, &force))
        return NULL;

    err = git_config_add_file_ondisk(self->config, path, level, force);
    if (err < 0)
        return Error_set_str(err, path);

    Py_RETURN_NONE;
}
Esempio n. 20
0
PyObject *
Repository_status_file(Repository *self, PyObject *value)
{
    char *path;
    unsigned int status;
    int err;

    path = py_path_to_c_str(value);
    if (!path)
        return NULL;

    err = git_status_file(&status, self->repo, path);
    if (err < 0) {
        PyObject *err_obj =  Error_set_str(err, path);
        free(path);
        return err_obj;
    }
    return PyLong_FromLong(status);
}
Esempio n. 21
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;
}
Esempio n. 22
0
int
Index_contains(Index *self, PyObject *value)
{
    char *path;
    int idx;

    path = py_path_to_c_str(value);
    if (!path)
        return -1;
    idx = git_index_find(self->index, path);
    if (idx == GIT_ENOTFOUND)
        return 0;
    if (idx < 0) {
        Error_set_str(idx, path);
        free(path);
        return -1;
    }

    return 1;
}
Esempio n. 23
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;
}
Esempio n. 24
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;
}
Esempio n. 25
0
int
Index_init(Index *self, PyObject *args, PyObject *kwds)
{
    char *path;
    int err;

    if (kwds && PyDict_Size(kwds) > 0) {
        PyErr_SetString(PyExc_TypeError, "Index takes no keyword arguments");
        return -1;
    }

    if (!PyArg_ParseTuple(args, "s", &path))
        return -1;

    err = git_index_open(&self->index, path);
    if (err < 0) {
        Error_set_str(err, path);
        return -1;
    }

    return 0;
}
Esempio n. 26
0
int
Index_contains(Index *self, PyObject *value)
{
    char *path;
    int err;

    path = py_path_to_c_str(value);
    if (!path)
        return -1;
    err = git_index_find(NULL, self->index, path);
    if (err == GIT_ENOTFOUND) {
        free(path);
        return 0;
    }
    if (err < 0) {
        Error_set_str(err, path);
        free(path);
        return -1;
    }
    free(path);
    return 1;
}
Esempio n. 27
0
PyObject *
Repository_lookup_reference(Repository *self, PyObject *py_name)
{
    git_reference *c_reference;
    char *c_name;
    int err;

    /* 1- Get the C name */
    c_name = py_path_to_c_str(py_name);
    if (c_name == NULL)
        return NULL;

    /* 2- Lookup */
    err = git_reference_lookup(&c_reference, self->repo, c_name);
    if (err < 0) {
        PyObject *err_obj = Error_set_str(err, c_name);
        free(c_name);
        return err_obj;
    }
    free(c_name);
    /* 3- Make an instance of Reference and return it */
    return wrap_reference(c_reference);
}