@@ -243,11 +243,7 @@ async def fetch_remote_branches_or_tags(url: str, *, ref_type: str, token: str |
243243 await ensure_git_installed ()
244244
245245 def fetch_refs ():
246- git_cmd = Git ()
247-
248- # Set up authentication if needed
249- if token and is_github_host (url ):
250- git_cmd = git_cmd .with_custom_environment (GIT_CONFIG_PARAMETERS = create_git_auth_header (token , url = url ))
246+ git_cmd = create_git_command_with_auth (token , url )
251247
252248 fetch_tags = ref_type == "tags"
253249 to_fetch = "tags" if fetch_tags else "heads"
@@ -278,7 +274,29 @@ def fetch_refs():
278274 ]
279275
280276
281- def create_git_command_with_auth (token : str | None , url : str ) -> Git :
277+ class GitCommandWithAuth :
278+ """A wrapper around Git command that stores authentication environment."""
279+
280+ def __init__ (self , token : str | None , url : str ):
281+ self .git = Git ()
282+ self .env = None
283+
284+ if token and is_github_host (url ):
285+ import os
286+ self .env = os .environ .copy ()
287+ self .env ["GIT_CONFIG_PARAMETERS" ] = create_git_auth_header (token , url = url )
288+
289+ def execute (self , args : list [str ]) -> str :
290+ """Execute a git command with authentication if needed."""
291+ return self .git .execute (args , env = self .env )
292+
293+ @property
294+ def custom_environment (self ) -> dict [str , str ] | None :
295+ """Get the custom environment for testing."""
296+ return self .env
297+
298+
299+ def create_git_command_with_auth (token : str | None , url : str ) -> GitCommandWithAuth :
282300 """Create a Git command object with authentication if needed.
283301
284302 Parameters
@@ -290,15 +308,11 @@ def create_git_command_with_auth(token: str | None, url: str) -> Git:
290308
291309 Returns
292310 -------
293- Git
294- A Git command object with authentication configured if needed.
311+ GitCommandWithAuth
312+ A Git command wrapper with authentication configured if needed.
295313
296314 """
297- if token and is_github_host (url ):
298- # Set authentication through environment
299- auth_config = create_git_auth_header (token , url = url )
300- return Git ().with_custom_environment (GIT_CONFIG_PARAMETERS = auth_config )
301- return Git ()
315+ return GitCommandWithAuth (token , url )
302316
303317
304318def create_git_auth_header (token : str , url : str = "https://github.com" ) -> str :
@@ -369,13 +383,15 @@ async def checkout_partial_clone(config: CloneConfig, token: str | None) -> None
369383 def setup_sparse_checkout ():
370384 try :
371385 repo = Repo (config .local_path )
372- git_cmd = repo .git
373386
374- # Set up authentication if needed
387+ # Set up authentication environment if needed
388+ env = None
375389 if token and is_github_host (config .url ):
376- git_cmd = git_cmd .with_custom_environment (GIT_CONFIG_PARAMETERS = create_git_auth_header (token , url = config .url ))
390+ import os
391+ env = os .environ .copy ()
392+ env ["GIT_CONFIG_PARAMETERS" ] = create_git_auth_header (token , url = config .url )
377393
378- git_cmd . execute (["sparse-checkout" , "set" , subpath ])
394+ repo . git . execute (["sparse-checkout" , "set" , subpath ], env = env )
379395 except Exception as e :
380396 raise RuntimeError (f"Failed to setup sparse checkout: { str (e )} " ) from e
381397
0 commit comments