Skip to content

index: map git_index_conflict_add #1382

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions pygit2/decl/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void git_index_conflict_iterator_free(
int git_index_conflict_iterator_new(
git_index_conflict_iterator **iterator_out,
git_index *index);
int git_index_conflict_add(
git_index *index,
const git_index_entry *ancestor_entry,
const git_index_entry *our_entry,
const git_index_entry *their_entry);
int git_index_conflict_get(
const git_index_entry **ancestor_out,
const git_index_entry **our_out,
Expand Down
35 changes: 35 additions & 0 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ def add(self, path_or_entry):

check_error(err, io=True)

def add_conflict(self, ancestor, ours, theirs):
"""
Add or update index entries to represent a conflict. Any staged entries that
exist at the given paths will be removed.

Parameters:

ancestor
ancestor of the conflict
ours
ours side of the conflict
theirs
their side of the conflict
"""

if ancestor and not isinstance(ancestor, IndexEntry):
raise TypeError('ancestor has to be an instance of IndexEntry or None')
if ours and not isinstance(ours, IndexEntry):
raise TypeError('ours has to be an instance of IndexEntry or None')
if theirs and not isinstance(theirs, IndexEntry):
raise TypeError('theirs has to be an instance of IndexEntry or None')

centry_ancestor = centry_ours = centry_theirs = ffi.NULL
if ancestor is not None:
centry_ancestor, _ = ancestor._to_c()
if ours is not None:
centry_ours, _ = ours._to_c()
if theirs is not None:
centry_theirs, _ = theirs._to_c()
err = C.git_index_conflict_add(
self._index, centry_ancestor, centry_ours, centry_theirs
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something to do with the centries after using them in git_index_conflict_add()?

check_error(err, io=True)

def diff_to_workdir(
self,
flags: DiffOption = DiffOption.NORMAL,
Expand Down
24 changes: 23 additions & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import pytest

import pygit2
from pygit2 import Repository, Index, Oid
from pygit2 import Repository, Index, Oid, IndexEntry
from pygit2.enums import FileMode
from . import utils

Expand Down Expand Up @@ -311,3 +311,25 @@ def test_create_empty_read_tree_as_string():
def test_create_empty_read_tree(testrepo):
index = Index()
index.read_tree(testrepo['fd937514cb799514d4b81bb24c5fcfeb6472b245'])


def test_add_conflict(testrepo):
ancestor_blob_id = testrepo.create_blob('ancestor')
ancestor = IndexEntry('conflict.txt', ancestor_blob_id, FileMode.BLOB_EXECUTABLE)

ours_blob_id = testrepo.create_blob('ours')
ours = IndexEntry('conflict.txt', ours_blob_id, FileMode.BLOB)

index = Index()
assert index.conflicts is None

index.add_conflict(ancestor, ours, None)

assert index.conflicts is not None
assert 'conflict.txt' in index.conflicts
conflict = index.conflicts['conflict.txt']
assert conflict[0].id == ancestor_blob_id
assert conflict[0].mode == FileMode.BLOB_EXECUTABLE
assert conflict[1].id == ours_blob_id
assert conflict[1].mode == FileMode.BLOB
assert conflict[2] is None
Loading