Skip to content

Commit

Permalink
Add enums DiffFlag, DeltaStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Dec 29, 2023
1 parent 1a190d4 commit 7f3fef8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
2 changes: 2 additions & 0 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
CheckoutNotify,
CheckoutStrategy,
ConfigLevel,
DeltaStatus,
DescribeStrategy,
DiffFlag,
DiffOption,
DiffStatsFormat,
Feature,
Expand Down
15 changes: 0 additions & 15 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ from .enums import ReferenceFilter
from .enums import ResetMode
from .enums import SortMode

GIT_DELTA_ADDED: int
GIT_DELTA_CONFLICTED: int
GIT_DELTA_COPIED: int
GIT_DELTA_DELETED: int
GIT_DELTA_IGNORED: int
GIT_DELTA_MODIFIED: int
GIT_DELTA_RENAMED: int
GIT_DELTA_TYPECHANGE: int
GIT_DELTA_UNMODIFIED: int
GIT_DELTA_UNREADABLE: int
GIT_DELTA_UNTRACKED: int
GIT_DIFF_FLAG_BINARY: int
GIT_DIFF_FLAG_EXISTS: int
GIT_DIFF_FLAG_NOT_BINARY: int
GIT_DIFF_FLAG_VALID_ID: int
GIT_OBJ_ANY: Literal[-2]
GIT_OBJ_BLOB: Literal[3]
GIT_OBJ_COMMIT: Literal[1]
Expand Down
72 changes: 72 additions & 0 deletions pygit2/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,52 @@ class ConfigLevel(IntEnum):
specific config file available that actually is loaded)"""


class DeltaStatus(IntEnum):
"""
What type of change is described by a DiffDelta?
`RENAMED` and `COPIED` will only show up if you run
`find_similar()` on the Diff object.
`TYPECHANGE` only shows up given `INCLUDE_TYPECHANGE`
in the DiffOption option flags (otherwise type changes
will be split into ADDED / DELETED pairs).
"""

UNMODIFIED = _pygit2.GIT_DELTA_UNMODIFIED
"no changes"

ADDED = _pygit2.GIT_DELTA_ADDED
"entry does not exist in old version"

DELETED = _pygit2.GIT_DELTA_DELETED
"entry does not exist in new version"

MODIFIED = _pygit2.GIT_DELTA_MODIFIED
"entry content changed between old and new"

RENAMED = _pygit2.GIT_DELTA_RENAMED
"entry was renamed between old and new"

COPIED = _pygit2.GIT_DELTA_COPIED
"entry was copied from another old entry"

IGNORED = _pygit2.GIT_DELTA_IGNORED
"entry is ignored item in workdir"

UNTRACKED = _pygit2.GIT_DELTA_UNTRACKED
"entry is untracked item in workdir"

TYPECHANGE = _pygit2.GIT_DELTA_TYPECHANGE
"type of entry changed between old and new"

UNREADABLE = _pygit2.GIT_DELTA_UNREADABLE
"entry is unreadable"

CONFLICTED = _pygit2.GIT_DELTA_CONFLICTED
"entry in the index is conflicted"


class DescribeStrategy(IntEnum):
"""
Reference lookup strategy.
Expand Down Expand Up @@ -338,6 +384,32 @@ class DiffFind(IntFlag):
"""


class DiffFlag(IntFlag):
"""
Flags for the delta object and the file objects on each side.
These flags are used for both the `flags` value of the `DiffDelta`
and the flags for the `DiffFile` objects representing the old and
new sides of the delta. Values outside of this public range should be
considered reserved for internal or future use.
"""

BINARY = _pygit2.GIT_DIFF_FLAG_BINARY
"file(s) treated as binary data"

NOT_BINARY = _pygit2.GIT_DIFF_FLAG_NOT_BINARY
"file(s) treated as text data"

VALID_ID = _pygit2.GIT_DIFF_FLAG_VALID_ID
"`id` value is known correct"

EXISTS = _pygit2.GIT_DIFF_FLAG_EXISTS
"file exists at this side of the delta"

VALID_SIZE = _pygit2.GIT_DIFF_FLAG_VALID_SIZE
"file size value is known correct"


class DiffOption(IntFlag):
"""
Flags for diff options. A combination of these flags can be passed
Expand Down
1 change: 1 addition & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ PyInit__pygit2(void)
ADD_CONSTANT_INT(m, GIT_DIFF_FLAG_NOT_BINARY)
ADD_CONSTANT_INT(m, GIT_DIFF_FLAG_VALID_ID)
ADD_CONSTANT_INT(m, GIT_DIFF_FLAG_EXISTS)
ADD_CONSTANT_INT(m, GIT_DIFF_FLAG_VALID_SIZE)

/* DiffDelta.status (git_delta_t in libgit2) */
ADD_CONSTANT_INT(m, GIT_DELTA_UNMODIFIED)
Expand Down
7 changes: 3 additions & 4 deletions test/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import pytest

import pygit2
from pygit2 import DiffOption, DiffStatsFormat
from pygit2 import GIT_DELTA_RENAMED
from pygit2 import DeltaStatus, DiffOption, DiffStatsFormat


COMMIT_SHA1_1 = '5fe808e8953c12735680c257f56600cb0de44b10'
Expand Down Expand Up @@ -285,10 +284,10 @@ def test_find_similar(barerepo):
#~ Must pass INCLUDE_UNMODIFIED if you expect to emulate
#~ --find-copies-harder during rename transformion...
diff = commit_a.tree.diff_to_tree(commit_b.tree, DiffOption.INCLUDE_UNMODIFIED)
assert all(x.delta.status != GIT_DELTA_RENAMED for x in diff)
assert all(x.delta.status != DeltaStatus.RENAMED for x in diff)
assert all(x.delta.status_char() != 'R' for x in diff)
diff.find_similar()
assert any(x.delta.status == GIT_DELTA_RENAMED for x in diff)
assert any(x.delta.status == DeltaStatus.RENAMED for x in diff)
assert any(x.delta.status_char() == 'R' for x in diff)

def test_diff_stats(barerepo):
Expand Down

0 comments on commit 7f3fef8

Please sign in to comment.