Use byte strings for raw-sha and text for hex-sha

As suggested by Douglas Morrison in issue 43:

  https://github.com/libgit2/pygit2/issues/43

(Unit tests still broken.)
This commit is contained in:
J. David Ibáñez
2011-08-13 23:22:17 +02:00
parent 7950ee1116
commit 81bfabea73

View File

@@ -276,26 +276,37 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
const char *hex_or_bin; const char *hex_or_bin;
int err; int err;
hex_or_bin = PyString_AsString(py_str); /* Case 1: raw sha */
if (hex_or_bin == NULL) { if (PyString_Check(py_str)) {
Error_set_py_obj(GIT_ENOTOID, py_str); hex_or_bin = PyString_AsString(py_str);
return 0; if (hex_or_bin == NULL)
} return 0;
if (PyString_Size(py_str) == 20) {
git_oid_fromraw(oid, (const unsigned char*)hex_or_bin); git_oid_fromraw(oid, (const unsigned char*)hex_or_bin);
err = 0; return 1;
} }
else {
/* Case 2: hex sha */
if (PyUnicode_Check(py_str)) {
py_str = PyUnicode_AsASCIIString(py_str);
if (py_str == NULL)
return 0;
hex_or_bin = PyString_AsString(py_str);
Py_DECREF(py_str);
if (hex_or_bin == NULL)
return 0;
err = git_oid_fromstr(oid, hex_or_bin); err = git_oid_fromstr(oid, hex_or_bin);
if (err < 0) {
Error_set_py_obj(err, py_str);
return 0;
}
return 1;
} }
if (err < 0) { /* Type error */
Error_set_py_obj(err, py_str); PyErr_Format(PyExc_TypeError,
return 0; "Git object id must be byte or a text string, not: %.200s",
} Py_TYPE(py_str)->tp_name);
return 0;
return 1;
} }
static PyObject* static PyObject*