Rename and review py_str_to_git_oid*

This commit is contained in:
J. David Ibáñez
2013-05-11 19:38:17 +02:00
parent d44f7aa9a9
commit 405d5ccdfb
7 changed files with 86 additions and 90 deletions

View File

@@ -352,10 +352,10 @@ Index_read_tree(Index *self, PyObject *value)
git_oid oid; git_oid oid;
git_tree *tree; git_tree *tree;
int err; int err;
Py_ssize_t len; size_t len;
len = py_str_to_git_oid(value, &oid); len = py_oid_to_git_oid(value, &oid);
if (len < 0) if (len == 0)
return NULL; return NULL;
err = git_tree_lookup_prefix(&tree, self->repo->repo, &oid, len); err = git_tree_lookup_prefix(&tree, self->repo->repo, &oid, len);

View File

@@ -45,8 +45,8 @@ git_oid_to_python(const git_oid *oid)
return (PyObject*)py_oid; return (PyObject*)py_oid;
} }
Py_ssize_t size_t
_oid_from_hex(PyObject *py_oid, git_oid *oid) py_hex_to_git_oid (PyObject *py_oid, git_oid *oid)
{ {
PyObject *py_hex; PyObject *py_hex;
int err; int err;
@@ -58,15 +58,15 @@ _oid_from_hex(PyObject *py_oid, git_oid *oid)
if (PyBytes_Check(py_oid)) { if (PyBytes_Check(py_oid)) {
err = PyBytes_AsStringAndSize(py_oid, &hex, &len); err = PyBytes_AsStringAndSize(py_oid, &hex, &len);
if (err) if (err)
return -1; return 0;
err = git_oid_fromstrn(oid, hex, len); err = git_oid_fromstrn(oid, hex, len);
if (err < 0) { if (err < 0) {
PyErr_SetObject(Error_type(err), py_oid); PyErr_SetObject(Error_type(err), py_oid);
return -1; return 0;
} }
return len; return (size_t)len;
} }
#endif #endif
@@ -74,31 +74,31 @@ _oid_from_hex(PyObject *py_oid, git_oid *oid)
if (PyUnicode_Check(py_oid)) { if (PyUnicode_Check(py_oid)) {
py_hex = PyUnicode_AsASCIIString(py_oid); py_hex = PyUnicode_AsASCIIString(py_oid);
if (py_hex == NULL) if (py_hex == NULL)
return -1; return 0;
err = PyBytes_AsStringAndSize(py_hex, &hex, &len); err = PyBytes_AsStringAndSize(py_hex, &hex, &len);
if (err) { if (err) {
Py_DECREF(py_hex); Py_DECREF(py_hex);
return -1; return 0;
} }
err = git_oid_fromstrn(oid, hex, len); err = git_oid_fromstrn(oid, hex, len);
Py_DECREF(py_hex); Py_DECREF(py_hex);
if (err < 0) { if (err < 0) {
PyErr_SetObject(Error_type(err), py_oid); PyErr_SetObject(Error_type(err), py_oid);
return -1; return 0;
} }
return len; return (size_t)len;
} }
/* Type error */ /* Type error */
PyErr_SetObject(PyExc_TypeError, py_oid); PyErr_SetObject(PyExc_TypeError, py_oid);
return -1; return 0;
} }
Py_ssize_t size_t
py_str_to_git_oid(PyObject *py_oid, git_oid *oid) py_oid_to_git_oid(PyObject *py_oid, git_oid *oid)
{ {
/* Oid */ /* Oid */
if (PyObject_TypeCheck(py_oid, (PyTypeObject*)&OidType)) { if (PyObject_TypeCheck(py_oid, (PyTypeObject*)&OidType)) {
@@ -107,41 +107,43 @@ py_str_to_git_oid(PyObject *py_oid, git_oid *oid)
} }
/* Hex */ /* Hex */
return _oid_from_hex(py_oid, oid); return py_hex_to_git_oid(py_oid, oid);
} }
Py_ssize_t int
py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str, git_oid *oid) py_oid_to_git_oid_expand(git_repository *repo, PyObject *py_str, git_oid *oid)
{ {
int err; int err;
Py_ssize_t len; size_t len;
git_odb *odb; git_odb *odb = NULL;
git_odb_object *obj; git_odb_object *obj = NULL;
len = py_str_to_git_oid(py_str, oid); len = py_oid_to_git_oid(py_str, oid);
if (len == 0)
if (len == GIT_OID_HEXSZ || len < 0)
return len;
err = git_repository_odb(&odb, repo);
if (err < 0) {
Error_set(err);
return -1; return -1;
}
if (len == GIT_OID_HEXSZ)
return 0;
/* Short oid */
err = git_repository_odb(&odb, repo);
if (err < 0)
goto error;
err = git_odb_read_prefix(&obj, odb, oid, len); err = git_odb_read_prefix(&obj, odb, oid, len);
if (err < 0) { if (err < 0)
git_odb_free(odb); goto error;
Error_set(err);
return err;
}
git_oid_cpy(oid, git_odb_object_id(obj)); git_oid_cpy(oid, git_odb_object_id(obj));
git_odb_object_free(obj); git_odb_object_free(obj);
git_odb_free(odb); git_odb_free(odb);
return 0; return 0;
error:
git_odb_object_free(obj);
git_odb_free(odb);
Error_set(err);
return -1;
} }
PyObject * PyObject *
@@ -197,8 +199,8 @@ Oid_init(Oid *self, PyObject *args, PyObject *kw)
} }
/* Case 2: hex */ /* Case 2: hex */
len = _oid_from_hex(hex, &self->oid); len = py_hex_to_git_oid(hex, &self->oid);
if (len < 0) if (len == 0)
return -1; return -1;
return 0; return 0;

