Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b3861b

Browse files
committed
Add enum ObjectType
1 parent 7f3fef8 commit 8b3861b

23 files changed

Lines changed: 130 additions & 89 deletions

pygit2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
DiffStatsFormat,
5555
Feature,
5656
FileMode,
57+
ObjectType,
5758
Option,
5859
MergeAnalysis,
5960
MergePreference,

pygit2/_pygit2.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ from .enums import DiffOption
88
from .enums import DiffStatsFormat
99
from .enums import MergeAnalysis
1010
from .enums import MergePreference
11+
from .enums import ObjectType
1112
from .enums import Option
1213
from .enums import ReferenceFilter
1314
from .enums import ResetMode
1415
from .enums import SortMode
1516

16-
GIT_OBJ_ANY: Literal[-2]
1717
GIT_OBJ_BLOB: Literal[3]
1818
GIT_OBJ_COMMIT: Literal[1]
1919
GIT_OBJ_TAG: Literal[4]
@@ -324,7 +324,7 @@ class Repository:
324324
def create_note(self, message: str, author: Signature, committer: Signature, annotated_id: str, ref: str = "refs/notes/commits", force: bool = False) -> Oid: ...
325325
def create_reference_direct(self, name: str, target: _OidArg, force: bool, message: Optional[str] = None) -> Reference: ...
326326
def create_reference_symbolic(self, name: str, target: str, force: bool, message: Optional[str] = None) -> Reference: ...
327-
def create_tag(self, name: str, oid: _OidArg, type: int, tagger: Signature, message: str) -> Oid: ...
327+
def create_tag(self, name: str, oid: _OidArg, type: ObjectType, tagger: Signature, message: str) -> Oid: ...
328328
def descendant_of(self, oid1: _OidArg, oid2: _OidArg) -> bool: ...
329329
def expand_id(self, hex: str) -> Oid: ...
330330
def free(self) -> None: ...

pygit2/enums.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,32 @@ class MergePreference(IntFlag):
694694
"""
695695

696696

697+
class ObjectType(IntEnum):
698+
ANY = _pygit2.GIT_OBJECT_ANY
699+
"Object can be any of the following"
700+
701+
INVALID = _pygit2.GIT_OBJECT_INVALID
702+
"Object is invalid."
703+
704+
COMMIT = _pygit2.GIT_OBJECT_COMMIT
705+
"A commit object."
706+
707+
TREE = _pygit2.GIT_OBJECT_TREE
708+
"A tree (directory listing) object."
709+
710+
BLOB = _pygit2.GIT_OBJECT_BLOB
711+
"A file revision object."
712+
713+
TAG = _pygit2.GIT_OBJECT_TAG
714+
"An annotated tag object."
715+
716+
OFS_DELTA = _pygit2.GIT_OBJECT_OFS_DELTA
717+
"A delta, base is given by an offset."
718+
719+
REF_DELTA = _pygit2.GIT_OBJECT_REF_DELTA
720+
"A delta, base is given by object id."
721+
722+
697723
class Option(IntEnum):
698724
""" Global libgit2 library options """
699725
# Commented out values --> exists in libgit2 but not supported in pygit2's options.c yet

pygit2/settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
from ssl import get_default_verify_paths
3131

32+
import pygit2.enums
33+
3234
from ._pygit2 import option
3335
from .enums import Option
3436
from .errors import GitError
@@ -136,13 +138,13 @@ def cache_max_size(self, value):
136138
"""
137139
return option(Option.SET_CACHE_MAX_SIZE, value)
138140

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

src/object.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ Object_short_id__get__(Object *self)
132132

133133

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

138137
PyObject *
139138
Object_type__get__(Object *self)
@@ -243,7 +242,7 @@ Object_peel(Object *self, PyObject *py_type)
243242
if (Object__load(self) == NULL) { return NULL; } // Lazy load
244243

245244
otype = py_object_to_otype(py_type);
246-
if (otype == GIT_OBJ_BAD)
245+
if (otype == GIT_OBJECT_INVALID)
247246
return NULL;
248247

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

