Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand revparse support #1022

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ PyTypeObject BlobType = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)Object_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
Expand Down
2 changes: 1 addition & 1 deletion src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ PyTypeObject CommitType = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)Object_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
Expand Down
10 changes: 9 additions & 1 deletion src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ Object_hash(Object *self)
return ret;
}

PyObject *
Object_repr(Object *self)
{
return PyUnicode_FromFormat("<pygit2.Object{%s:%S}>",
git_object_type2string(Object__type(self)),
Object_hex__get__(self));
}

PyObject *
Object_richcompare(PyObject *o1, PyObject *o2, int op)
{
Expand Down Expand Up @@ -325,7 +333,7 @@ PyTypeObject ObjectType = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)Object_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
Expand Down
1 change: 1 addition & 0 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ PyObject* Object_get_oid(Object *self);
PyObject* Object_get_hex(Object *self);
PyObject* Object_get_type(Object *self);
PyObject* Object_read_raw(Object *self);
PyObject* Object_repr(Object *self);
PyObject* wrap_object(git_object *c_object, Repository *repo, const git_tree_entry *entry);

#endif
9 changes: 9 additions & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern PyTypeObject RefdbType;
extern PyTypeObject RefdbBackendType;
extern PyTypeObject RefdbFsBackendType;
extern PyTypeObject ReferenceType;
extern PyTypeObject RevSpecType;
extern PyTypeObject RefLogIterType;
extern PyTypeObject RefLogEntryType;
extern PyTypeObject BranchType;
Expand Down Expand Up @@ -424,6 +425,14 @@ PyInit__pygit2(void)
ADD_CONSTANT_INT(m, GIT_REF_SYMBOLIC)
ADD_CONSTANT_INT(m, GIT_REF_LISTALL)

/*
* RevSpec
*/
INIT_TYPE(RevSpecType, NULL, NULL)
ADD_CONSTANT_INT(m, GIT_REVPARSE_SINGLE)
ADD_CONSTANT_INT(m, GIT_REVPARSE_RANGE)
ADD_CONSTANT_INT(m, GIT_REVPARSE_MERGE_BASE)

/*
* Worktree
*/
Expand Down
67 changes: 67 additions & 0 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "error.h"
#include "types.h"
#include "reference.h"
#include "revspec.h"
#include "utils.h"
#include "odb.h"
#include "object.h"
Expand Down Expand Up @@ -57,6 +58,7 @@ extern PyTypeObject TreeBuilderType;
extern PyTypeObject ConfigType;
extern PyTypeObject DiffType;
extern PyTypeObject ReferenceType;
extern PyTypeObject RevSpecType;
extern PyTypeObject NoteType;
extern PyTypeObject NoteIterType;

Expand Down Expand Up @@ -364,6 +366,69 @@ Repository_revparse_single(Repository *self, PyObject *py_spec)
}


PyDoc_STRVAR(Repository_revparse_ext__doc__,
"revparse_ext(revision) -> (Object, Reference)\n"
"\n"
"Find a single object and intermediate reference, as specified by a revision\n"
"string. See `man gitrevisions`, or the documentation for `git rev-parse`\n"
"for information on the syntax accepted.\n"
"\n"
"In some cases (@{<-n>} or <branchname>@{upstream}), the expression may\n"
"point to an intermediate reference, which is returned in the second element\n"
"of the result tuple.");

PyObject *
Repository_revparse_ext(Repository *self, PyObject *py_spec)
{
/* Get the C revision spec */
const char *c_spec = pgit_borrow(py_spec);
if (c_spec == NULL)
return NULL;

/* Lookup */
git_object *c_obj = NULL;
git_reference *c_ref = NULL;
int err = git_revparse_ext(&c_obj, &c_ref, self->repo, c_spec);
if (err)
return Error_set_str(err, c_spec);

PyObject *py_obj = wrap_object(c_obj, self, NULL);
PyObject *py_ref = NULL;
if (c_ref != NULL) {
py_ref = wrap_reference(c_ref, self);
} else {
py_ref = Py_None;
Py_INCREF(Py_None);
}
return Py_BuildValue("NN", py_obj, py_ref);
}


PyDoc_STRVAR(Repository_revparse__doc__,
"revparse(revspec) -> RevSpec\n"
"\n"
"Parse a revision string for from, to, and intent. See `man gitrevisions`,\n"
"or the documentation for `git rev-parse` for information on the syntax\n"
"accepted.");

