49
49
50
50
51
51
# typing ----------------------------------------------------------------------
52
- from typing import Callable , Dict , Mapping , Sequence , TYPE_CHECKING , cast
52
+ from typing import Dict , TYPE_CHECKING
53
53
from typing import Any , Iterator , Union
54
54
55
- from git .types import Commit_ish , PathLike , TBD
55
+ from git .types import Commit_ish , PathLike
56
56
57
57
if TYPE_CHECKING :
58
58
from git .repo import Repo
59
- from git .index import IndexFile
60
59
61
60
62
61
# -----------------------------------------------------------------------------
@@ -132,17 +131,17 @@ def __init__(self, repo: 'Repo', binsha: bytes,
132
131
if url is not None :
133
132
self ._url = url
134
133
if branch_path is not None :
135
- # assert isinstance(branch_path, str)
134
+ assert isinstance (branch_path , str )
136
135
self ._branch_path = branch_path
137
136
if name is not None :
138
137
self ._name = name
139
138
140
139
def _set_cache_ (self , attr : str ) -> None :
141
140
if attr in ('path' , '_url' , '_branch_path' ):
142
- reader : SectionConstraint = self .config_reader ()
141
+ reader = self .config_reader ()
143
142
# default submodule values
144
143
try :
145
- self .path : PathLike = reader .get ('path' )
144
+ self .path = reader .get ('path' )
146
145
except cp .NoSectionError as e :
147
146
if self .repo .working_tree_dir is not None :
148
147
raise ValueError ("This submodule instance does not exist anymore in '%s' file"
@@ -227,7 +226,7 @@ def _config_parser(cls, repo: 'Repo',
227
226
228
227
return SubmoduleConfigParser (fp_module , read_only = read_only )
229
228
230
- def _clear_cache (self ) -> None :
229
+ def _clear_cache (self ):
231
230
# clear the possibly changed values
232
231
for name in self ._cache_attrs :
233
232
try :
@@ -247,7 +246,7 @@ def _sio_modules(cls, parent_commit: Commit_ish) -> BytesIO:
247
246
def _config_parser_constrained (self , read_only : bool ) -> SectionConstraint :
248
247
""":return: Config Parser constrained to our submodule in read or write mode"""
249
248
try :
250
- pc : Union [ 'Commit_ish' , None ] = self .parent_commit
249
+ pc = self .parent_commit
251
250
except ValueError :
252
251
pc = None
253
252
# end handle empty parent repository
@@ -256,12 +255,10 @@ def _config_parser_constrained(self, read_only: bool) -> SectionConstraint:
256
255
return SectionConstraint (parser , sm_section (self .name ))
257
256
258
257
@classmethod
259
- def _module_abspath (cls , parent_repo : 'Repo' , path : PathLike , name : str ) -> PathLike :
258
+ def _module_abspath (cls , parent_repo , path , name ) :
260
259
if cls ._need_gitfile_submodules (parent_repo .git ):
261
260
return osp .join (parent_repo .git_dir , 'modules' , name )
262
- if parent_repo .working_tree_dir :
263
- return osp .join (parent_repo .working_tree_dir , path )
264
- raise NotADirectoryError ()
261
+ return osp .join (parent_repo .working_tree_dir , path )
265
262
# end
266
263
267
264
@classmethod
@@ -289,15 +286,15 @@ def _clone_repo(cls, repo, url, path, name, **kwargs):
289
286
return clone
290
287
291
288
@classmethod
292
- def _to_relative_path (cls , parent_repo : 'Repo' , path : PathLike ) -> PathLike :
289
+ def _to_relative_path (cls , parent_repo , path ) :
293
290
""":return: a path guaranteed to be relative to the given parent - repository
294
291
:raise ValueError: if path is not contained in the parent repository's working tree"""
295
292
path = to_native_path_linux (path )
296
293
if path .endswith ('/' ):
297
294
path = path [:- 1 ]
298
295
# END handle trailing slash
299
296
300
- if osp .isabs (path ) and parent_repo . working_tree_dir :
297
+ if osp .isabs (path ):
301
298
working_tree_linux = to_native_path_linux (parent_repo .working_tree_dir )
302
299
if not path .startswith (working_tree_linux ):
303
300
raise ValueError ("Submodule checkout path '%s' needs to be within the parents repository at '%s'"
@@ -311,7 +308,7 @@ def _to_relative_path(cls, parent_repo: 'Repo', path: PathLike) -> PathLike:
311
308
return path
312
309
313
310
@classmethod
314
- def _write_git_file_and_module_config (cls , working_tree_dir : PathLike , module_abspath : PathLike ) -> None :
311
+ def _write_git_file_and_module_config (cls , working_tree_dir , module_abspath ) :
315
312
"""Writes a .git file containing a(preferably) relative path to the actual git module repository.
316
313
It is an error if the module_abspath cannot be made into a relative path, relative to the working_tree_dir
317
314
:note: will overwrite existing files !
@@ -338,8 +335,7 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab
338
335
339
336
@classmethod
340
337
def add (cls , repo : 'Repo' , name : str , path : PathLike , url : Union [str , None ] = None ,
341
- branch : Union [str , None ] = None , no_checkout : bool = False , depth : Union [int , None ] = None ,
342
- env : Mapping [str , str ] = None , clone_multi_options : Union [Sequence [TBD ], None ] = None
338
+ branch = None , no_checkout : bool = False , depth = None , env = None
343
339
) -> 'Submodule' :
344
340
"""Add a new submodule to the given repository. This will alter the index
345
341
as well as the .gitmodules file, but will not create a new commit.
@@ -373,8 +369,6 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
373
369
and is defined in `os.environ`, value from `os.environ` will be used.
374
370
If you want to unset some variable, consider providing empty string
375
371
as its value.
376
- :param clone_multi_options: A list of Clone options. Please see ``git.repo.base.Repo.clone``
377
- for details.
378
372
:return: The newly created submodule instance
379
373
:note: works atomically, such that no change will be done if the repository
380
374
update fails for instance"""
@@ -387,15 +381,15 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
387
381
# assure we never put backslashes into the url, as some operating systems
388
382
# like it ...
389
383
if url is not None :
390
- url = to_native_path_linux (url )
384
+ url = to_native_path_linux (url ) # to_native_path_linux does nothing??
391
385
# END assure url correctness
392
386
393
387
# INSTANTIATE INTERMEDIATE SM
394
388
sm = cls (repo , cls .NULL_BIN_SHA , cls .k_default_mode , path , name , url = 'invalid-temporary' )
395
389
if sm .exists ():
396
390
# reretrieve submodule from tree
397
391
try :
398
- sm = repo .head .commit .tree [str ( path )]
392
+ sm = repo .head .commit .tree [path ] # type: ignore
399
393
sm ._name = name
400
394
return sm
401
395
except KeyError :
@@ -418,8 +412,7 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
418
412
# END check url
419
413
# END verify urls match
420
414
421
- mrepo : Union [Repo , None ] = None
422
-
415
+ mrepo = None
423
416
if url is None :
424
417
if not has_module :
425
418
raise ValueError ("A URL was not given and a repository did not exist at %s" % path )
@@ -432,7 +425,7 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
432
425
url = urls [0 ]
433
426
else :
434
427
# clone new repo
435
- kwargs : Dict [str , Union [bool , int , Sequence [ TBD ] ]] = {'n' : no_checkout }
428
+ kwargs : Dict [str , Union [bool , int ]] = {'n' : no_checkout }
436
429
if not branch_is_default :
437
430
kwargs ['b' ] = br .name
438
431
# END setup checkout-branch
@@ -442,8 +435,6 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
442
435
kwargs ['depth' ] = depth
443
436
else :
444
437
raise ValueError ("depth should be an integer" )
445
- if clone_multi_options :
446
- kwargs ['multi_options' ] = clone_multi_options
447
438
448
439
# _clone_repo(cls, repo, url, path, name, **kwargs):
449
440
mrepo = cls ._clone_repo (repo , url , path , name , env = env , ** kwargs )
@@ -456,8 +447,6 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
456
447
# otherwise there is a '-' character in front of the submodule listing
457
448
# a38efa84daef914e4de58d1905a500d8d14aaf45 mymodule (v0.9.0-1-ga38efa8)
458
449
# -a38efa84daef914e4de58d1905a500d8d14aaf45 submodules/intermediate/one
459
- writer : Union [GitConfigParser , SectionConstraint ]
460
-
461
450
with sm .repo .config_writer () as writer :
462
451
writer .set_value (sm_section (name ), 'url' , url )
463
452
@@ -474,16 +463,13 @@ def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = No
474
463
sm ._branch_path = br .path
475
464
476
465
# we deliberately assume that our head matches our index !
477
- mrepo = cast ('Repo' , mrepo )
478
466
sm .binsha = mrepo .head .commit .binsha
479
467
index .add ([sm ], write = True )
480
468
481
469
return sm
482
470
483
- def update (self , recursive : bool = False , init : bool = True , to_latest_revision : bool = False ,
484
- progress : Union ['UpdateProgress' , None ] = None , dry_run : bool = False ,
485
- force : bool = False , keep_going : bool = False , env : Mapping [str , str ] = None ,
486
- clone_multi_options : Union [Sequence [TBD ], None ] = None ):
471
+ def update (self , recursive = False , init = True , to_latest_revision = False , progress = None , dry_run = False ,
472
+ force = False , keep_going = False , env = None ):
487
473
"""Update the repository of this submodule to point to the checkout
488
474
we point at with the binsha of this instance.
489
475
@@ -514,8 +500,6 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
514
500
and is defined in `os.environ`, value from `os.environ` will be used.
515
501
If you want to unset some variable, consider providing empty string
516
502
as its value.
517
- :param clone_multi_options: list of Clone options. Please see ``git.repo.base.Repo.clone``
518
- for details. Only take effect with `init` option.
519
503
:note: does nothing in bare repositories
520
504
:note: method is definitely not atomic if recurisve is True
521
505
:return: self"""
@@ -582,16 +566,13 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
582
566
progress .update (BEGIN | CLONE , 0 , 1 , prefix + "Cloning url '%s' to '%s' in submodule %r" %
583
567
(self .url , checkout_module_abspath , self .name ))
584
568
if not dry_run :
585
- mrepo = self ._clone_repo (self .repo , self .url , self .path , self .name , n = True , env = env ,
586
- multi_options = clone_multi_options )
569
+ mrepo = self ._clone_repo (self .repo , self .url , self .path , self .name , n = True , env = env )
587
570
# END handle dry-run
588
571
progress .update (END | CLONE , 0 , 1 , prefix + "Done cloning to %s" % checkout_module_abspath )
589
572
590
573
if not dry_run :
591
574
# see whether we have a valid branch to checkout
592
575
try :
593
- # assert isinstance(mrepo, Repo) # cant do this cos of circular import
594
- mrepo = cast ('Repo' , mrepo ) # Try TypeGuard wirh hasattr, or has_remotes&_head protocol?
595
576
# find a remote which has our branch - we try to be flexible
596
577
remote_branch = find_first_remote_branch (mrepo .remotes , self .branch_name )
597
578
local_branch = mkhead (mrepo , self .branch_path )
@@ -652,7 +633,7 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
652
633
may_reset = True
653
634
if mrepo .head .commit .binsha != self .NULL_BIN_SHA :
654
635
base_commit = mrepo .merge_base (mrepo .head .commit , hexsha )
655
- if len (base_commit ) == 0 or base_commit [0 ].hexsha == hexsha : # type: ignore
636
+ if len (base_commit ) == 0 or base_commit [0 ].hexsha == hexsha :
656
637
if force :
657
638
msg = "Will force checkout or reset on local branch that is possibly in the future of"
658
639
msg += "the commit it will be checked out to, effectively 'forgetting' new commits"
@@ -819,8 +800,7 @@ def move(self, module_path, configuration=True, module=True):
819
800
return self
820
801
821
802
@unbare_repo
822
- def remove (self , module : bool = True , force : bool = False ,
823
- configuration : bool = True , dry_run : bool = False ) -> 'Submodule' :
803
+ def remove (self , module = True , force = False , configuration = True , dry_run = False ):
824
804
"""Remove this submodule from the repository. This will remove our entry
825
805
from the .gitmodules file and the entry in the .git / config file.
826
806
@@ -874,7 +854,7 @@ def remove(self, module: bool = True, force: bool = False,
874
854
# TODO: If we run into permission problems, we have a highly inconsistent
875
855
# state. Delete the .git folders last, start with the submodules first
876
856
mp = self .abspath
877
- method : Union [ None , Callable [[ PathLike ], None ]] = None
857
+ method = None
878
858
if osp .islink (mp ):
879
859
method = os .remove
880
860
elif osp .isdir (mp ):
@@ -927,7 +907,7 @@ def remove(self, module: bool = True, force: bool = False,
927
907
import gc
928
908
gc .collect ()
929
909
try :
930
- rmtree (str ( wtd ) )
910
+ rmtree (wtd )
931
911
except Exception as ex :
932
912
if HIDE_WINDOWS_KNOWN_ERRORS :
933
913
raise SkipTest ("FIXME: fails with: PermissionError\n {}" .format (ex )) from ex
@@ -941,7 +921,7 @@ def remove(self, module: bool = True, force: bool = False,
941
921
rmtree (git_dir )
942
922
except Exception as ex :
943
923
if HIDE_WINDOWS_KNOWN_ERRORS :
944
- raise SkipTest (f "FIXME: fails with: PermissionError\n { ex } " ) from ex
924
+ raise SkipTest ("FIXME: fails with: PermissionError\n %s" , ex ) from ex
945
925
else :
946
926
raise
947
927
# end handle separate bare repository
@@ -965,8 +945,6 @@ def remove(self, module: bool = True, force: bool = False,
965
945
966
946
# now git config - need the config intact, otherwise we can't query
967
947
# information anymore
968
- writer : Union [GitConfigParser , SectionConstraint ]
969
-
970
948
with self .repo .config_writer () as writer :
971
949
writer .remove_section (sm_section (self .name ))
972
950
@@ -976,7 +954,7 @@ def remove(self, module: bool = True, force: bool = False,
976
954
977
955
return self
978
956
979
- def set_parent_commit (self , commit : Union [Commit_ish , None ], check : bool = True ) -> 'Submodule' :
957
+ def set_parent_commit (self , commit : Union [Commit_ish , None ], check = True ):
980
958
"""Set this instance to use the given commit whose tree is supposed to
981
959
contain the .gitmodules blob.
982
960
@@ -1015,7 +993,7 @@ def set_parent_commit(self, commit: Union[Commit_ish, None], check: bool = True)
1015
993
# If check is False, we might see a parent-commit that doesn't even contain the submodule anymore.
1016
994
# in that case, mark our sha as being NULL
1017
995
try :
1018
- self .binsha = pctree [str ( self .path ) ].binsha
996
+ self .binsha = pctree [self .path ].binsha # type: ignore
1019
997
except KeyError :
1020
998
self .binsha = self .NULL_BIN_SHA
1021
999
# end
@@ -1024,7 +1002,7 @@ def set_parent_commit(self, commit: Union[Commit_ish, None], check: bool = True)
1024
1002
return self
1025
1003
1026
1004
@unbare_repo
1027
- def config_writer (self , index : Union [ 'IndexFile' , None ] = None , write : bool = True ) -> SectionConstraint :
1005
+ def config_writer (self , index = None , write = True ):
1028
1006
""":return: a config writer instance allowing you to read and write the data
1029
1007
belonging to this submodule into the .gitmodules file.
1030
1008
@@ -1045,7 +1023,7 @@ def config_writer(self, index: Union['IndexFile', None] = None, write: bool = Tr
1045
1023
return writer
1046
1024
1047
1025
@unbare_repo
1048
- def rename (self , new_name : str ) -> 'Submodule' :
1026
+ def rename (self , new_name ) :
1049
1027
"""Rename this submodule
1050
1028
:note: This method takes care of renaming the submodule in various places, such as
1051
1029
@@ -1080,14 +1058,13 @@ def rename(self, new_name: str) -> 'Submodule':
1080
1058
destination_module_abspath = self ._module_abspath (self .repo , self .path , new_name )
1081
1059
source_dir = mod .git_dir
1082
1060
# Let's be sure the submodule name is not so obviously tied to a directory
1083
- if str ( destination_module_abspath ) .startswith (str ( mod .git_dir ) ):
1061
+ if destination_module_abspath .startswith (mod .git_dir ):
1084
1062
tmp_dir = self ._module_abspath (self .repo , self .path , str (uuid .uuid4 ()))
1085
1063
os .renames (source_dir , tmp_dir )
1086
1064
source_dir = tmp_dir
1087
1065
# end handle self-containment
1088
1066
os .renames (source_dir , destination_module_abspath )
1089
- if mod .working_tree_dir :
1090
- self ._write_git_file_and_module_config (mod .working_tree_dir , destination_module_abspath )
1067
+ self ._write_git_file_and_module_config (mod .working_tree_dir , destination_module_abspath )
1091
1068
# end move separate git repository
1092
1069
1093
1070
return self
@@ -1097,7 +1074,7 @@ def rename(self, new_name: str) -> 'Submodule':
1097
1074
#{ Query Interface
1098
1075
1099
1076
@unbare_repo
1100
- def module (self ) -> 'Repo' :
1077
+ def module (self ):
1101
1078
""":return: Repo instance initialized from the repository at our submodule path
1102
1079
:raise InvalidGitRepositoryError: if a repository was not available. This could
1103
1080
also mean that it was not yet initialized"""
@@ -1114,7 +1091,7 @@ def module(self) -> 'Repo':
1114
1091
raise InvalidGitRepositoryError ("Repository at %r was not yet checked out" % module_checkout_abspath )
1115
1092
# END handle exceptions
1116
1093
1117
- def module_exists (self ) -> bool :
1094
+ def module_exists (self ):
1118
1095
""":return: True if our module exists and is a valid git repository. See module() method"""
1119
1096
try :
1120
1097
self .module ()
@@ -1123,7 +1100,7 @@ def module_exists(self) -> bool:
1123
1100
return False
1124
1101
# END handle exception
1125
1102
1126
- def exists (self ) -> bool :
1103
+ def exists (self ):
1127
1104
"""
1128
1105
:return: True if the submodule exists, False otherwise. Please note that
1129
1106
a submodule may exist ( in the .gitmodules file) even though its module
@@ -1164,34 +1141,34 @@ def branch(self):
1164
1141
return mkhead (self .module (), self ._branch_path )
1165
1142
1166
1143
@property
1167
- def branch_path (self ) -> PathLike :
1144
+ def branch_path (self ):
1168
1145
"""
1169
1146
:return: full(relative) path as string to the branch we would checkout
1170
1147
from the remote and track"""
1171
1148
return self ._branch_path
1172
1149
1173
1150
@property
1174
- def branch_name (self ) -> str :
1151
+ def branch_name (self ):
1175
1152
""":return: the name of the branch, which is the shortest possible branch name"""
1176
1153
# use an instance method, for this we create a temporary Head instance
1177
1154
# which uses a repository that is available at least ( it makes no difference )
1178
1155
return git .Head (self .repo , self ._branch_path ).name
1179
1156
1180
1157
@property
1181
- def url (self ) -> str :
1158
+ def url (self ):
1182
1159
""":return: The url to the repository which our module - repository refers to"""
1183
1160
return self ._url
1184
1161
1185
1162
@property
1186
- def parent_commit (self ) -> 'Commit_ish' :
1163
+ def parent_commit (self ):
1187
1164
""":return: Commit instance with the tree containing the .gitmodules file
1188
1165
:note: will always point to the current head's commit if it was not set explicitly"""
1189
1166
if self ._parent_commit is None :
1190
1167
return self .repo .commit ()
1191
1168
return self ._parent_commit
1192
1169
1193
1170
@property
1194
- def name (self ) -> str :
1171
+ def name (self ):
1195
1172
""":return: The name of this submodule. It is used to identify it within the
1196
1173
.gitmodules file.
1197
1174
:note: by default, the name is the path at which to find the submodule, but
0 commit comments