From fb4ac21de92b7ef403b90c3112cb63ccf30800a1 Mon Sep 17 00:00:00 2001 From: VALLONGOL Date: Fri, 21 Nov 2025 13:00:08 +0100 Subject: [PATCH] sistemato problema su revert to commit --- GitUtility.spec | 2 +- gitutility/async_tasks/async_workers.py | 47 +++++++++++++++++++++ gitutility/core/action_handler.py | 55 +++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/GitUtility.spec b/GitUtility.spec index 0c9dd09..2c166db 100644 --- a/GitUtility.spec +++ b/GitUtility.spec @@ -1,6 +1,6 @@ block_cipher = None import os -a = Analysis(scripts=['gitutility\\__main__.py'], pathex=['gitutility', '.'], binaries=[], datas=[('C:\\src\\____GitProjects\\GitUtility\\git_svn_sync.ini', '.')], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=None, noarchive=False) +a = Analysis(scripts=['gitutility\\__main__.py'], pathex=['gitutility', '.'], binaries=[], datas=[('git_svn_sync.ini', '.')], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=None, noarchive=False) pyz = PYZ(a.pure, a.zipped_data, cipher=None) exe = EXE(pyz, a.scripts, [], exclude_binaries=True, name='GitUtility', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, runtime_tmpdir=None, console=False, disable_windowed_traceback=False, target_arch=None, codesign_identity=None, entitlements_file=None, icon='GitUtility.ico') coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, upx_exclude=[], name='GitUtility') diff --git a/gitutility/async_tasks/async_workers.py b/gitutility/async_tasks/async_workers.py index cc0e054..698467b 100644 --- a/gitutility/async_tasks/async_workers.py +++ b/gitutility/async_tasks/async_workers.py @@ -2122,6 +2122,53 @@ def run_revert_to_tag_async( ) +def run_reset_to_commit_async( + action_handler: ActionHandler, + repo_path: str, + commit_hash: str, + results_queue: queue.Queue[Dict[str, Any]], +) -> None: + """Worker to perform a hard reset to a commit asynchronously.""" + func_name = "run_reset_to_commit_async" + log_handler.log_debug( + f"[Worker] Started: Reset to Commit '{commit_hash}' in '{repo_path}'", + func_name=func_name, + ) + result_payload: Dict[str, Any] = { + "status": "error", + "result": False, + "message": f"Failed to reset to commit '{commit_hash}'.", + "exception": None, + } + try: + success = action_handler.execute_reset_to_commit(repo_path, commit_hash) + result_payload["status"] = "success" + result_payload["result"] = success + result_payload["message"] = ( + f"Repository successfully reset to commit '{commit_hash[:7]}'." + ) + log_handler.log_info( + f"[Worker] {result_payload['message']}", func_name=func_name + ) + except (GitCommandError, ValueError, Exception) as e: + log_handler.log_exception( + f"[Worker] EXCEPTION resetting to commit: {e}", func_name=func_name + ) + result_payload["exception"] = e + result_payload["message"] = f"Error resetting to commit '{commit_hash[:7]}': {e}" + finally: + try: + results_queue.put(result_payload) + except Exception as qe: + log_handler.log_error( + f"[Worker] Failed to put result in queue for {func_name}: {qe}", + func_name=func_name, + ) + log_handler.log_debug( + f"[Worker] Finished: Reset to Commit '{commit_hash}'", func_name=func_name + ) + + def run_promote_branch_to_main_async( git_commands: GitCommands, repo_path: str, diff --git a/gitutility/core/action_handler.py b/gitutility/core/action_handler.py index 5a6b91e..28d1430 100644 --- a/gitutility/core/action_handler.py +++ b/gitutility/core/action_handler.py @@ -1695,5 +1695,60 @@ class ActionHandler: # Rilancia come un'eccezione generica o GitCommandError per coerenza raise Exception(f"Unexpected revert error: {e}") from e + def execute_reset_to_commit(self, repo_path: str, commit_hash: str) -> bool: + """ + Executes a hard reset to the specified commit and cleans the working directory. + This is a wrapper for a destructive operation. + + Args: + repo_path (str): The path to the repository. + commit_hash (str): The commit hash to reset to. + + Returns: + bool: True if the operation was successful. + + Raises: + ValueError: If inputs are invalid. + GitCommandError: If the underlying git commands fail. + """ + func_name: str = "execute_reset_to_commit" + log_handler.log_warning( + f"Executing destructive reset to commit '{commit_hash}' in repo: {repo_path}", + func_name=func_name, + ) + + # --- Validazione Input --- + if not repo_path or not os.path.isdir(repo_path): + raise ValueError(f"Invalid repository path provided: '{repo_path}'") + if not commit_hash or commit_hash.isspace(): + raise ValueError("Commit hash for reset cannot be empty.") + if not os.path.exists(os.path.join(repo_path, ".git")): + raise ValueError(f"Directory '{repo_path}' is not a valid Git repository.") + + try: + # Chiama il metodo di basso livello in GitCommands + self.git_commands.git_reset_hard(repo_path, commit_hash) + + log_handler.log_info( + f"Successfully reset repository to commit '{commit_hash}'.", + func_name=func_name, + ) + return True # L'operazione รจ andata a buon fine + + except (GitCommandError, ValueError) as e: + # Logga e rilancia l'eccezione per essere gestita dal worker + log_handler.log_error( + f"Failed to reset to commit '{commit_hash}': {e}", func_name=func_name + ) + raise e + except Exception as e: + # Cattura altri errori imprevisti + log_handler.log_exception( + f"An unexpected error occurred during reset to commit '{commit_hash}': {e}", + func_name=func_name, + ) + # Rilancia come un'eccezione generica o GitCommandError per coerenza + raise Exception(f"Unexpected reset error: {e}") from e + # --- END OF FILE gitsync_tool/core/action_handler.py ---