385384
switch (obj_type) {
386-
case GIT_OBJ_COMMIT:
385+
case GIT_OBJECT_COMMIT:
387386
py_obj = PyObject_New(Object, &CommitType);
388387
break;
389-
case GIT_OBJ_TREE:
388+
case GIT_OBJECT_TREE:
390389
py_obj = PyObject_New(Object, &TreeType);
391390
break;
392-
case GIT_OBJ_BLOB:
391+
case GIT_OBJECT_BLOB:
393392
py_obj = PyObject_New(Object, &BlobType);
394393
break;
395-
case GIT_OBJ_TAG:
394+
case GIT_OBJECT_TAG:
396395
py_obj = PyObject_New(Object, &TagType);
397396
break;
398397
default:

src/odb.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ static git_otype
4141
int_to_loose_object_type(int type_id)
4242
{
4343
switch((git_otype)type_id) {
44-
case GIT_OBJ_COMMIT: return GIT_OBJ_COMMIT;
45-
case GIT_OBJ_TREE: return GIT_OBJ_TREE;
46-
case GIT_OBJ_BLOB: return GIT_OBJ_BLOB;
47-
case GIT_OBJ_TAG: return GIT_OBJ_TAG;
48-
default: return GIT_OBJ_BAD;
44+
case GIT_OBJECT_COMMIT:
45+
case GIT_OBJECT_TREE:
46+
case GIT_OBJECT_BLOB:
47+
case GIT_OBJECT_TAG:
48+
return (git_otype)type_id;
49+
default:
50+
return GIT_OBJECT_INVALID;
4951
}
5052
}
5153

@@ -217,7 +219,7 @@ Odb_write(Odb *self, PyObject *args)
217219
return NULL;
218220

219221
type = int_to_loose_object_type(type_id);
220-
if (type == GIT_OBJ_BAD)
222+
if (type == GIT_OBJECT_INVALID)
221223
return PyErr_Format(PyExc_ValueError, "%d", type_id);
222224

223225
err = git_odb_open_wstream(&stream, self->odb, buflen, type);

src/pygit2.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ hashfile(PyObject *self, PyObject *args)
147147
if (py_path != NULL)
148148
path = PyBytes_AS_STRING(py_path);
149149

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

174-
err = git_odb_hash(&oid, data, size, GIT_OBJ_BLOB);
174+
err = git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB);
175175
if (err < 0) {
176176
return Error_set(err);
177177
}
@@ -479,18 +479,27 @@ PyInit__pygit2(void)
479479
ADD_TYPE(m, TreeBuilder)
480480
ADD_TYPE(m, Blob)
481481
ADD_TYPE(m, Tag)
482-
ADD_CONSTANT_INT(m, GIT_OBJ_ANY)
483-
ADD_CONSTANT_INT(m, GIT_OBJ_COMMIT)
484-
ADD_CONSTANT_INT(m, GIT_OBJ_TREE)
485-
ADD_CONSTANT_INT(m, GIT_OBJ_BLOB)
486-
ADD_CONSTANT_INT(m, GIT_OBJ_TAG)
482+
ADD_CONSTANT_INT(m, GIT_OBJECT_ANY)
483+
ADD_CONSTANT_INT(m, GIT_OBJECT_INVALID)
484+
ADD_CONSTANT_INT(m, GIT_OBJECT_COMMIT)
485+
ADD_CONSTANT_INT(m, GIT_OBJECT_TREE)
486+
ADD_CONSTANT_INT(m, GIT_OBJECT_BLOB)
487+
ADD_CONSTANT_INT(m, GIT_OBJECT_TAG)
488+
ADD_CONSTANT_INT(m, GIT_OBJECT_OFS_DELTA)
489+
ADD_CONSTANT_INT(m, GIT_OBJECT_REF_DELTA)
487490
/* Valid modes for index and tree entries. */
488491
ADD_CONSTANT_INT(m, GIT_FILEMODE_UNREADABLE)
489492
ADD_CONSTANT_INT(m, GIT_FILEMODE_TREE)
490493
ADD_CONSTANT_INT(m, GIT_FILEMODE_BLOB)
491494
ADD_CONSTANT_INT(m, GIT_FILEMODE_BLOB_EXECUTABLE)
492495
ADD_CONSTANT_INT(m, GIT_FILEMODE_LINK)
493496
ADD_CONSTANT_INT(m, GIT_FILEMODE_COMMIT)
497+
/* Deprecated constants for backward compatibility with old Python code*/
498+
ADD_CONSTANT_INT(m, GIT_OBJ_ANY)
499+
ADD_CONSTANT_INT(m, GIT_OBJ_COMMIT)
500+
ADD_CONSTANT_INT(m, GIT_OBJ_TREE)
501+
ADD_CONSTANT_INT(m, GIT_OBJ_BLOB)
502+
ADD_CONSTANT_INT(m, GIT_OBJ_TAG)
494503

