Skip to content

Commit 28b4342

Browse files
committed
Repository.merge_file_from_index() returns MergeFileResult by default
The previous string-returning behavior is still available with `use_deprecated=True`, but is deprecated.
1 parent 5e1b040 commit 28b4342

4 files changed

Lines changed: 69 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Breaking changes:
1111
- Remove deprecated support for passing `str` to `Repository.merge(...)`,
1212
pass a `Commit`, `Oid`, or `Reference` object instead
1313

14+
- `Repository.merge_file_from_index(...)` now returns `MergeFileResult`
15+
by default. The previous string-returning behavior is still available
16+
with `use_deprecated=True`, but is deprecated.
17+
1418

1519
# 1.19.3 (2026-06-13)
1620

pygit2/repository.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,13 @@ def merge_file_from_index(
813813
ancestor: 'IndexEntry | None',
814814
ours: 'IndexEntry | None',
815815
theirs: 'IndexEntry | None',
816-
use_deprecated: bool = True,
817-
) -> 'str | MergeFileResult | None':
816+
use_deprecated: bool = False,
817+
) -> 'MergeFileResult | str | None':
818818
"""Merge files from index.
819819
820-
Returns: A string with the content of the file containing
821-
possible conflicts if use_deprecated==True.
822-
If use_deprecated==False then it returns an instance of MergeFileResult.
820+
Returns: An instance of MergeFileResult by default.
821+
If use_deprecated==True then it returns a string with the content of
822+
the file containing possible conflicts.
823823
824824
ancestor
825825
The index entry which will be used as a common
@@ -829,9 +829,9 @@ def merge_file_from_index(
829829
theirs
830830
The index entry which will be merged into "ours"
831831
use_deprecated
832-
This controls what will be returned. If use_deprecated==True (default),
833-
a string with the contents of the file will be returned.
834-
An instance of MergeFileResult will be returned otherwise.
832+
This controls what will be returned. If use_deprecated==False (default),
833+
an instance of MergeFileResult will be returned.
834+
A string with the contents of the file will be returned otherwise.
835835
"""
836836
cmergeresult = ffi.new('git_merge_file_result *')
837837

test/test_repository.py

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,46 +1073,63 @@ def get_hello_txt_from_repo() -> str:
10731073
assert isinstance(blob, Blob)
10741074
return blob.data.decode()
10751075

1076-
# no change
1077-
res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_txt)
1078-
assert res == get_hello_txt_from_repo()
1076+
with pytest.warns(DeprecationWarning, match='Getting an str'):
1077+
# no change
1078+
res = testrepo.merge_file_from_index(
1079+
hello_txt, hello_txt, hello_txt, use_deprecated=True
1080+
)
1081+
assert res == get_hello_txt_from_repo()
10791082

1080-
# executable switch on ours
1081-
res = testrepo.merge_file_from_index(hello_txt, hello_txt_executable, hello_txt)
1082-
assert res == get_hello_txt_from_repo()
1083+
# executable switch on ours
1084+
res = testrepo.merge_file_from_index(
1085+
hello_txt, hello_txt_executable, hello_txt, use_deprecated=True
1086+
)
1087+
assert res == get_hello_txt_from_repo()
10831088

1084-
# executable switch on theirs
1085-
res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_txt_executable)
1086-
assert res == get_hello_txt_from_repo()
1089+
# executable switch on theirs
1090+
res = testrepo.merge_file_from_index(
1091+
hello_txt, hello_txt, hello_txt_executable, use_deprecated=True
1092+
)
1093+
assert res == get_hello_txt_from_repo()
10871094

1088-
# executable switch on both
1089-
res = testrepo.merge_file_from_index(
1090-
hello_txt, hello_txt_executable, hello_txt_executable
1091-
)
1092-
assert res == get_hello_txt_from_repo()
1095+
# executable switch on both
1096+
res = testrepo.merge_file_from_index(
1097+
hello_txt, hello_txt_executable, hello_txt_executable, use_deprecated=True
1098+
)
1099+
assert res == get_hello_txt_from_repo()
10931100

1094-
# path switch on ours
1095-
res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_txt)
1096-
assert res == get_hello_txt_from_repo()
1101+
# path switch on ours
1102+
res = testrepo.merge_file_from_index(
1103+
hello_txt, hello_world, hello_txt, use_deprecated=True
1104+
)
1105+
assert res == get_hello_txt_from_repo()
10971106

1098-
# path switch on theirs
1099-
res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_world)
1100-
assert res == get_hello_txt_from_repo()
1107+
# path switch on theirs
1108+
res = testrepo.merge_file_from_index(
1109+
hello_txt, hello_txt, hello_world, use_deprecated=True
1110+
)
1111+
assert res == get_hello_txt_from_repo()
11011112

1102-
# path switch on both
1103-
res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_world)
1104-
assert res == get_hello_txt_from_repo()
1113+
# path switch on both
1114+
res = testrepo.merge_file_from_index(
1115+
hello_txt, hello_world, hello_world, use_deprecated=True
1116+
)
1117+
assert res == get_hello_txt_from_repo()
11051118

