3434if TYPE_CHECKING :
3535 from pygit2 import Commit , Oid , Signature
3636 from pygit2 .config import Config as _Pygit2Config
37+ from pygit2 .enums import CheckoutStrategy
3738 from pygit2 .remotes import Remote
3839 from pygit2 .repository import Repository
3940
@@ -246,17 +247,15 @@ def _get_signature(self, name: str) -> "Signature":
246247 )
247248
248249 @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
255254
256255 if strategy is None :
257- strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING
256+ strategy = CheckoutStrategy . SAFE | CheckoutStrategy . RECREATE_MISSING
258257 if os .name == "nt" :
259- strategy |= GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES
258+ strategy |= CheckoutStrategy . SKIP_LOCKED_DIRECTORIES
260259 return strategy
261260
262261 # Workaround to force git_backend_odb_pack to release open file handles
@@ -343,9 +342,12 @@ def checkout(
343342 force : bool = False ,
344343 ** kwargs ,
345344 ):
346- from pygit2 import GIT_CHECKOUT_FORCE , GitError
345+ from pygit2 import GitError
346+ from pygit2 .enums import CheckoutStrategy
347347
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+ )
349351
350352 with self .release_odb_handles ():
351353 if create_new :
@@ -613,7 +615,7 @@ def _merge_remote_branch(
613615 force : bool = False ,
614616 on_diverged : Optional [Callable [[str , str ], bool ]] = None ,
615617 ) -> SyncStatus :
616- import pygit2
618+ from pygit2 . enums import MergeAnalysis
617619
618620 rh_rev = self .resolve_rev (rh )
619621
@@ -627,16 +629,16 @@ def _merge_remote_branch(
627629 self .set_ref (lh , rh_rev )
628630 return SyncStatus .SUCCESS
629631
630- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_UP_TO_DATE :
632+ if merge_result & MergeAnalysis . UP_TO_DATE :
631633 return SyncStatus .UP_TO_DATE
632- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_FASTFORWARD :
634+ if merge_result & MergeAnalysis . FASTFORWARD :
633635 self .set_ref (lh , rh_rev )
634636 return SyncStatus .SUCCESS
635- if merge_result & pygit2 . GIT_MERGE_ANALYSIS_NORMAL :
637+ if merge_result & MergeAnalysis . NORMAL :
636638 if on_diverged and on_diverged (lh , rh_rev ):
637639 return SyncStatus .SUCCESS
638640 return SyncStatus .DIVERGED
639- logger .debug ("Unexpected merge result: %s" , pygit2 . GIT_MERGE_ANALYSIS_NORMAL )
641+ logger .debug ("Unexpected merge result: %s" , MergeAnalysis . NORMAL )
640642 raise SCMError ("Unknown merge analysis result" )
641643
642644 @contextmanager
@@ -779,7 +781,8 @@ def _stash_apply(
779781 skip_conflicts : bool = False ,
780782 ** kwargs ,
781783 ):
782- from pygit2 import GIT_CHECKOUT_ALLOW_CONFLICTS , GitError
784+ from pygit2 import GitError
785+ from pygit2 .enums import CheckoutStrategy
783786
784787 from scmrepo .git import Stash
785788
@@ -788,7 +791,7 @@ def _apply(index):
788791 self .repo .index .read (False )
789792 strategy = self ._get_checkout_strategy ()
790793 if skip_conflicts :
791- strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS
794+ strategy |= CheckoutStrategy . ALLOW_CONFLICTS
792795 self .repo .stash_apply (
793796 index , strategy = strategy , reinstate_index = reinstate_index
794797 )
@@ -834,7 +837,8 @@ def diff(self, rev_a: str, rev_b: str, binary=False) -> str:
834837 raise NotImplementedError
835838
836839 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
838842
839843 self .repo .index .read (False )
840844 if paths is not None :
@@ -847,9 +851,9 @@ def reset(self, hard: bool = False, paths: Optional[Iterable[str]] = None):
847851 self .repo .index .add (IndexEntry (rel , obj .id , obj .filemode ))
848852 self .repo .index .write ()
849853 elif hard :
850- self .repo .reset (self .repo .head .target , GIT_RESET_HARD )
854+ self .repo .reset (self .repo .head .target , ResetMode . HARD )
851855 else :
852- self .repo .reset (self .repo .head .target , GIT_RESET_MIXED )
856+ self .repo .reset (self .repo .head .target , ResetMode . MIXED )
853857
854858 def checkout_index (
855859 self ,
@@ -858,22 +862,17 @@ def checkout_index(
858862 ours : bool = False ,
859863 theirs : bool = False ,
860864 ):
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
867866
868867 assert not (ours and theirs )
869- strategy = GIT_CHECKOUT_RECREATE_MISSING
868+ strategy = CheckoutStrategy . RECREATE_MISSING
870869 if force or ours or theirs :
871- strategy |= GIT_CHECKOUT_FORCE
870+ strategy |= CheckoutStrategy . FORCE
872871 else :
873- strategy |= GIT_CHECKOUT_SAFE
872+ strategy |= CheckoutStrategy . SAFE
874873
875874 if ours or theirs :
876- strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS
875+ strategy |= CheckoutStrategy . ALLOW_CONFLICTS
877876 strategy = self ._get_checkout_strategy (strategy )
878877
879878 index = self .repo .index
@@ -910,18 +909,7 @@ def checkout_index(
910909 def status (
911910 self , ignored : bool = False , untracked_files : str = "all"
912911 ) -> 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
925913
926914 staged : Mapping [str , list [str ]] = {
927915 "add" : [],
@@ -932,19 +920,19 @@ def status(
932920 untracked : list [str ] = []
933921
934922 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 ,
944932 }
945933
946934 if untracked_files != "no" and ignored :
947- states [GIT_STATUS_IGNORED ] = untracked
935+ states [FileStatus . IGNORED ] = untracked
948936
949937 for file , state in self .repo .status (
950938 untracked_files = untracked_files , ignored = ignored
@@ -970,15 +958,8 @@ def merge( # noqa: C901
970958 msg : Optional [str ] = None ,
971959 squash : bool = False ,
972960 ) -> 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
982963
983964 if commit and squash :
984965 raise SCMError ("Cannot merge with 'squash' and 'commit'" )
@@ -991,9 +972,9 @@ def merge( # noqa: C901
991972 except GitError as exc :
992973 raise SCMError ("Merge analysis failed" ) from exc
993974
994- if analysis == GIT_MERGE_ANALYSIS_NONE :
975+ if analysis == MergeAnalysis . NONE :
995976 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 :
997978 return None
998979
999980 try :
@@ -1006,15 +987,15 @@ def merge( # noqa: C901
1006987 raise MergeConflictError ("Merge contained conflicts" )
1007988
1008989 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 :
1011992 return self ._merge_ff (rev , obj )
1012993
1013- if analysis & GIT_MERGE_ANALYSIS_UNBORN :
994+ if analysis & MergeAnalysis . UNBORN :
1014995 self .repo .set_head (obj .id )
1015996 return str (obj .id )
1016997
1017- if ff_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY :
998+ if ff_pref & MergePreference . FASTFORWARD_ONLY :
1018999 raise SCMError (f"Cannot fast-forward HEAD to '{ rev } '" )
10191000
10201001 if commit :
@@ -1105,19 +1086,15 @@ def check_attr(
11051086 attr : str ,
11061087 source : Optional [str ] = None ,
11071088 ) -> 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
11141091
11151092 commit : Optional ["Commit" ] = None
1116- flags = GIT_ATTR_CHECK_FILE_THEN_INDEX
1093+ flags = AttrCheck . FILE_THEN_INDEX
11171094 if source :
11181095 try :
11191096 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
11211098 except (KeyError , GitError ) as exc :
11221099 raise SCMError (f"Invalid commit '{ source } '" ) from exc
11231100 try :
0 commit comments