Skip to content

Commit

Permalink
Add enum ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Dec 29, 2023
1 parent 7f3fef8 commit 8b3861b
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 89 deletions.
1 change: 1 addition & 0 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
DiffStatsFormat,
Feature,
FileMode,
ObjectType,
Option,
MergeAnalysis,
MergePreference,
Expand Down
4 changes: 2 additions & 2 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ from .enums import DiffOption
from .enums import DiffStatsFormat
from .enums import MergeAnalysis
from .enums import MergePreference
from .enums import ObjectType
from .enums import Option
from .enums import ReferenceFilter
from .enums import ResetMode
from .enums import SortMode

GIT_OBJ_ANY: Literal[-2]
GIT_OBJ_BLOB: Literal[3]
GIT_OBJ_COMMIT: Literal[1]
GIT_OBJ_TAG: Literal[4]
Expand Down Expand Up @@ -324,7 +324,7 @@ class Repository:
def create_note(self, message: str, author: Signature, committer: Signature, annotated_id: str, ref: str = "refs/notes/commits", force: bool = False) -> Oid: ...
def create_reference_direct(self, name: str, target: _OidArg, force: bool, message: Optional[str] = None) -> Reference: ...
def create_reference_symbolic(self, name: str, target: str, force: bool, message: Optional[str] = None) -> Reference: ...
def create_tag(self, name: str, oid: _OidArg, type: int, tagger: Signature, message: str) -> Oid: ...
def create_tag(self, name: str, oid: _OidArg, type: ObjectType, tagger: Signature, message: str) -> Oid: ...
def descendant_of(self, oid1: _OidArg, oid2: _OidArg) -> bool: ...
def expand_id(self, hex: str) -> Oid: ...
def free(self) -> None: ...
Expand Down
26 changes: 26 additions & 0 deletions pygit2/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,32 @@ class MergePreference(IntFlag):
"""


class ObjectType(IntEnum):
ANY = _pygit2.GIT_OBJECT_ANY
"Object can be any of the following"

INVALID = _pygit2.GIT_OBJECT_INVALID
"Object is invalid."

COMMIT = _pygit2.GIT_OBJECT_COMMIT
"A commit object."

TREE = _pygit2.GIT_OBJECT_TREE
"A tree (directory listing) object."

BLOB = _pygit2.GIT_OBJECT_BLOB
"A file revision object."

TAG = _pygit2.GIT_OBJECT_TAG
"An annotated tag object."

OFS_DELTA = _pygit2.GIT_OBJECT_OFS_DELTA
"A delta, base is given by an offset."

REF_DELTA = _pygit2.GIT_OBJECT_REF_DELTA
"A delta, base is given by object id."


class Option(IntEnum):
""" Global libgit2 library options """
# Commented out values --> exists in libgit2 but not supported in pygit2's options.c yet
Expand Down
8 changes: 5 additions & 3 deletions pygit2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

from ssl import get_default_verify_paths

import pygit2.enums

from ._pygit2 import option
from .enums import Option
from .errors import GitError
Expand Down Expand Up @@ -136,13 +138,13 @@ def cache_max_size(self, value):
"""
return option(Option.SET_CACHE_MAX_SIZE, value)

def cache_object_limit(self, object_type, value):
def cache_object_limit(self, object_type: pygit2.enums.ObjectType, value):
"""
Set the maximum data size for the given type of object to be
considered eligible for caching in memory. Setting to value to
zero means that that type of object will not be cached.
Defaults to 0 for GIT_OBJECT_BLOB (i.e. won't cache blobs) and 4k
for GIT_OBJECT_COMMIT, GIT_OBJECT_TREE, and GIT_OBJECT_TAG.
Defaults to 0 for enums.ObjectType.BLOB (i.e. won't cache blobs)
and 4k for COMMIT, TREE, and TAG.
"""
return option(Option.SET_CACHE_OBJECT_LIMIT, object_type, value)

Expand Down
13 changes: 6 additions & 7 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ Object_short_id__get__(Object *self)


PyDoc_STRVAR(Object_type__doc__,
"One of the GIT_OBJ_COMMIT, GIT_OBJ_TREE, GIT_OBJ_BLOB or GIT_OBJ_TAG\n"
"constants.");
"One of the enums.ObjectType.COMMIT, TREE, BLOB or TAG constants.");

PyObject *
Object_type__get__(Object *self)
Expand Down Expand Up @@ -243,7 +242,7 @@ Object_peel(Object *self, PyObject *py_type)
if (Object__load(self) == NULL) { return NULL; } // Lazy load

otype = py_object_to_otype(py_type);
if (otype == GIT_OBJ_BAD)
if (otype == GIT_OBJECT_INVALID)
return NULL;

err = git_object_peel(&peeled, self->obj, otype);
Expand Down Expand Up @@ -383,16 +382,16 @@ wrap_object(git_object *c_object, Repository *repo, const git_tree_entry *entry)
git_object_t obj_type = (c_object) ? git_object_type(c_object) : git_tree_entry_type(entry);

