fix git_commit

This commit is contained in:
VALLONGOL 2025-07-29 11:23:50 +02:00
parent 87ca907305
commit c6ccd6e10f

View File

@ -623,7 +623,19 @@ class GitCommands:
) from e ) from e
# --- Commit and Status --- # --- Commit and Status ---
def git_commit(self, working_directory: str, message: str) -> bool: def git_commit(self, working_directory: str, message: str, stage_all_first: bool = True) -> bool:
"""
Commits changes in the repository.
Args:
working_directory (str): Path to the repository.
message (str): The commit message.
stage_all_first (bool): If True, runs 'git add .' before committing.
Set to False if changes are already staged.
Returns:
bool: True if a commit was made, False otherwise.
"""
func_name = "git_commit" func_name = "git_commit"
log_handler.log_info( log_handler.log_info(
f"Attempting commit in '{working_directory}' msg: '{message[:50]}...'", f"Attempting commit in '{working_directory}' msg: '{message[:50]}...'",
@ -632,25 +644,40 @@ class GitCommands:
if not message or message.isspace(): if not message or message.isspace():
raise ValueError("Commit message cannot be empty.") raise ValueError("Commit message cannot be empty.")
try: try:
add_cmd = ["git", "add", "."] if stage_all_first:
log_handler.log_debug( add_cmd = ["git", "add", "."]
"Staging all changes ('git add .')...", func_name=func_name log_handler.log_debug(
) "Staging all changes ('git add .')...", func_name=func_name
self.log_and_execute(add_cmd, working_directory, check=True) )
log_handler.log_debug("Staging successful.", func_name=func_name) self.log_and_execute(add_cmd, working_directory, check=True)
log_handler.log_debug("Staging successful.", func_name=func_name)
else:
log_handler.log_debug(
"Skipping 'git add .', assuming changes are already staged.", func_name=func_name
)
commit_cmd = ["git", "commit", "-m", message] commit_cmd = ["git", "commit", "-m", message]
log_handler.log_debug("Attempting commit...", func_name=func_name) log_handler.log_debug("Attempting commit...", func_name=func_name)
result = self.log_and_execute(commit_cmd, working_directory, check=False) result = self.log_and_execute(commit_cmd, working_directory, check=False)
out_low = (result.stdout or "").lower() out_low = (result.stdout or "").lower()
err_low = (result.stderr or "").lower() err_low = (result.stderr or "").lower()
combined_low = out_low + err_low combined_low = out_low + err_low
if result.returncode == 0: if result.returncode == 0:
log_handler.log_info("Commit successful.", func_name=func_name) # Se c'è un output e non è un messaggio di "nessun cambiamento", allora è un successo.
return True if "nothing to commit" not in combined_low:
log_handler.log_info("Commit successful.", func_name=func_name)
return True
else:
# Questo caso può accadere se `stage_all_first` è False e non c'era nulla in staging
log_handler.log_info(
"No changes were available to commit.", func_name=func_name
)
return False
elif ( elif (
"nothing to commit" in combined_low "nothing to commit" in combined_low
or "no changes added to commit" in combined_low or "no changes added to commit" in combined_low
or "nothing added to commit" in combined_low
): ):
log_handler.log_info( log_handler.log_info(
"No changes available to commit.", func_name=func_name "No changes available to commit.", func_name=func_name
@ -2201,7 +2228,7 @@ class GitCommands:
submodules = [] submodules = []
# Regex to parse the status line # Regex to parse the status line
status_regex = re.compile(r"^\s*([U -+])([0-9a-fA-F]+)\s(.*?)\s\((.*?)\)\s*$") status_regex = re.compile(r"^\s*([ U+-])([0-9a-fA-F]+)\s(.*?)\s\((.*?)\)\s*$")
for line in result.stdout.strip().splitlines(): for line in result.stdout.strip().splitlines():
line = line.strip() line = line.strip()