495504
/*
496505
* Log

src/reference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ Reference_peel(Reference *self, PyObject *args)
485485
return NULL;
486486

487487
otype = py_object_to_otype(py_type);
488-
if (otype == GIT_OBJ_BAD)
488+
if (otype == GIT_OBJECT_INVALID)
489489
return NULL;
490490

491491
err = git_reference_peel(&obj, self->reference, otype);

src/repository.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Repository_git_object_lookup_prefix(Repository *self, PyObject *key)
297297
if (len == 0)
298298
return NULL;
299299

300-
err = git_object_lookup_prefix(&obj, self->repo, &oid, len, GIT_OBJ_ANY);
300+
err = git_object_lookup_prefix(&obj, self->repo, &oid, len, GIT_OBJECT_ANY);
301301
if (err == 0)
302302
return wrap_object(obj, self, NULL);
303303

@@ -1181,7 +1181,7 @@ Repository_create_commit_with_signature(Repository *self, PyObject *args)
11811181
}
11821182

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

@@ -2032,7 +2032,7 @@ Repository_reset(Repository *self, PyObject* args)
20322032
return NULL;
20332033

20342034
err = git_object_lookup_prefix(&target, self->repo, &oid, len,
2035-
GIT_OBJ_ANY);
2035+
GIT_OBJECT_ANY);
20362036
err = err < 0 ? err : git_reset(self->repo, target, reset_type, NULL);
20372037
git_object_free(target);
20382038
if (err < 0)

src/utils.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ static git_otype
241241
py_type_to_git_type(PyTypeObject *py_type)
242242
{
243243
if (py_type == &CommitType)
244-
return GIT_OBJ_COMMIT;
244+
return GIT_OBJECT_COMMIT;
245245
else if (py_type == &TreeType)
246-
return GIT_OBJ_TREE;
246+
return GIT_OBJECT_TREE;
247247
else if (py_type == &BlobType)
248-
return GIT_OBJ_BLOB;
248+
return GIT_OBJECT_BLOB;
249249
else if (py_type == &TagType)
250-
return GIT_OBJ_TAG;
250+
return GIT_OBJECT_TAG;
251251

252252
PyErr_SetString(PyExc_ValueError, "invalid target type");
253-
return GIT_OBJ_BAD; /* -1 */
253+
return GIT_OBJECT_INVALID; /* -1 */
254254
}
255255

256256
git_otype
@@ -259,12 +259,12 @@ py_object_to_otype(PyObject *py_type)
259259
long value;
260260

261261
if (py_type == Py_None)
262-
return GIT_OBJ_ANY;
262+
return GIT_OBJECT_ANY;
263263

264264
if (PyLong_Check(py_type)) {
265265
value = PyLong_AsLong(py_type);
266266
if (value == -1 && PyErr_Occurred())
267-
return GIT_OBJ_BAD;
267+
return GIT_OBJECT_INVALID;
268268

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

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

0 commit comments

Comments
 (0)