switch (obj_type) {
case GIT_OBJ_COMMIT:
case GIT_OBJECT_COMMIT:
py_obj = PyObject_New(Object, &CommitType);
break;
case GIT_OBJ_TREE:
case GIT_OBJECT_TREE:
py_obj = PyObject_New(Object, &TreeType);
break;
case GIT_OBJ_BLOB:
case GIT_OBJECT_BLOB:
py_obj = PyObject_New(Object, &BlobType);
break;
case GIT_OBJ_TAG:
case GIT_OBJECT_TAG:
py_obj = PyObject_New(Object, &TagType);
break;
default:
Expand Down
14 changes: 8 additions & 6 deletions src/odb.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ static git_otype
int_to_loose_object_type(int type_id)
{
switch((git_otype)type_id) {
case GIT_OBJ_COMMIT: return GIT_OBJ_COMMIT;
case GIT_OBJ_TREE: return GIT_OBJ_TREE;
case GIT_OBJ_BLOB: return GIT_OBJ_BLOB;
case GIT_OBJ_TAG: return GIT_OBJ_TAG;
default: return GIT_OBJ_BAD;
case GIT_OBJECT_COMMIT:
case GIT_OBJECT_TREE:
case GIT_OBJECT_BLOB:
case GIT_OBJECT_TAG:
return (git_otype)type_id;
default:
return GIT_OBJECT_INVALID;
}
}

Expand Down Expand Up @@ -217,7 +219,7 @@ Odb_write(Odb *self, PyObject *args)
return NULL;

type = int_to_loose_object_type(type_id);
if (type == GIT_OBJ_BAD)
if (type == GIT_OBJECT_INVALID)
return PyErr_Format(PyExc_ValueError, "%d", type_id);

err = git_odb_open_wstream(&stream, self->odb, buflen, type);
Expand Down
23 changes: 16 additions & 7 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ hashfile(PyObject *self, PyObject *args)
if (py_path != NULL)
path = PyBytes_AS_STRING(py_path);

err = git_odb_hashfile(&oid, path, GIT_OBJ_BLOB);
err = git_odb_hashfile(&oid, path, GIT_OBJECT_BLOB);
Py_XDECREF(py_path);
if (err < 0)
return Error_set(err);
Expand All @@ -171,7 +171,7 @@ hash(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "s#", &data, &size))
return NULL;

err = git_odb_hash(&oid, data, size, GIT_OBJ_BLOB);
err = git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB);
if (err < 0) {
return Error_set(err);
}
Expand Down Expand Up @@ -479,18 +479,27 @@ PyInit__pygit2(void)
ADD_TYPE(m, TreeBuilder)
ADD_TYPE(m, Blob)
ADD_TYPE(m, Tag)
ADD_CONSTANT_INT(m, GIT_OBJ_ANY)
ADD_CONSTANT_INT(m, GIT_OBJ_COMMIT)
ADD_CONSTANT_INT(m, GIT_OBJ_TREE)
ADD_CONSTANT_INT(m, GIT_OBJ_BLOB)
ADD_CONSTANT_INT(m, GIT_OBJ_TAG)
ADD_CONSTANT_INT(m, GIT_OBJECT_ANY)
ADD_CONSTANT_INT(m, GIT_OBJECT_INVALID)
ADD_CONSTANT_INT(m, GIT_OBJECT_COMMIT)
ADD_CONSTANT_INT(m, GIT_OBJECT_TREE)
ADD_CONSTANT_INT(m, GIT_OBJECT_BLOB)
ADD_CONSTANT_INT(m, GIT_OBJECT_TAG)
ADD_CONSTANT_INT(m, GIT_OBJECT_OFS_DELTA)
ADD_CONSTANT_INT(m, GIT_OBJECT_REF_DELTA)
/* Valid modes for index and tree entries. */
ADD_CONSTANT_INT(m, GIT_FILEMODE_UNREADABLE)
ADD_CONSTANT_INT(m, GIT_FILEMODE_TREE)
ADD_CONSTANT_INT(m, GIT_FILEMODE_BLOB)
ADD_CONSTANT_INT(m, GIT_FILEMODE_BLOB_EXECUTABLE)
ADD_CONSTANT_INT(m, GIT_FILEMODE_LINK)
ADD_CONSTANT_INT(m, GIT_FILEMODE_COMMIT)
/* Deprecated constants for backward compatibility with old Python code*/
ADD_CONSTANT_INT(m, GIT_OBJ_ANY)
ADD_CONSTANT_INT(m, GIT_OBJ_COMMIT)
ADD_CONSTANT_INT(m, GIT_OBJ_TREE)
ADD_CONSTANT_INT(m, GIT_OBJ_BLOB)
ADD_CONSTANT_INT(m, GIT_OBJ_TAG)

