@@ -12,6 +12,7 @@ class Git:
1212
1313 def __init__ (self , path : str ):
1414 self .path = path
15+ self .ensure_safe_directory (path )
1516 self .repo = Repo (path )
1617 assert self .repo
1718 self .head = self .repo .head
@@ -409,3 +410,24 @@ def is_on_default_branch(self) -> bool:
409410 except Exception as error :
410411 log .debug (f"Error checking if on default branch: { error } " )
411412 return False
413+ 414+ @staticmethod
415+ def ensure_safe_directory (path : str ) -> None :
416+ # Ensure the repo is marked as safe for git (prevents SHA empty/dubious ownership errors)
417+ try :
418+ import subprocess
419+ abs_path = os .path .abspath (path )
420+ # Get all safe directories
421+ result = subprocess .run ([
422+ "git" , "config" , "--global" , "--get-all" , "safe.directory"
423+ ], capture_output = True , text = True )
424+ safe_dirs = result .stdout .splitlines () if result .returncode == 0 else []
425+ if abs_path not in safe_dirs :
426+ subprocess .run ([
427+ "git" , "config" , "--global" , "--add" , "safe.directory" , abs_path
428+ ], check = True )
429+ log .debug (f"Added { abs_path } to git safe.directory config." )
430+ else :
431+ log .debug (f"{ abs_path } already present in git safe.directory config." )
432+ except Exception as safe_error :
433+ log .debug (f"Failed to set safe.directory for git: { safe_error } " )
0 commit comments