3333from .enums import DiffOption , FileMode
3434from .errors import check_error
3535from .ffi import C , ffi
36- from .utils import GenericIterator , StrArray , to_bytes , to_str
36+ from .utils import GenericIterator , StrArray , decode_fs_path , encode_fs_path
3737
3838if typing .TYPE_CHECKING :
3939 from .repository import Repository
@@ -52,7 +52,7 @@ def __init__(self, path: str | PathLike[str] | None = None) -> None:
5252 to read from and write to.
5353 """
5454 cindex = ffi .new ('git_index **' )
55- err = C .git_index_open (cindex , to_bytes (path ))
55+ err = C .git_index_open (cindex , encode_fs_path (path ))
5656 check_error (err )
5757
5858 self ._repo = None
@@ -79,7 +79,7 @@ def __len__(self) -> int:
7979 return C .git_index_entrycount (self ._index )
8080
8181 def __contains__ (self , path ) -> bool :
82- err = C .git_index_find (ffi .NULL , self ._index , to_bytes (path ))
82+ err = C .git_index_find (ffi .NULL , self ._index , encode_fs_path (path ))
8383 if err == C .GIT_ENOTFOUND :
8484 return False
8585
@@ -89,7 +89,7 @@ def __contains__(self, path) -> bool:
8989 def __getitem__ (self , key : str | int | PathLike [str ]) -> 'IndexEntry' :
9090 centry = ffi .NULL
9191 if isinstance (key , str ) or hasattr (key , '__fspath__' ):
92- centry = C .git_index_get_bypath (self ._index , to_bytes (key ), 0 )
92+ centry = C .git_index_get_bypath (self ._index , encode_fs_path (key ), 0 )
9393 elif isinstance (key , int ):
9494 if key >= 0 :
9595 centry = C .git_index_get_byindex (self ._index , key )
@@ -180,12 +180,12 @@ def write_tree(self, repo: 'Repository | None' = None) -> Oid:
180180
181181 def remove (self , path : PathLike [str ] | str , level : int = 0 ) -> None :
182182 """Remove an entry from the Index."""
183- err = C .git_index_remove (self ._index , to_bytes (path ), level )
183+ err = C .git_index_remove (self ._index , encode_fs_path (path ), level )
184184 check_error (err , io = True )
185185
186186 def remove_directory (self , path : PathLike [str ] | str , level : int = 0 ) -> None :
187187 """Remove a directory from the Index."""
188- err = C .git_index_remove_directory (self ._index , to_bytes (path ), level )
188+ err = C .git_index_remove_directory (self ._index , encode_fs_path (path ), level )
189189 check_error (err , io = True )
190190
191191 def remove_all (self , pathspecs : typing .Sequence [str | PathLike [str ]]) -> None :
@@ -221,7 +221,7 @@ def add(self, path_or_entry: 'IndexEntry | str | PathLike[str]') -> None:
221221 err = C .git_index_add (self ._index , centry )
222222 elif isinstance (path_or_entry , str ) or hasattr (path_or_entry , '__fspath__' ):
223223 path = path_or_entry
224- err = C .git_index_add_bypath (self ._index , to_bytes (path ))
224+ err = C .git_index_add_bypath (self ._index , encode_fs_path (path ))
225225 else :
226226 raise TypeError ('argument must be string, Path or IndexEntry' )
227227
@@ -420,7 +420,7 @@ def _from_c(cls, centry):
420420 return None
421421
422422 automergeable = centry .automergeable != 0
423- path = to_str (ffi .string (centry .path )) if centry .path else None
423+ path = decode_fs_path (ffi .string (centry .path )) if centry .path else None
424424 mode = FileMode (centry .mode )
425425 contents = ffi .string (centry .ptr , centry .len ).decode ('utf-8' )
426426
@@ -475,7 +475,7 @@ def _to_c(self) -> tuple['ffi.GitIndexEntryC', 'ffi.ArrayC[ffi.char]']:
475475 # basically memcpy()
476476 ffi .buffer (ffi .addressof (centry , 'id' ))[:] = self .id .raw [:]
477477 centry .mode = int (self .mode )
478- path = ffi .new ('char[]' , to_bytes (self .path ))
478+ path = ffi .new ('char[]' , encode_fs_path (self .path ))
479479 centry .path = path
480480
481481 return centry , path
@@ -486,7 +486,7 @@ def _from_c(cls, centry):
486486 return None
487487
488488 entry = cls .__new__ (cls )
489- entry .path = to_str (ffi .string (centry .path ))
489+ entry .path = decode_fs_path (ffi .string (centry .path ))
490490 entry .mode = FileMode (centry .mode )
491491 entry .id = Oid (raw = bytes (ffi .buffer (ffi .addressof (centry , 'id' ))[:]))
492492
@@ -503,7 +503,7 @@ def __getitem__(self, path):
503503 ctheirs = ffi .new ('git_index_entry **' )
504504
505505 err = C .git_index_conflict_get (
506- cancestor , cours , ctheirs , self ._index ._index , to_bytes (path )
506+ cancestor , cours , ctheirs , self ._index ._index , encode_fs_path (path )
507507 )
508508 check_error (err )
509509
@@ -514,7 +514,7 @@ def __getitem__(self, path):
514514 return ancestor , ours , theirs
515515
516516 def __delitem__ (self , path ):
517- err = C .git_index_conflict_remove (self ._index ._index , to_bytes (path ))
517+ err = C .git_index_conflict_remove (self ._index ._index , encode_fs_path (path ))
518518 check_error (err )
519519
520520 def __iter__ (self ):
@@ -526,7 +526,7 @@ def __contains__(self, path):
526526 ctheirs = ffi .new ('git_index_entry **' )
527527
528528 err = C .git_index_conflict_get (
529- cancestor , cours , ctheirs , self ._index ._index , to_bytes (path )
529+ cancestor , cours , ctheirs , self ._index ._index , encode_fs_path (path )
530530 )
531531 if err == C .GIT_ENOTFOUND :
532532 return False
0 commit comments