Skip to content

Commit a60a7bf

Browse files
committed
Repository: get all values from git_merge_file_from_index
In Repository.merge_file_from_index, pygit2 is only returning the resulting content out of the merge, however other values might be relevant like the filemode. Add an optional flag to make it possible to get all the values coming out of git_merge_file_from_index instead of just the file content.
1 parent fdb927e commit a60a7bf

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

pygit2/repository.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,15 @@ def merge_file_from_index(
682682
ancestor: typing.Union[None, IndexEntry],
683683
ours: typing.Union[None, IndexEntry],
684684
theirs: typing.Union[None, IndexEntry],
685-
) -> str:
686-
"""Merge files from index. Return a string with the merge result
687-
containing possible conflicts.
685+
return_full: bool = False,
686+
) -> typing.Union[str, tuple[bool, str, int, typing.Union[str,None]]]:
687+
"""Merge files from index.
688+
689+
Returns: A string with the content of the file containing
690+
possible conflicts.
691+
If return_full then it returns a tuple containing
692+
whether the file is automergeable, the content of the file,
693+
the filemode and the path.
688694
689695
ancestor
690696
The index entry which will be used as a common
@@ -693,6 +699,8 @@ def merge_file_from_index(
693699
The index entry to take as "ours" or base.
694700
theirs
695701
The index entry which will be merged into "ours"
702+
return_full
703+
Whether to return the full output of the low-level call.
696704
"""
697705
cmergeresult = ffi.new('git_merge_file_result *')
698706

@@ -709,10 +717,16 @@ def merge_file_from_index(
709717
)
710718
check_error(err)
711719

712-
ret = ffi.string(cmergeresult.ptr, cmergeresult.len).decode('utf-8')
720+
automergeable = cmergeresult.automergeable != 0
721+
content = ffi.string(cmergeresult.ptr, cmergeresult.len).decode('utf-8')
722+
filemode = cmergeresult.mode
723+
path = cmergeresult.path
713724
C.git_merge_file_result_free(cmergeresult)
714725

715-
return ret
726+
if not return_full:
727+
return content
728+
729+
return automergeable, content, filemode, path
716730

717731
def merge_commits(
718732
self,
@@ -834,6 +848,41 @@ def merge_trees(
834848

835849
return Index.from_c(self, cindex)
836850

851+
def merge_files(self,
852+
ancestor: typing.Union[str, Oid, Blob],
853+
ours: typing.Union[str, Oid, Blob],
854+
theirs: typing.Union[str, Oid, Blob],
855+
opts: MergeFileFlag=MergeFileFlag.DEFAULT):
856+
"""
857+
Merge two files using the common ancestor as the baseline.
858+
859+
Returns: an Index that reflects the result of the merge.
860+
861+
Parameters:
862+
863+
ancestor:
864+
ours:
865+
theirs:
866+
pots:
867+
A combination of enums.MergeFileFlag constants.
868+
:return:
869+
"""
870+
cmergeresult = ffi.new('git_merge_file_result *')
871+
872+
cancestor = ancestor._to_c() if ancestor else ffi.NULL
873+
cours = ours._to_c() if ours else ffi.NULL
874+
ctheirs = theirs._to_c if theirs else ffi.NULL
875+
876+
err = C.git_merge_file(
877+
cmergeresult, cancestor, cours, ctheirs, opts
878+
)
879+
check_error(err)
880+
881+
ret = ffi.string(cmergeresult.ptr, cmergeresult.len).decode('utf-8')
882+
C.git_merge_file_result_free(cmergeresult)
883+
884+
return ret
885+
837886
def merge(
838887
self,
839888
source: typing.Union[Reference, Commit, Oid, str],

0 commit comments

Comments
 (0)