|
31 | 31 |
|
32 | 32 | # pygit2
|
33 | 33 | import pygit2
|
34 |
| -from pygit2 import init_repository, clone_repository, discover_repository |
| 34 | +from pygit2 import init_repository, clone_repository, discover_repository, IndexEntry |
35 | 35 | from pygit2 import Oid
|
36 | 36 | from pygit2.enums import (
|
37 | 37 | CheckoutNotify,
|
|
42 | 42 | RepositoryState,
|
43 | 43 | ResetMode,
|
44 | 44 | StashApplyProgress,
|
| 45 | + FileMode, |
45 | 46 | )
|
| 47 | +from pygit2.index import MergeFileResult |
46 | 48 | from . import utils
|
47 | 49 |
|
48 | 50 |
|
@@ -985,3 +987,130 @@ def test_repository_hashfile_filter(testrepo):
|
985 | 987 | testrepo.config['core.safecrlf'] = 'fail'
|
986 | 988 | with pytest.raises(pygit2.GitError):
|
987 | 989 | h = testrepo.hashfile('hello.txt')
|
| 990 | + |
| 991 | + |
| 992 | +def test_merge_file_from_index_deprecated(testrepo): |
| 993 | + hello_txt = testrepo.index['hello.txt'] |
| 994 | + hello_txt_executable = IndexEntry( |
| 995 | + hello_txt.path, hello_txt.id, FileMode.BLOB_EXECUTABLE |
| 996 | + ) |
| 997 | + hello_world = IndexEntry('hello_world.txt', hello_txt.id, hello_txt.mode) |
| 998 | + other_file_blob = testrepo.create_blob('Data that will clash with hello.txt') |
| 999 | + |
| 1000 | + # no change |
| 1001 | + res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_txt) |
| 1002 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1003 | + |
| 1004 | + # executable switch on ours |
| 1005 | + res = testrepo.merge_file_from_index(hello_txt, hello_txt_executable, hello_txt) |
| 1006 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1007 | + |
| 1008 | + # executable switch on theirs |
| 1009 | + res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_txt_executable) |
| 1010 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1011 | + |
| 1012 | + # executable switch on both |
| 1013 | + res = testrepo.merge_file_from_index( |
| 1014 | + hello_txt, hello_txt_executable, hello_txt_executable |
| 1015 | + ) |
| 1016 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1017 | + |
| 1018 | + # path switch on ours |
| 1019 | + res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_txt) |
| 1020 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1021 | + |
| 1022 | + # path switch on theirs |
| 1023 | + res = testrepo.merge_file_from_index(hello_txt, hello_txt, hello_world) |
| 1024 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1025 | + |
| 1026 | + # path switch on both |
| 1027 | + res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_world) |
| 1028 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1029 | + |
| 1030 | + # path switch on ours, executable flag switch on theirs |
| 1031 | + res = testrepo.merge_file_from_index(hello_txt, hello_world, hello_txt_executable) |
| 1032 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1033 | + |
| 1034 | + # path switch on theirs, executable flag switch on ours |
| 1035 | + res = testrepo.merge_file_from_index(hello_txt, hello_txt_executable, hello_world) |
| 1036 | + assert res == testrepo.get(hello_txt.id).data.decode() |
| 1037 | + |
| 1038 | + |
| 1039 | +def test_merge_file_from_index_non_deprecated(testrepo): |
| 1040 | + hello_txt = testrepo.index['hello.txt'] |
| 1041 | + hello_txt_executable = IndexEntry( |
| 1042 | + hello_txt.path, hello_txt.id, FileMode.BLOB_EXECUTABLE |
| 1043 | + ) |
| 1044 | + hello_world = IndexEntry('hello_world.txt', hello_txt.id, hello_txt.mode) |
| 1045 | + |
| 1046 | + # no change |
| 1047 | + res = testrepo.merge_file_from_index( |
| 1048 | + hello_txt, hello_txt, hello_txt, use_deprecated=False |
| 1049 | + ) |
| 1050 | + assert res == MergeFileResult( |
| 1051 | + True, hello_txt.path, hello_txt.mode, testrepo.get(hello_txt.id).data.decode() |
| 1052 | + ) |
| 1053 | + |
| 1054 | + # executable switch on ours |
| 1055 | + res = testrepo.merge_file_from_index( |
| 1056 | + hello_txt, hello_txt_executable, hello_txt, use_deprecated=False |
| 1057 | + ) |
| 1058 | + assert res == MergeFileResult( |
| 1059 | + True, hello_txt.path, hello_txt_executable.mode, testrepo.get(hello_txt.id).data.decode() |
| 1060 | + ) |
| 1061 | + |
| 1062 | + # executable switch on theirs |
| 1063 | + res = testrepo.merge_file_from_index( |
| 1064 | + hello_txt, hello_txt, hello_txt_executable, use_deprecated=False |
| 1065 | + ) |
| 1066 | + assert res == MergeFileResult( |
| 1067 | + True, hello_txt.path, hello_txt_executable.mode, testrepo.get(hello_txt.id).data.decode() |
| 1068 | + ) |
| 1069 | + |
| 1070 | + # executable switch on both |
| 1071 | + res = testrepo.merge_file_from_index( |
| 1072 | + hello_txt, hello_txt_executable, hello_txt_executable, use_deprecated=False |
| 1073 | + ) |
| 1074 | + assert res == MergeFileResult( |
| 1075 | + True, hello_txt.path, hello_txt_executable.mode, testrepo.get(hello_txt.id).data.decode() |
| 1076 | + ) |
| 1077 | + |
| 1078 | + # path switch on ours |
| 1079 | + res = testrepo.merge_file_from_index( |
| 1080 | + hello_txt, hello_world, hello_txt, use_deprecated=False |
| 1081 | + ) |
| 1082 | + assert res == MergeFileResult( |
| 1083 | + True, hello_world.path, hello_txt.mode, testrepo.get(hello_txt.id).data.decode() |
| 1084 | + ) |
| 1085 | + |
| 1086 | + # path switch on theirs |
| 1087 | + res = testrepo.merge_file_from_index( |
| 1088 | + hello_txt, hello_txt, hello_world, use_deprecated=False |
| 1089 | + ) |
| 1090 | + assert res == MergeFileResult( |
| 1091 | + True, hello_world.path, hello_txt.mode, testrepo.get(hello_txt.id).data.decode() |
| 1092 | + ) |
| 1093 | + |
| 1094 | + # path switch on both |
| 1095 | + res = testrepo.merge_file_from_index( |
| 1096 | + hello_txt, hello_world, hello_world, use_deprecated=False |
| 1097 | + ) |
| 1098 | + assert res == MergeFileResult( |
| 1099 | + True, None, hello_txt.mode, testrepo.get(hello_txt.id).data.decode() |
| 1100 | + ) |
| 1101 | + |
| 1102 | + # path switch on ours, executable flag switch on theirs |
| 1103 | + res = testrepo.merge_file_from_index( |
| 1104 | + hello_txt, hello_world, hello_txt_executable, use_deprecated=False |
| 1105 | + ) |
| 1106 | + assert res == MergeFileResult( |
| 1107 | + True, hello_world.path, hello_txt_executable.mode, testrepo.get(hello_txt.id).data.decode() |
| 1108 | + ) |
| 1109 | + |
| 1110 | + # path switch on theirs, executable flag switch on ours |
| 1111 | + res = testrepo.merge_file_from_index( |
| 1112 | + hello_txt, hello_txt_executable, hello_world, use_deprecated=False |
| 1113 | + ) |
| 1114 | + assert res == MergeFileResult( |
| 1115 | + True, hello_world.path, hello_txt_executable.mode, testrepo.get(hello_txt.id).data.decode() |
| 1116 | + ) |
0 commit comments