View File

@@ -32,8 +32,8 @@
#include <Python.h> #include <Python.h>
#include <git2.h> #include <git2.h>
Py_ssize_t py_str_to_git_oid(PyObject *py_str, git_oid *oid); size_t py_oid_to_git_oid(PyObject *py_str, git_oid *oid);
Py_ssize_t py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str, int py_oid_to_git_oid_expand(git_repository *repo, PyObject *py_str,
git_oid *oid); git_oid *oid);
PyObject* git_oid_to_python(const git_oid *oid); PyObject* git_oid_to_python(const git_oid *oid);
PyObject* git_oid_to_py_str(const git_oid *oid); PyObject* git_oid_to_py_str(const git_oid *oid);

View File

@@ -226,7 +226,6 @@ Reference_target__set__(Reference *self, PyObject *py_target)
git_oid oid; git_oid oid;
char *c_name; char *c_name;
int err; int err;
Py_ssize_t len;
git_reference *new_ref; git_reference *new_ref;
git_repository *repo; git_repository *repo;
@@ -235,11 +234,9 @@ Reference_target__set__(Reference *self, PyObject *py_target)
/* Case 1: Direct */ /* Case 1: Direct */
if (GIT_REF_OID == git_reference_type(self->reference)) { if (GIT_REF_OID == git_reference_type(self->reference)) {
repo = git_reference_owner(self->reference); repo = git_reference_owner(self->reference);
len = py_str_to_git_oid_expand(repo, py_target, &oid); err = py_oid_to_git_oid_expand(repo, py_target, &oid);
if (len < 0) { if (err < 0)
err = (int)len; return err;
goto error;
}
err = git_reference_set_target(&new_ref, self->reference, &oid); err = git_reference_set_target(&new_ref, self->reference, &oid);
if (err < 0) if (err < 0)

View File

@@ -254,12 +254,12 @@ PyObject *
Repository_git_object_lookup_prefix(Repository *self, PyObject *key) Repository_git_object_lookup_prefix(Repository *self, PyObject *key)
{ {
int err; int err;
Py_ssize_t len; size_t len;
git_oid oid; git_oid oid;
git_object *obj; git_object *obj;
len = py_str_to_git_oid(key, &oid); len = py_oid_to_git_oid(key, &oid);
if (len < 0) if (len == 0)
return NULL; return NULL;
err = git_object_lookup_prefix(&obj, self->repo, &oid, len, GIT_OBJ_ANY); err = git_object_lookup_prefix(&obj, self->repo, &oid, len, GIT_OBJ_ANY);
@@ -339,11 +339,11 @@ Repository_read(Repository *self, PyObject *py_hex)
{ {
git_oid oid; git_oid oid;
git_odb_object *obj; git_odb_object *obj;
Py_ssize_t len; size_t len;
PyObject* tuple; PyObject* tuple;
len = py_str_to_git_oid(py_hex, &oid); len = py_oid_to_git_oid(py_hex, &oid);
if (len < 0) if (len == 0)
return NULL; return NULL;
obj = Repository_read_raw(self->repo, &oid, len); obj = Repository_read_raw(self->repo, &oid, len);
@@ -522,11 +522,11 @@ Repository_merge_base(Repository *self, PyObject *args)
if (!PyArg_ParseTuple(args, "OO", &value1, &value2)) if (!PyArg_ParseTuple(args, "OO", &value1, &value2))
return NULL; return NULL;
err = py_str_to_git_oid_expand(self->repo, value1, &oid1); err = py_oid_to_git_oid_expand(self->repo, value1, &oid1);
if (err < 0) if (err < 0)
return NULL; return NULL;
err = py_str_to_git_oid_expand(self->repo, value2, &oid2); err = py_oid_to_git_oid_expand(self->repo, value2, &oid2);
if (err < 0) if (err < 0)
return NULL; return NULL;
@@ -548,7 +548,6 @@ Repository_walk(Repository *self, PyObject *args)
PyObject *value; PyObject *value;
unsigned int sort; unsigned int sort;
int err; int err;
Py_ssize_t len;
git_oid oid; git_oid oid;
git_revwalk *walk; git_revwalk *walk;
Walker *py_walker; Walker *py_walker;
@@ -565,10 +564,10 @@ Repository_walk(Repository *self, PyObject *args)
/* Push */ /* Push */
if (value != Py_None) { if (value != Py_None) {
len = py_str_to_git_oid_expand(self->repo, value, &oid); err = py_oid_to_git_oid_expand(self->repo, value, &oid);
if (len < 0) { if (err < 0) {
git_revwalk_free(walk); git_revwalk_free(walk);
return Error_set((int)len); return NULL;
} }
err = git_revwalk_push(walk, &oid); err = git_revwalk_push(walk, &oid);
@@ -683,7 +682,7 @@ Repository_create_commit(Repository *self, PyObject *args)
int parent_count; int parent_count;
git_commit **parents = NULL; git_commit **parents = NULL;
int err = 0, i = 0; int err = 0, i = 0;
Py_ssize_t len; size_t len;
if (!PyArg_ParseTuple(args, "zO!O!OOO!|s", if (!PyArg_ParseTuple(args, "zO!O!OOO!|s",
&update_ref, &update_ref,
@@ -695,8 +694,8 @@ Repository_create_commit(Repository *self, PyObject *args)
&encoding)) &encoding))
return NULL; return NULL;
len = py_str_to_git_oid(py_oid, &oid); len = py_oid_to_git_oid(py_oid, &oid);
if (len < 0) if (len == 0)
goto out; goto out;
message = py_str_to_c_str(py_message, encoding); message = py_str_to_c_str(py_message, encoding);
@@ -717,13 +716,15 @@ Repository_create_commit(Repository *self, PyObject *args)
} }
for (; i < parent_count; i++) { for (; i < parent_count; i++) {
py_parent = PyList_GET_ITEM(py_parents, i); py_parent = PyList_GET_ITEM(py_parents, i);
len = py_str_to_git_oid(py_parent, &oid); len = py_oid_to_git_oid(py_parent, &oid);
if (len < 0) if (len == 0)
goto out; goto out;
if (git_commit_lookup_prefix(&parents[i], self->repo, &oid, err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);
(unsigned int)len)) if (err < 0) {
Error_set(err);
goto out; goto out;
} }
}
err = git_commit_create(&oid, self->repo, update_ref, err = git_commit_create(&oid, self->repo, update_ref,
py_author->signature, py_committer->signature, py_author->signature, py_committer->signature,
@@ -762,7 +763,7 @@ Repository_create_tag(Repository *self, PyObject *args)
git_oid oid; git_oid oid;
git_object *target = NULL; git_object *target = NULL;
int err, target_type; int err, target_type;
Py_ssize_t len; size_t len;
if (!PyArg_ParseTuple(args, "sOiO!s", if (!PyArg_ParseTuple(args, "sOiO!s",
&tag_name, &tag_name,
@@ -772,12 +773,12 @@ Repository_create_tag(Repository *self, PyObject *args)
&message)) &message))
return NULL; return NULL;
len = py_str_to_git_oid(py_oid, &oid); len = py_oid_to_git_oid(py_oid, &oid);
if (len < 0) if (len == 0)
return NULL; return NULL;
err = git_object_lookup_prefix(&target, self->repo, &oid, err = git_object_lookup_prefix(&target, self->repo, &oid, len,
(unsigned int)len, target_type); target_type);
err = err < 0 ? err : git_tag_create(&oid, self->repo, tag_name, target, err = err < 0 ? err : git_tag_create(&oid, self->repo, tag_name, target,
py_tagger->signature, message, 0); py_tagger->signature, message, 0);
git_object_free(target); git_object_free(target);
@@ -885,14 +886,13 @@ Repository_create_reference_direct(Repository *self, PyObject *args,
char *c_name; char *c_name;
git_oid oid; git_oid oid;
int err, force; int err, force;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "sOi", &c_name, &py_obj, &force)) if (!PyArg_ParseTuple(args, "sOi", &c_name, &py_obj, &force))
return NULL; return NULL;
len = py_str_to_git_oid_expand(self->repo, py_obj, &oid); err = py_oid_to_git_oid_expand(self->repo, py_obj, &oid);
if (len < 0) if (err < 0)
return Error_set((int)len); return NULL;
err = git_reference_create(&c_reference, self->repo, c_name, &oid, force); err = git_reference_create(&c_reference, self->repo, c_name, &oid, force);
if (err < 0) if (err < 0)
@@ -1013,7 +1013,6 @@ Repository_TreeBuilder(Repository *self, PyObject *args)
git_tree *tree = NULL; git_tree *tree = NULL;
git_tree *must_free = NULL; git_tree *must_free = NULL;
int err; int err;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "|O", &py_src)) if (!PyArg_ParseTuple(args, "|O", &py_src))
return NULL; return NULL;
@@ -1027,8 +1026,8 @@ Repository_TreeBuilder(Repository *self, PyObject *args)
} }
tree = py_tree->tree; tree = py_tree->tree;
} else { } else {
len = py_str_to_git_oid_expand(self->repo, py_src, &oid); err = py_oid_to_git_oid_expand(self->repo, py_src, &oid);
if (len < 0) if (err < 0)
return NULL; return NULL;
err = git_tree_lookup(&tree, self->repo, &oid); err = git_tree_lookup(&tree, self->repo, &oid);

View File

@@ -53,7 +53,7 @@ PyObject *
TreeBuilder_insert(TreeBuilder *self, PyObject *args) TreeBuilder_insert(TreeBuilder *self, PyObject *args)
{ {
PyObject *py_oid; PyObject *py_oid;
Py_ssize_t len; size_t len;
int err, attr; int err, attr;
git_oid oid; git_oid oid;
const char *fname; const char *fname;
@@ -61,8 +61,8 @@ TreeBuilder_insert(TreeBuilder *self, PyObject *args)
if (!PyArg_ParseTuple(args, "sOi", &fname, &py_oid, &attr)) if (!PyArg_ParseTuple(args, "sOi", &fname, &py_oid, &attr))
return NULL; return NULL;
len = py_str_to_git_oid(py_oid, &oid); len = py_oid_to_git_oid(py_oid, &oid);
if (len < 0) if (len == 0)
return NULL; return NULL;
err = git_treebuilder_insert(NULL, self->bld, fname, &oid, attr); err = git_treebuilder_insert(NULL, self->bld, fname, &oid, attr);

View File

@@ -53,12 +53,11 @@ PyObject *
Walker_hide(Walker *self, PyObject *py_hex) Walker_hide(Walker *self, PyObject *py_hex)
{ {
int err; int err;
Py_ssize_t len;
git_oid oid; git_oid oid;
len = py_str_to_git_oid_expand(self->repo->repo, py_hex, &oid); err = py_oid_to_git_oid_expand(self->repo->repo, py_hex, &oid);
if (len < 0) if (err < 0)
return Error_set((int)len); return NULL;
err = git_revwalk_hide(self->walk, &oid); err = git_revwalk_hide(self->walk, &oid);
if (err < 0) if (err < 0)
@@ -77,12 +76,11 @@ PyObject *
Walker_push(Walker *self, PyObject *py_hex) Walker_push(Walker *self, PyObject *py_hex)
{ {
int err; int err;
Py_ssize_t len;
git_oid oid; git_oid oid;
len = py_str_to_git_oid_expand(self->repo->repo, py_hex, &oid); err = py_oid_to_git_oid_expand(self->repo->repo, py_hex, &oid);
if (len < 0) if (err < 0)
return Error_set((int)len); return NULL;
err = git_revwalk_push(self->walk, &oid); err = git_revwalk_push(self->walk, &oid);
if (err < 0) if (err < 0)