Skip to content

Commit 095b0cc

Browse files
authored
Added logic to Ensure the repo is marked as safe for git (prevents SH… (#107)
* Added logic to Ensure the repo is marked as safe for git (prevents SHA empty/dubious ownership errors) * Fixed calling new function
1 parent 0546fab commit 095b0cc

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.1.33"
9+
version = "2.1.35"
1010
requires-python = ">= 3.10"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.1.33'
2+
__version__ = '2.1.35'

socketsecurity/core/git_interface.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)