Skip to content

Add repr, str, == and != support to IndexEntry. #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ def hex(self):
"""The id of the referenced object as a hex string"""
return self.id.hex

def __str__(self):
return "<path={} id={} mode={}>".format(self.path, self.hex, self.mode)

def __repr__(self):
t = type(self)
return "<{}.{} path={} id={} mode={}>".format(
t.__module__, t.__qualname__, self.path, self.hex, self.mode
)

def __eq__(self, other):
if self is other:
return True
if not isinstance(other, IndexEntry):
return NotImplemented
return self.path == other.path and self.id == other.id and self.mode == other.mode

def _to_c(self):
"""Convert this entry into the C structure

Expand Down Expand Up @@ -440,3 +456,6 @@ def __next__(self):
theirs = IndexEntry._from_c(ctheirs[0])

return ancestor, ours, theirs

def __iter__(self):
return self
24 changes: 23 additions & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import pytest

import pygit2
from pygit2 import Repository, Index
from pygit2 import Repository, Index, Oid
from . import utils


Expand Down Expand Up @@ -242,6 +242,28 @@ def test_create_entry_aspath(testrepo):
index.add(entry)
index.write_tree()

def test_entry_eq(testrepo):
index = testrepo.index
hello_entry = index['hello.txt']
entry = pygit2.IndexEntry(hello_entry.path, hello_entry.id, hello_entry.mode)
assert hello_entry == entry

entry = pygit2.IndexEntry("README.md", hello_entry.id, hello_entry.mode)
assert hello_entry != entry
oid = Oid(hex='0907563af06c7464d62a70cdd135a6ba7d2b41d8')
entry = pygit2.IndexEntry(hello_entry.path, oid, hello_entry.mode)
assert hello_entry != entry
entry = pygit2.IndexEntry(hello_entry.path, hello_entry.id, pygit2.GIT_FILEMODE_BLOB_EXECUTABLE)
assert hello_entry != entry

def test_entry_repr(testrepo):
index = testrepo.index
hello_entry = index['hello.txt']
assert (repr(hello_entry) ==
"<pygit2.index.IndexEntry path=hello.txt id=a520c24d85fbfc815d385957eed41406ca5a860b mode=33188>")
assert (str(hello_entry) ==
"<path=hello.txt id=a520c24d85fbfc815d385957eed41406ca5a860b mode=33188>")


def test_create_empty():
Index()
Expand Down