1106-
# path switch on ours, executable flag switch on theirs
1107-
res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_txt_executable)
1108-
assert res == get_hello_txt_from_repo()
1119+
# path switch on ours, executable flag switch on theirs
1120+
res = testrepo.merge_file_from_index(
1121+
hello_txt, hello_world, hello_txt_executable, use_deprecated=True
1122+
)
1123+
assert res == get_hello_txt_from_repo()
11091124

1110-
# path switch on theirs, executable flag switch on ours
1111-
res = testrepo.merge_file_from_index(hello_txt, hello_txt_executable, hello_world)
1112-
assert res == get_hello_txt_from_repo()
1125+
# path switch on theirs, executable flag switch on ours
1126+
res = testrepo.merge_file_from_index(
1127+
hello_txt, hello_txt_executable, hello_world, use_deprecated=True
1128+
)
1129+
assert res == get_hello_txt_from_repo()
11131130

11141131

1115-
def test_merge_file_from_index_non_deprecated(testrepo: Repository) -> None:
1132+
def test_merge_file_from_index(testrepo: Repository) -> None:
11161133
hello_txt = testrepo.index['hello.txt']
11171134
hello_txt_executable = IndexEntry(
11181135
hello_txt.path, hello_txt.id, FileMode.BLOB_EXECUTABLE
@@ -1126,15 +1143,15 @@ def get_hello_txt_from_repo() -> str:
11261143

11271144
# no change
11281145
res = testrepo.merge_file_from_index(
1129-
hello_txt, hello_txt, hello_txt, use_deprecated=False
1146+
hello_txt, hello_txt, hello_txt
11301147
)
11311148
assert res == MergeFileResult(
11321149
True, hello_txt.path, hello_txt.mode, get_hello_txt_from_repo()
11331150
)
11341151

11351152
# executable switch on ours
11361153
res = testrepo.merge_file_from_index(
1137-
hello_txt, hello_txt_executable, hello_txt, use_deprecated=False
1154+
hello_txt, hello_txt_executable, hello_txt
11381155
)
11391156
assert res == MergeFileResult(
11401157
True,
@@ -1145,7 +1162,7 @@ def get_hello_txt_from_repo() -> str:
11451162

11461163
# executable switch on theirs
11471164
res = testrepo.merge_file_from_index(
1148-
hello_txt, hello_txt, hello_txt_executable, use_deprecated=False
1165+
hello_txt, hello_txt, hello_txt_executable
11491166
)
11501167
assert res == MergeFileResult(
11511168
True,
@@ -1156,7 +1173,7 @@ def get_hello_txt_from_repo() -> str:
11561173

11571174
# executable switch on both
11581175
res = testrepo.merge_file_from_index(
1159-
hello_txt, hello_txt_executable, hello_txt_executable, use_deprecated=False
1176+
hello_txt, hello_txt_executable, hello_txt_executable
11601177
)
11611178
assert res == MergeFileResult(
11621179
True,
@@ -1167,29 +1184,29 @@ def get_hello_txt_from_repo() -> str:
11671184

11681185
# path switch on ours
11691186
res = testrepo.merge_file_from_index(
1170-
hello_txt, hello_world, hello_txt, use_deprecated=False
1187+
hello_txt, hello_world, hello_txt
11711188
)
11721189
assert res == MergeFileResult(
11731190
True, hello_world.path, hello_txt.mode, get_hello_txt_from_repo()
11741191
)
11751192

11761193
# path switch on theirs
11771194
res = testrepo.merge_file_from_index(
1178-
hello_txt, hello_txt, hello_world, use_deprecated=False
1195+
hello_txt, hello_txt, hello_world
11791196
)
11801197
assert res == MergeFileResult(
11811198
True, hello_world.path, hello_txt.mode, get_hello_txt_from_repo()
11821199
)
11831200

11841201
# path switch on both
11851202
res = testrepo.merge_file_from_index(
1186-
hello_txt, hello_world, hello_world, use_deprecated=False
1203+
hello_txt, hello_world, hello_world
11871204
)
11881205
assert res == MergeFileResult(True, None, hello_txt.mode, get_hello_txt_from_repo())
11891206

11901207
# path switch on ours, executable flag switch on theirs
11911208
res = testrepo.merge_file_from_index(
1192-
hello_txt, hello_world, hello_txt_executable, use_deprecated=False
1209+
hello_txt, hello_world, hello_txt_executable
11931210
)
11941211
assert res == MergeFileResult(
11951212
True,
@@ -1200,7 +1217,7 @@ def get_hello_txt_from_repo() -> str:
12001217

12011218
# path switch on theirs, executable flag switch on ours
12021219
res = testrepo.merge_file_from_index(
1203-
hello_txt, hello_txt_executable, hello_world, use_deprecated=False
1220+
hello_txt, hello_txt_executable, hello_world
12041221
)
12051222
assert res == MergeFileResult(
12061223
True,

test/test_repository_bare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def create_conflict_file(repo: Repository, branch: Branch, content: str) -> Oid:
241241
(a, t, o) = index.conflicts['conflict']
242242
diff = barerepo.merge_file_from_index(a, t, o)
243243
assert (
244-
diff
244+
diff.contents
245245
== """<<<<<<< conflict
246246
ASCII - abc
247247
=======

0 commit comments

Comments
 (0)