index: replace has_conflicts by conflicts returning None

This commit is contained in:
J. David Ibáñez
2014-07-22 10:50:30 +02:00
parent bcce5c13ba
commit 57e25c1a4a
3 changed files with 27 additions and 15 deletions

View File

@@ -99,7 +99,7 @@ Changelog
- New ``Repository.state_cleanup()`` - New ``Repository.state_cleanup()``
`#386 <https://github.com/libgit2/pygit2/pull/386>`_ `#386 <https://github.com/libgit2/pygit2/pull/386>`_
- New ``Index.has_conflicts`` and ``Index.conflicts`` #345 - New ```Index.conflicts``
`#345 <https://github.com/libgit2/pygit2/issues/345>`_ `#345 <https://github.com/libgit2/pygit2/issues/345>`_
`#389 <https://github.com/libgit2/pygit2/pull/389>`_ `#389 <https://github.com/libgit2/pygit2/pull/389>`_

View File

@@ -203,12 +203,6 @@ class Index(object):
check_error(err, True) check_error(err, True)
@property
def has_conflicts(self):
"""Whether this Index contains conflict information
"""
return C.git_index_has_conflicts(self._index) != 0
def diff_to_workdir(self, flags=0, context_lines=3, interhunk_lines=0): def diff_to_workdir(self, flags=0, context_lines=3, interhunk_lines=0):
"""diff_to_workdir(flags=0, context_lines=3, interhunk_lines=0) -> Diff """diff_to_workdir(flags=0, context_lines=3, interhunk_lines=0) -> Diff
@@ -290,11 +284,20 @@ class Index(object):
return Diff.from_c(bytes(ffi.buffer(cdiff)[:]), self._repo) return Diff.from_c(bytes(ffi.buffer(cdiff)[:]), self._repo)
#
# Conflicts
#
_conflicts = None
@property @property
def conflicts(self): def conflicts(self):
"""A collection of conflict information """A collection of conflict information
This presents a mapping interface with the paths as keys. You If there are no conflicts None is returned. Otherwise return an object
that represents the conflicts in the index.
This object presents a mapping interface with the paths as keys. You
can use the ``del`` operator to remove a conflict form the Index. can use the ``del`` operator to remove a conflict form the Index.
Each conflict is made up of three elements. Access or iteration Each conflict is made up of three elements. Access or iteration
@@ -306,7 +309,11 @@ class Index(object):
These elements may be None depending on which sides exist for These elements may be None depending on which sides exist for
the particular conflict. the particular conflict.
""" """
if not hasattr(self, '_conflicts'): if not C.git_index_has_conflicts(self._index):
self._conflicts = None
return None
if self._conflicts is None:
self._conflicts = ConflictCollection(self) self._conflicts = ConflictCollection(self)
return self._conflicts return self._conflicts

View File

@@ -81,7 +81,7 @@ class MergeTestBasic(utils.RepoTestCaseForMerging):
self.assertFalse(analysis & GIT_MERGE_ANALYSIS_FASTFORWARD) self.assertFalse(analysis & GIT_MERGE_ANALYSIS_FASTFORWARD)
self.repo.merge(branch_id) self.repo.merge(branch_id)
self.assertTrue(self.repo.index.has_conflicts) self.assertTrue(self.repo.index.conflicts is not None)
status = pygit2.GIT_STATUS_WT_NEW | pygit2.GIT_STATUS_INDEX_DELETED status = pygit2.GIT_STATUS_WT_NEW | pygit2.GIT_STATUS_INDEX_DELETED
# Asking twice to assure the reference counting is correct # Asking twice to assure the reference counting is correct
self.assertEqual({'.gitignore': status}, self.repo.status()) self.assertEqual({'.gitignore': status}, self.repo.status())
@@ -114,7 +114,7 @@ class MergeTestWithConflicts(utils.RepoTestCaseForMerging):
self.assertFalse(analysis & GIT_MERGE_ANALYSIS_FASTFORWARD) self.assertFalse(analysis & GIT_MERGE_ANALYSIS_FASTFORWARD)
self.repo.merge(branch_id) self.repo.merge(branch_id)
self.assertTrue(self.repo.index.has_conflicts) self.assertTrue(self.repo.index.conflicts is not None)
self.assertRaises(KeyError, self.repo.index.conflicts.__getitem__, 'some-file') self.assertRaises(KeyError, self.repo.index.conflicts.__getitem__, 'some-file')
ancestor, ours, theirs = self.repo.index.conflicts['.gitignore'] ancestor, ours, theirs = self.repo.index.conflicts['.gitignore']
self.assertEqual(None, ancestor) self.assertEqual(None, ancestor)
@@ -126,13 +126,18 @@ class MergeTestWithConflicts(utils.RepoTestCaseForMerging):
# Checking the index works as expected # Checking the index works as expected
self.repo.index.add('.gitignore') self.repo.index.add('.gitignore')
self.repo.index.write() self.repo.index.write()
self.assertRaises(KeyError, self.repo.index.conflicts.__getitem__, '.gitignore') self.assertTrue(self.repo.index.conflicts is None)
def test_merge_remove_conflicts(self): def test_merge_remove_conflicts(self):
other_branch_tip = '1b2bae55ac95a4be3f8983b86cd579226d0eb247' other_branch_tip = '1b2bae55ac95a4be3f8983b86cd579226d0eb247'
self.repo.merge(other_branch_tip) self.repo.merge(other_branch_tip)
idx = self.repo.index idx = self.repo.index
self.assertTrue(idx.has_conflicts) conflicts = idx.conflicts
self.assertRaises(KeyError, idx.conflicts.__delitem__, 'some-file') self.assertTrue(conflicts is not None)
try:
conflicts['.gitignore']
except KeyError:
self.fail("conflicts['.gitignore'] raised KeyError unexpectedly")
del idx.conflicts['.gitignore'] del idx.conflicts['.gitignore']
self.assertFalse(idx.has_conflicts) self.assertRaises(KeyError, conflicts.__getitem__, '.gitignore')
self.assertTrue(idx.conflicts is None)