34
34
if TYPE_CHECKING :
35
35
from pygit2 import Commit , Oid , Signature
36
36
from pygit2 .config import Config as _Pygit2Config
37
+ from pygit2 .enums import CheckoutStrategy
37
38
from pygit2 .remotes import Remote
38
39
from pygit2 .repository import Repository
39
40
@@ -246,17 +247,15 @@ def _get_signature(self, name: str) -> "Signature":
246
247
)
247
248
248
249
@staticmethod
249
- def _get_checkout_strategy (strategy : Optional [int ] = None ):
250
- from pygit2 import (
251
- GIT_CHECKOUT_RECREATE_MISSING ,
252
- GIT_CHECKOUT_SAFE ,
253
- GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES ,
254
- )
250
+ def _get_checkout_strategy (
251
+ strategy : Optional ["CheckoutStrategy" ] = None ,
252
+ ) -> "CheckoutStrategy" :
253
+ from pygit2 .enums import CheckoutStrategy
255
254
256
255
if strategy is None :
257
- strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING
256
+ strategy = CheckoutStrategy . SAFE | CheckoutStrategy . RECREATE_MISSING
258
257
if os .name == "nt" :
259
- strategy |= GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
258
+ strategy |= CheckoutStrategy . SKIP_LOCKED_DIRECTORIES
260
259
return strategy
261
260
262
261
# Workaround to force git_backend_odb_pack to release open file handles
@@ -343,9 +342,12 @@ def checkout(
343
342
force : bool = False ,
344
343
** kwargs ,
345
344
):
346
- from pygit2 import GIT_CHECKOUT_FORCE , GitError
345
+ from pygit2 import GitError
346
+ from pygit2 .enums import CheckoutStrategy
347
347
348
- strategy = self ._get_checkout_strategy (GIT_CHECKOUT_FORCE if force else None )
348
+ strategy = self ._get_checkout_strategy (
349
+ CheckoutStrategy .FORCE if force else None
350
+ )
349
351
350
352
with self .release_odb_handles ():
351
353
if create_new :
@@ -613,7 +615,7 @@ def _merge_remote_branch(
613
615
force : bool = False ,
614
616
on_diverged : Optional [Callable [[str , str ], bool ]] = None ,
615
617
) -> SyncStatus :
616
- import pygit2
618
+ from pygit2 . enums import MergeAnalysis
617
619
618
620
rh_rev = self .resolve_rev (rh )
619
621
@@ -627,16 +629,16 @@ def _merge_remote_branch(
627
629
self .set_ref (lh , rh_rev )
628
630
return SyncStatus .SUCCESS
629
631
630
- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_UP_TO_DATE :
632
+ if merge_result & MergeAnalysis . UP_TO_DATE :
631
633
return SyncStatus .UP_TO_DATE
632
- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_FASTFORWARD :
634
+ if merge_result & MergeAnalysis . FASTFORWARD :
633
635
self .set_ref (lh , rh_rev )
634
636
return SyncStatus .SUCCESS
635
- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_NORMAL :
637
+ if merge_result & MergeAnalysis . NORMAL :
636
638
if on_diverged and on_diverged (lh , rh_rev ):
637
639
return SyncStatus .SUCCESS
638
640
return SyncStatus .DIVERGED
639
- logger .debug ("Unexpected merge result: %s" , pygit2 . GIT_MERGE_ANALYSIS_NORMAL )
641
+ logger .debug ("Unexpected merge result: %s" , MergeAnalysis . NORMAL )
640
642
raise SCMError ("Unknown merge analysis result" )
641
643
642
644
@contextmanager
@@ -779,7 +781,8 @@ def _stash_apply(
779
781
skip_conflicts : bool = False ,
780
782
** kwargs ,
781
783
):
782
- from pygit2 import GIT_CHECKOUT_ALLOW_CONFLICTS , GitError
784
+ from pygit2 import GitError
785
+ from pygit2 .enums import CheckoutStrategy
783
786
784
787
from scmrepo .git import Stash
785
788
@@ -788,7 +791,7 @@ def _apply(index):
788
791
self .repo .index .read (False )
789
792
strategy = self ._get_checkout_strategy ()
790
793
if skip_conflicts :
791
- strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS
794
+ strategy |= CheckoutStrategy . ALLOW_CONFLICTS
792
795
self .repo .stash_apply (
793
796
index , strategy = strategy , reinstate_index = reinstate_index
794
797
)
@@ -834,7 +837,8 @@ def diff(self, rev_a: str, rev_b: str, binary=False) -> str:
834
837
raise NotImplementedError
835
838
836
839
def reset (self , hard : bool = False , paths : Optional [Iterable [str ]] = None ):
837
- from pygit2 import GIT_RESET_HARD , GIT_RESET_MIXED , IndexEntry
840
+ from pygit2 import IndexEntry
841
+ from pygit2 .enums import ResetMode
838
842
839
843
self .repo .index .read (False )
840
844
if paths is not None :
@@ -847,9 +851,9 @@ def reset(self, hard: bool = False, paths: Optional[Iterable[str]] = None):
847
851
self .repo .index .add (IndexEntry (rel , obj .id , obj .filemode ))
848
852
self .repo .index .write ()
849
853
elif hard :
850
- self .repo .reset (self .repo .head .target , GIT_RESET_HARD )
854
+ self .repo .reset (self .repo .head .target , ResetMode . HARD )
851
855
else :
852
- self .repo .reset (self .repo .head .target , GIT_RESET_MIXED )
856
+ self .repo .reset (self .repo .head .target , ResetMode . MIXED )
853
857
854
858
def checkout_index (
855
859
self ,
@@ -858,22 +862,17 @@ def checkout_index(
858
862
ours : bool = False ,
859
863
theirs : bool = False ,
860
864
):
861
- from pygit2 import (
862
- GIT_CHECKOUT_ALLOW_CONFLICTS ,
863
- GIT_CHECKOUT_FORCE ,
864
- GIT_CHECKOUT_RECREATE_MISSING ,
865
- GIT_CHECKOUT_SAFE ,
866
- )
865
+ from pygit2 .enums import CheckoutStrategy
867
866
868
867
assert not (ours and theirs )
869
- strategy = GIT_CHECKOUT_RECREATE_MISSING
868
+ strategy = CheckoutStrategy . RECREATE_MISSING
870
869
if force or ours or theirs :
871
- strategy |= GIT_CHECKOUT_FORCE
870
+ strategy |= CheckoutStrategy . FORCE
872
871
else :
873
- strategy |= GIT_CHECKOUT_SAFE
872
+ strategy |= CheckoutStrategy . SAFE
874
873
875
874
if ours or theirs :
876
- strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS
875
+ strategy |= CheckoutStrategy . ALLOW_CONFLICTS
877
876
strategy = self ._get_checkout_strategy (strategy )
878
877
879
878
index = self .repo .index
@@ -910,18 +909,7 @@ def checkout_index(
910
909
def status (
911
910
self , ignored : bool = False , untracked_files : str = "all"
912
911
) -> tuple [Mapping [str , Iterable [str ]], Iterable [str ], Iterable [str ]]:
913
- from pygit2 import (
914
- GIT_STATUS_IGNORED ,
915
- GIT_STATUS_INDEX_DELETED ,
916
- GIT_STATUS_INDEX_MODIFIED ,
917
- GIT_STATUS_INDEX_NEW ,
918
- GIT_STATUS_WT_DELETED ,
919
- GIT_STATUS_WT_MODIFIED ,
920
- GIT_STATUS_WT_NEW ,
921
- GIT_STATUS_WT_RENAMED ,
922
- GIT_STATUS_WT_TYPECHANGE ,
923
- GIT_STATUS_WT_UNREADABLE ,
924
- )
912
+ from pygit2 .enums import FileStatus
925
913
926
914
staged : Mapping [str , list [str ]] = {
927
915
"add" : [],
@@ -932,19 +920,19 @@ def status(
932
920
untracked : list [str ] = []
933
921
934
922
states = {
935
- GIT_STATUS_WT_NEW : untracked ,
936
- GIT_STATUS_WT_MODIFIED : unstaged ,
937
- GIT_STATUS_WT_TYPECHANGE : staged ["modify" ],
938
- GIT_STATUS_WT_DELETED : staged ["modify" ],
939
- GIT_STATUS_WT_RENAMED : staged ["modify" ],
940
- GIT_STATUS_INDEX_NEW : staged ["add" ],
941
- GIT_STATUS_INDEX_MODIFIED : staged ["modify" ],
942
- GIT_STATUS_INDEX_DELETED : staged ["delete" ],
943
- GIT_STATUS_WT_UNREADABLE : untracked ,
923
+ FileStatus . WT_NEW : untracked ,
924
+ FileStatus . WT_MODIFIED : unstaged ,
925
+ FileStatus . WT_TYPECHANGE : staged ["modify" ],
926
+ FileStatus . WT_DELETED : staged ["modify" ],
927
+ FileStatus . WT_RENAMED : staged ["modify" ],
928
+ FileStatus . INDEX_NEW : staged ["add" ],
929
+ FileStatus . INDEX_MODIFIED : staged ["modify" ],
930
+ FileStatus . INDEX_DELETED : staged ["delete" ],
931
+ FileStatus . WT_UNREADABLE : untracked ,
944
932
}
945
933
946
934
if untracked_files != "no" and ignored :
947
- states [GIT_STATUS_IGNORED ] = untracked
935
+ states [FileStatus . IGNORED ] = untracked
948
936
949
937
for file , state in self .repo .status (
950
938
untracked_files = untracked_files , ignored = ignored
@@ -970,15 +958,8 @@ def merge( # noqa: C901
970
958
msg : Optional [str ] = None ,
971
959
squash : bool = False ,
972
960
) -> Optional [str ]:
973
- from pygit2 import (
974
- GIT_MERGE_ANALYSIS_FASTFORWARD ,
975
- GIT_MERGE_ANALYSIS_NONE ,
976
- GIT_MERGE_ANALYSIS_UNBORN ,
977
- GIT_MERGE_ANALYSIS_UP_TO_DATE ,
978
- GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY ,
979
- GIT_MERGE_PREFERENCE_NO_FASTFORWARD ,
980
- GitError ,
981
- )
961
+ from pygit2 import GitError
962
+ from pygit2 .enums import MergeAnalysis , MergePreference
982
963
983
964
if commit and squash :
984
965
raise SCMError ("Cannot merge with 'squash' and 'commit'" )
@@ -991,9 +972,9 @@ def merge( # noqa: C901
991
972
except GitError as exc :
992
973
raise SCMError ("Merge analysis failed" ) from exc
993
974
994
- if analysis == GIT_MERGE_ANALYSIS_NONE :
975
+ if analysis == MergeAnalysis . NONE :
995
976
raise SCMError (f"'{ rev } ' cannot be merged into HEAD" )
996
- if analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE :
977
+ if analysis & MergeAnalysis . UP_TO_DATE :
997
978
return None
998
979
999
980
try :
@@ -1006,15 +987,15 @@ def merge( # noqa: C901
1006
987
raise MergeConflictError ("Merge contained conflicts" )
1007
988
1008
989
try :
1009
- if not (squash or ff_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD ):
1010
- if analysis & GIT_MERGE_ANALYSIS_FASTFORWARD :
990
+ if not (squash or ff_pref & MergePreference . NO_FASTFORWARD ):
991
+ if analysis & MergeAnalysis . FASTFORWARD :
1011
992
return self ._merge_ff (rev , obj )
1012
993
1013
- if analysis & GIT_MERGE_ANALYSIS_UNBORN :
994
+ if analysis & MergeAnalysis . UNBORN :
1014
995
self .repo .set_head (obj .id )
1015
996
return str (obj .id )
1016
997
1017
- if ff_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY :
998
+ if ff_pref & MergePreference . FASTFORWARD_ONLY :
1018
999
raise SCMError (f"Cannot fast-forward HEAD to '{ rev } '" )
1019
1000
1020
1001
if commit :
@@ -1105,19 +1086,15 @@ def check_attr(
1105
1086
attr : str ,
1106
1087
source : Optional [str ] = None ,
1107
1088
) -> Optional [Union [bool , str ]]:
1108
- from pygit2 import (
1109
- GIT_ATTR_CHECK_FILE_THEN_INDEX ,
1110
- GIT_ATTR_CHECK_INCLUDE_COMMIT ,
1111
- GIT_ATTR_CHECK_INDEX_ONLY ,
1112
- GitError ,
1113
- )
1089
+ from pygit2 import GitError
1090
+ from pygit2 .enums import AttrCheck
1114
1091
1115
1092
commit : Optional ["Commit" ] = None
1116
- flags = GIT_ATTR_CHECK_FILE_THEN_INDEX
1093
+ flags = AttrCheck . FILE_THEN_INDEX
1117
1094
if source :
1118
1095
try :
1119
1096
commit , _ref = self ._resolve_refish (source )
1120
- flags = GIT_ATTR_CHECK_INDEX_ONLY | GIT_ATTR_CHECK_INCLUDE_COMMIT
1097
+ flags = AttrCheck . INDEX_ONLY | AttrCheck . INCLUDE_COMMIT
1121
1098
except (KeyError , GitError ) as exc :
1122
1099
raise SCMError (f"Invalid commit '{ source } '" ) from exc
1123
1100
try :
0 commit comments