Skip to content

Commit 7e00418

Browse files
committed
index: map git_index_conflict_add
map libgit2's git_index_conflict_add() as Index.add_conflict().
1 parent bc0cf9f commit 7e00418

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

pygit2/decl/index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ void git_index_conflict_iterator_free(
6060
int git_index_conflict_iterator_new(
6161
git_index_conflict_iterator **iterator_out,
6262
git_index *index);
63+
int git_index_conflict_add(
64+
git_index *index,
65+
const git_index_entry *ancestor_entry,
66+
const git_index_entry *our_entry,
67+
const git_index_entry *their_entry);
6368
int git_index_conflict_get(
6469
const git_index_entry **ancestor_out,
6570
const git_index_entry **our_out,

pygit2/index.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,41 @@ def add(self, path_or_entry):
223223

224224
check_error(err, io=True)
225225

226+
def add_conflict(self, ancestor, ours, theirs):
227+
"""
228+
Add or update index entries to represent a conflict. Any staged entries that
229+
exist at the given paths will be removed.
230+
231+
Parameters:
232+
233+
ancestor
234+
ancestor of the conflict
235+
ours
236+
ours side of the conflict
237+
theirs
238+
their side of the conflict
239+
"""
240+
241+
if ancestor and not isinstance(ancestor, IndexEntry):
242+
raise TypeError('ancestor has to be an instance of IndexEntry or None')
243+
if ours and not isinstance(ours, IndexEntry):
244+
raise TypeError('ours has to be an instance of IndexEntry or None')
245+
if theirs and not isinstance(theirs, IndexEntry):
246+
raise TypeError('theirs has to be an instance of IndexEntry or None')
247+
248+
centry_ancestor = centry_ours = centry_theirs = ffi.NULL
249+
if ancestor is not None:
250+
centry_ancestor, _ = ancestor._to_c()
251+
if ours is not None:
252+
centry_ours, _ = ours._to_c()
253+
if theirs is not None:
254+
centry_theirs, _ = theirs._to_c()
255+
err = C.git_index_conflict_add(
256+
self._index, centry_ancestor, centry_ours, centry_theirs
257+
)
258+
259+
check_error(err, io=True)
260+
226261
def diff_to_workdir(
227262
self,
228263
flags: DiffOption = DiffOption.NORMAL,

test/test_index.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import pytest
3131

3232
import pygit2
33-
from pygit2 import Repository, Index, Oid
33+
from pygit2 import Repository, Index, Oid, IndexEntry
3434
from pygit2.enums import FileMode
3535
from . import utils
3636

@@ -311,3 +311,25 @@ def test_create_empty_read_tree_as_string():
311311
def test_create_empty_read_tree(testrepo):
312312
index = Index()
313313
index.read_tree(testrepo['fd937514cb799514d4b81bb24c5fcfeb6472b245'])
314+
315+
316+
def test_add_conflict(testrepo):
317+
ancestor_blob_id = testrepo.create_blob('ancestor')
318+
ancestor = IndexEntry('conflict.txt', ancestor_blob_id, FileMode.BLOB_EXECUTABLE)
319+
320+
ours_blob_id = testrepo.create_blob('ours')
321+
ours = IndexEntry('conflict.txt', ours_blob_id, FileMode.BLOB)
322+
323+
index = Index()
324+
assert index.conflicts is None
325+
326+
index.add_conflict(ancestor, ours, None)
327+
328+
assert index.conflicts is not None
329+
assert 'conflict.txt' in index.conflicts
330+
conflict = index.conflicts['conflict.txt']
331+
assert conflict[0].id == ancestor_blob_id
332+
assert conflict[0].mode == FileMode.BLOB_EXECUTABLE
333+
assert conflict[1].id == ours_blob_id
334+
assert conflict[1].mode == FileMode.BLOB
335+
assert conflict[2] is None

0 commit comments

Comments
 (0)