/*
* Log
Expand Down
2 changes: 1 addition & 1 deletion src/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ Reference_peel(Reference *self, PyObject *args)
return NULL;

otype = py_object_to_otype(py_type);
if (otype == GIT_OBJ_BAD)
if (otype == GIT_OBJECT_INVALID)
return NULL;

err = git_reference_peel(&obj, self->reference, otype);
Expand Down
6 changes: 3 additions & 3 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Repository_git_object_lookup_prefix(Repository *self, PyObject *key)
if (len == 0)
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_OBJECT_ANY);
if (err == 0)
return wrap_object(obj, self, NULL);

Expand Down Expand Up @@ -1181,7 +1181,7 @@ Repository_create_commit_with_signature(Repository *self, PyObject *args)
}

PyDoc_STRVAR(Repository_create_tag__doc__,
"create_tag(name: str, oid: Oid, type: int, tagger: Signature[, message: str]) -> Oid\n"
"create_tag(name: str, oid: Oid, type: enums.ObjectType, tagger: Signature[, message: str]) -> Oid\n"
"\n"
"Create a new tag object, return its oid.");

Expand Down Expand Up @@ -2032,7 +2032,7 @@ Repository_reset(Repository *self, PyObject* args)
return NULL;

err = git_object_lookup_prefix(&target, self->repo, &oid, len,
GIT_OBJ_ANY);
GIT_OBJECT_ANY);
err = err < 0 ? err : git_reset(self->repo, target, reset_type, NULL);
git_object_free(target);
if (err < 0)
Expand Down
16 changes: 8 additions & 8 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ static git_otype
py_type_to_git_type(PyTypeObject *py_type)
{
if (py_type == &CommitType)
return GIT_OBJ_COMMIT;
return GIT_OBJECT_COMMIT;
else if (py_type == &TreeType)
return GIT_OBJ_TREE;
return GIT_OBJECT_TREE;
else if (py_type == &BlobType)
return GIT_OBJ_BLOB;
return GIT_OBJECT_BLOB;
else if (py_type == &TagType)
return GIT_OBJ_TAG;
return GIT_OBJECT_TAG;

PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJ_BAD; /* -1 */
return GIT_OBJECT_INVALID; /* -1 */
}

git_otype
Expand All @@ -259,12 +259,12 @@ py_object_to_otype(PyObject *py_type)
long value;

if (py_type == Py_None)
return GIT_OBJ_ANY;
return GIT_OBJECT_ANY;

if (PyLong_Check(py_type)) {
value = PyLong_AsLong(py_type);
if (value == -1 && PyErr_Occurred())
return GIT_OBJ_BAD;
return GIT_OBJECT_INVALID;

/* TODO Check whether the value is a valid value */
return (git_otype)value;
Expand All @@ -274,5 +274,5 @@ py_object_to_otype(PyObject *py_type)
return py_type_to_git_type((PyTypeObject *) py_type);

PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJ_BAD; /* -1 */
return GIT_OBJECT_INVALID; /* -1 */
}
11 changes: 6 additions & 5 deletions test/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import pytest

import pygit2
from pygit2 import ObjectType
from . import utils


Expand Down Expand Up @@ -86,7 +87,7 @@ def test_read_blob(testrepo):
assert sha == BLOB_SHA
assert isinstance(blob, pygit2.Blob)
assert not blob.is_binary
assert pygit2.GIT_OBJ_BLOB == blob.type
assert ObjectType.BLOB == blob.type
assert BLOB_CONTENT == blob.data
assert len(BLOB_CONTENT) == blob.size
assert BLOB_CONTENT == blob.read_raw()
Expand All @@ -96,7 +97,7 @@ def test_create_blob(testrepo):
blob = testrepo[blob_oid]

assert isinstance(blob, pygit2.Blob)
assert pygit2.GIT_OBJ_BLOB == blob.type
assert ObjectType.BLOB == blob.type

assert blob_oid == blob.id
assert utils.gen_blob_sha1(BLOB_NEW_CONTENT) == blob_oid.hex
Expand All @@ -117,7 +118,7 @@ def test_create_blob_fromworkdir(testrepo):
blob = testrepo[blob_oid]

assert isinstance(blob, pygit2.Blob)
assert pygit2.GIT_OBJ_BLOB == blob.type
assert ObjectType.BLOB == blob.type

assert blob_oid == blob.id
assert utils.gen_blob_sha1(BLOB_FILE_CONTENT) == blob_oid.hex
Expand All @@ -142,7 +143,7 @@ def test_create_blob_fromdisk(testrepo):
blob = testrepo[blob_oid]

assert isinstance(blob, pygit2.Blob)
assert pygit2.GIT_OBJ_BLOB == blob.type
assert ObjectType.BLOB == blob.type

def test_create_blob_fromiobase(testrepo):
with pytest.raises(TypeError):
Expand All @@ -153,7 +154,7 @@ def test_create_blob_fromiobase(testrepo):
blob = testrepo[blob_oid]

assert isinstance(blob, pygit2.Blob)
assert pygit2.GIT_OBJ_BLOB == blob.type
assert ObjectType.BLOB == blob.type

assert blob_oid == blob.id
assert BLOB_SHA == blob_oid.hex
Expand Down
Loading

0 comments on commit 8b3861b

Please sign in to comment.