Skip to content

Commit

Permalink
Add enum RevSpecFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Nov 22, 2023
1 parent b6f0af8 commit 983fef5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/revparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ You can use any of the fancy `<rev>` forms supported by libgit2::

Constants:

.. py:data:: GIT_REVPARSE_SINGLE
.. py:data:: GIT_REVPARSE_RANGE
.. py:data:: GIT_REVPARSE_MERGE_BASE
.. py:data:: RevparseFlags.SINGLE
.. py:data:: RevparseFlags.RANGE
.. py:data:: RevparseFlags.MERGE_BASE
1 change: 1 addition & 0 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
RepositoryOpenFlags,
RepositoryState,
ResetType,
RevSpecFlags,
StashApplyProgress,
StatusFlags,
SubmoduleIgnore,
Expand Down
3 changes: 0 additions & 3 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ GIT_OID_HEXSZ: int
GIT_OID_HEX_ZERO: str
GIT_OID_MINPREFIXLEN: int
GIT_OID_RAWSZ: int
GIT_REVPARSE_MERGE_BASE: int
GIT_REVPARSE_RANGE: int
GIT_REVPARSE_SINGLE: int
LIBGIT2_VERSION: str
LIBGIT2_VER_MAJOR: int
LIBGIT2_VER_MINOR: int
Expand Down
16 changes: 16 additions & 0 deletions pygit2/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,22 @@ class ResetType(IntEnum):
"MIXED plus changes in working tree discarded"


class RevSpecFlags(IntFlag):
"""
Revparse flags.
These indicate the intended behavior of the spec passed to Repository.revparse()
"""

SINGLE = _pygit2.GIT_REVSPEC_SINGLE
"The spec targeted a single object."

RANGE = _pygit2.GIT_REVSPEC_RANGE
"The spec targeted a range of commits."

MERGE_BASE = _pygit2.GIT_REVSPEC_MERGE_BASE
"The spec used the '...' operator, which invokes special semantics."


class StatusFlags(IntFlag):
"""
Status flags for a single file.
Expand Down
4 changes: 4 additions & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ PyInit__pygit2(void)
*/
INIT_TYPE(RevSpecType, NULL, NULL)
ADD_TYPE(m, RevSpec)
ADD_CONSTANT_INT(m, GIT_REVSPEC_SINGLE)
ADD_CONSTANT_INT(m, GIT_REVSPEC_RANGE)
ADD_CONSTANT_INT(m, GIT_REVSPEC_MERGE_BASE)
/* Deprecated enums */
ADD_CONSTANT_INT(m, GIT_REVPARSE_SINGLE)
ADD_CONSTANT_INT(m, GIT_REVPARSE_RANGE)
ADD_CONSTANT_INT(m, GIT_REVPARSE_MERGE_BASE)
Expand Down
2 changes: 1 addition & 1 deletion src/revspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ RevSpec_to_object__get__(RevSpec *self)
}

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

PyObject *
Expand Down
8 changes: 4 additions & 4 deletions test/test_revparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

"""Tests for revision parsing."""

from pygit2 import GIT_REVPARSE_SINGLE, GIT_REVPARSE_RANGE, GIT_REVPARSE_MERGE_BASE, InvalidSpecError
from pygit2 import RevSpecFlags, InvalidSpecError
from pytest import raises

HEAD_SHA = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
Expand Down Expand Up @@ -61,21 +61,21 @@ def test_revparse_1(testrepo):
s = testrepo.revparse('master')
assert s.from_object.hex == HEAD_SHA
assert s.to_object is None
assert s.flags == GIT_REVPARSE_SINGLE
assert s.flags == RevSpecFlags.SINGLE


def test_revparse_range_1(testrepo):
s = testrepo.revparse('HEAD^1..acecd5e')
assert s.from_object.hex == PARENT_SHA
assert s.to_object.hex.startswith('acecd5e')
assert s.flags == GIT_REVPARSE_RANGE
assert s.flags == RevSpecFlags.RANGE


def test_revparse_range_2(testrepo):
s = testrepo.revparse('HEAD...i18n')
assert s.from_object.hex.startswith('2be5719')
assert s.to_object.hex.startswith('5470a67')
assert s.flags == GIT_REVPARSE_RANGE | GIT_REVPARSE_MERGE_BASE
assert s.flags == RevSpecFlags.RANGE | RevSpecFlags.MERGE_BASE
assert testrepo.merge_base(s.from_object.id, s.to_object.id) is not None


Expand Down

0 comments on commit 983fef5

Please sign in to comment.