From c6ccd6e10fb1de292074dac43a7a26c3deb321fd Mon Sep 17 00:00:00 2001 From: VALLONGOL Date: Tue, 29 Jul 2025 11:23:50 +0200 Subject: [PATCH] fix git_commit --- gitutility/commands/git_commands.py | 49 ++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/gitutility/commands/git_commands.py b/gitutility/commands/git_commands.py index 516bc8e..1bfd45b 100644 --- a/gitutility/commands/git_commands.py +++ b/gitutility/commands/git_commands.py @@ -623,7 +623,19 @@ class GitCommands: ) from e # --- 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" log_handler.log_info( f"Attempting commit in '{working_directory}' msg: '{message[:50]}...'", @@ -632,25 +644,40 @@ class GitCommands: if not message or message.isspace(): raise ValueError("Commit message cannot be empty.") try: - add_cmd = ["git", "add", "."] - 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) + if stage_all_first: + add_cmd = ["git", "add", "."] + 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) + else: + log_handler.log_debug( + "Skipping 'git add .', assuming changes are already staged.", func_name=func_name + ) + commit_cmd = ["git", "commit", "-m", message] log_handler.log_debug("Attempting commit...", func_name=func_name) result = self.log_and_execute(commit_cmd, working_directory, check=False) + out_low = (result.stdout or "").lower() err_low = (result.stderr or "").lower() combined_low = out_low + err_low + if result.returncode == 0: - log_handler.log_info("Commit successful.", func_name=func_name) - return True + # Se c'è un output e non è un messaggio di "nessun cambiamento", allora è un successo. + 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 ( "nothing 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( "No changes available to commit.", func_name=func_name @@ -2201,7 +2228,7 @@ class GitCommands: submodules = [] # 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(): line = line.strip()