From a8e75e25ffddda67d914b11ff54212baadd95024 Mon Sep 17 00:00:00 2001 From: Edmundo Carmona Antoranz Date: Tue, 10 Jun 2025 19:50:03 +0200 Subject: [PATCH] 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. --- pygit2/repository.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pygit2/repository.py b/pygit2/repository.py index 34712bb3..3c2d4554 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -682,9 +682,15 @@ def merge_file_from_index( ancestor: typing.Union[None, IndexEntry], ours: typing.Union[None, IndexEntry], theirs: typing.Union[None, IndexEntry], - ) -> str: - """Merge files from index. Return a string with the merge result - containing possible conflicts. + return_full: bool = False, + ) -> typing.Union[str, tuple[bool, str, int, typing.Union[str, None]]]: + """Merge files from index. + + Returns: A string with the content of the file containing + possible conflicts. + If return_full then it returns a tuple containing + whether the file is automergeable, the content of the file, + the filemode and the path. ancestor The index entry which will be used as a common @@ -693,6 +699,8 @@ def merge_file_from_index( The index entry to take as "ours" or base. theirs The index entry which will be merged into "ours" + return_full + Whether to return the full output of the low-level call. """ cmergeresult = ffi.new('git_merge_file_result *') @@ -709,10 +717,16 @@ def merge_file_from_index( ) check_error(err) - ret = ffi.string(cmergeresult.ptr, cmergeresult.len).decode('utf-8') + automergeable = cmergeresult.automergeable != 0 + content = ffi.string(cmergeresult.ptr, cmergeresult.len).decode('utf-8') + filemode = cmergeresult.mode + path = cmergeresult.path C.git_merge_file_result_free(cmergeresult) - return ret + if not return_full: + return content + + return automergeable, content, filemode, path def merge_commits( self,