# --- FILE: gitsync_tool/logic/automation_handler.py --- from typing import TYPE_CHECKING from tkinter import simpledialog from gitutility.async_tasks import async_workers from gitutility.logging_setup import log_handler from gitutility.config.config_manager import DEFAULT_REMOTE_NAME from gitutility.gui.dialogs import SelectSubmoduleDialog # Forward reference for type hinting if TYPE_CHECKING: from ..app import GitSvnSyncApp class AutomationHandler: """ Handles logic for automated, multi-step tasks such as updating a wiki or cleaning repository history. """ def __init__(self, app: "GitSvnSyncApp"): """ Initializes the AutomationHandler. Args: app: The main application instance. """ self.app = app self.main_frame = app.main_frame self.wiki_updater = app.wiki_updater self.history_cleaner = app.history_cleaner def update_gitea_wiki(self): """Starts the asynchronous task to update the Gitea wiki.""" func_name = "update_gitea_wiki" log_handler.log_info(f"--- Action Triggered: Update Gitea Wiki ---", func_name=func_name) svn_path = self.app._get_and_validate_svn_path("Update Gitea Wiki") if not svn_path or not self.app._is_repo_ready(svn_path): self.main_frame.show_error("Action Failed", "Repository path is invalid or not prepared.") return remote_url = self.main_frame.remote_url_var.get().strip() if not remote_url: self.main_frame.show_error("Action Failed", "Remote URL is not configured in the current profile.") return if not self.main_frame.ask_yes_no( "Confirm Wiki Update", "Update Gitea Wiki pages from local 'doc/' folder?\n" "(This will clone the wiki, commit, and push changes.)" ): self.main_frame.update_status_bar("Wiki update cancelled.") return args = (self.wiki_updater, svn_path, remote_url) self.app._start_async_operation( worker_func=async_workers.run_update_wiki_async, args_tuple=args, context_dict={"context": "update_wiki", "status_msg": "Updating Gitea Wiki..."}, ) def analyze_and_clean_history(self): """Starts the analysis phase for cleaning the repository history.""" func_name = "analyze_and_clean_history" log_handler.log_info(f"--- Action Triggered: Analyze History for Cleaning ---", func_name=func_name) svn_path = self.app._get_and_validate_svn_path("Analyze History") if not svn_path or not self.app._is_repo_ready(svn_path): self.main_frame.show_error("Action Failed", "Repository path is invalid or not prepared.") return args = (self.history_cleaner, svn_path) self.app._start_async_operation( worker_func=async_workers.run_analyze_repo_for_purge_async, args_tuple=args, context_dict={ "context": "analyze_history", "status_msg": "Analyzing repository history...", "repo_path": svn_path, }, ) def clean_invalid_submodule(self): """Handles the workflow for cleaning an invalid submodule state.""" func_name = "clean_invalid_submodule" log_handler.log_info(f"--- Action Triggered: Clean Invalid Submodule ---", func_name=func_name) svn_path = self.app._get_and_validate_svn_path("Clean Submodule State") if not svn_path or not self.app._is_repo_ready(svn_path): self.main_frame.show_error("Action Failed", "Repository path is invalid or not prepared.") return try: # --- NUOVA LOGICA: Usa il metodo di scoperta potenziato --- discovered_paths = self.app.git_commands.discover_submodules_in_any_state(svn_path) if not discovered_paths: self.main_frame.show_info("No Submodules Found", "No submodule configurations were found in .gitmodules, .git/config, or the Git index.") return # Mostra la finestra di dialogo con la lista trovata dialog = SelectSubmoduleDialog(self.app.master, submodule_paths=discovered_paths) submodule_path = dialog.result # -------------------------------------------------- if not submodule_path: self.main_frame.update_status_bar("Clean submodule cancelled.") return if not self.main_frame.ask_yes_no( "Confirm Destructive Clean", f"This will forcibly remove all traces of '{submodule_path}' from the Git index and config, then create a commit.\n\n" "This is intended to fix a broken state. Proceed?" ): self.main_frame.update_status_bar("Clean submodule cancelled.") return args = (self.app.submodule_handler, svn_path, submodule_path) self.app._start_async_operation( worker_func=async_workers.run_force_clean_submodule_async, args_tuple=args, context_dict={ "context": "force_clean_submodule", "status_msg": f"Cleaning submodule '{submodule_path}'..." } ) except Exception as e: log_handler.log_exception(f"Error during submodule clean setup: {e}", func_name=func_name) self.main_frame.show_error("Error", f"Could not prepare submodule clean operation:\n{e}")