PyObject *
Repository_revparse(Repository *self, PyObject *py_spec)
{
/* Get the C revision spec */
const char *c_spec = pgit_borrow(py_spec);
if (c_spec == NULL)
return NULL;

/* Lookup */
git_revspec revspec;
int err = git_revparse(&revspec, self->repo, c_spec);
if (err) {
return Error_set_str(err, c_spec);
}
return wrap_revspec(&revspec, self);
}


PyDoc_STRVAR(Repository_path__doc__,
"The normalized path to the git repository.");

Expand Down Expand Up @@ -1989,6 +2054,8 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, lookup_reference, METH_O),
METHOD(Repository, lookup_reference_dwim, METH_O),
METHOD(Repository, revparse_single, METH_O),
METHOD(Repository, revparse_ext, METH_O),
METHOD(Repository, revparse, METH_O),
METHOD(Repository, status, METH_NOARGS),
METHOD(Repository, status_file, METH_O),
METHOD(Repository, notes, METH_VARARGS),
Expand Down
160 changes: 160 additions & 0 deletions src/revspec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2010-2020 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "error.h"
#include "object.h"
#include "types.h"
#include "utils.h"

extern PyTypeObject RevSpecType;

PyObject*
wrap_revspec(git_revspec *revspec, Repository *repo)
{
RevSpec *py_revspec;

py_revspec = PyObject_New(RevSpec, &RevSpecType);
if (py_revspec) {
py_revspec->flags = revspec->flags;

if (revspec->from != NULL) {
py_revspec->from = wrap_object(revspec->from, repo, NULL);
} else {
py_revspec->from = NULL;
}

if (revspec->to != NULL) {
py_revspec->to = wrap_object(revspec->to, repo, NULL);
} else {
py_revspec->to = NULL;
}
}

return (PyObject*) py_revspec;
}

PyDoc_STRVAR(RevSpec_from_object__doc__, "From revision");

PyObject *
RevSpec_from_object__get__(RevSpec *self)
{
if (self->from == NULL)
Py_RETURN_NONE;

Py_INCREF(self->from);
return self->from;
}

PyDoc_STRVAR(RevSpec_to_object__doc__, "To revision");

PyObject *
RevSpec_to_object__get__(RevSpec *self)
{
if (self->to == NULL)
Py_RETURN_NONE;

Py_INCREF(self->to);
return self->to;
}

PyDoc_STRVAR(RevSpec_flags__doc__,
"A combination of GIT_REVPARSE_ flags which indicate the\n"
"intended behavior of the spec passed to Repository.revparse()");

PyObject *
RevSpec_flags__get__(RevSpec *self)
{
return PyLong_FromLong(self->flags);
}

static PyObject *
RevSpec_repr(RevSpec *self)
{
return PyUnicode_FromFormat("<pygit2.RevSpec{from=%S,to=%S}>",
(self->from != NULL) ? self->from : Py_None,
(self->to != NULL) ? self->to : Py_None);
}

static void
RevSpec_dealloc(RevSpec *self)
{
Py_XDECREF(self->from);
Py_XDECREF(self->to);
PyObject_Del(self);
}

PyGetSetDef RevSpec_getsetters[] = {
GETTER(RevSpec, from_object),
GETTER(RevSpec, to_object),
GETTER(RevSpec, flags),
{NULL}
};

PyDoc_STRVAR(RevSpec__doc__, "RevSpec object, output from Repository.revparse().");

PyTypeObject RevSpecType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.RevSpec", /* tp_name */
sizeof(RevSpec), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)RevSpec_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)RevSpec_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
RevSpec__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
RevSpec_getsetters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
38 changes: 38 additions & 0 deletions src/revspec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2020 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef INCLUDE_pygit2_revspec_h
#define INCLUDE_pygit2_revspec_h

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <git2.h>
#include "types.h"

PyObject* wrap_revspec(git_revspec *revspec, Repository *repo);

#endif
2 changes: 1 addition & 1 deletion src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ PyTypeObject TreeType = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)Object_repr, /* tp_repr */
&Tree_as_number, /* tp_as_number */
&Tree_as_sequence, /* tp_as_sequence */
&Tree_as_mapping, /* tp_as_mapping */
Expand Down
7 changes: 7 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ typedef struct {
size_t size;
} RefLogIter;

/* git_revspec */
typedef struct {
PyObject_HEAD
PyObject *from;
PyObject *to;
unsigned int flags;
} RevSpec;

/* git_signature */
typedef struct {
Expand Down
Loading