Class: Ace::Git::Worktree::Organisms::WorktreeManager
- Inherits:
-
Object
- Object
- Ace::Git::Worktree::Organisms::WorktreeManager
- Defined in:
- lib/ace/git/worktree/organisms/worktree_manager.rb
Overview
Worktree manager organism
High-level manager for all worktree operations, providing a unified interface for creating, listing, switching between, removing, and managing worktrees. Integrates both task-aware and traditional worktree operations.
Instance Method Summary collapse
-
#configuration ⇒ WorktreeConfig
Get configuration.
-
#create(branch_name, options = {}) ⇒ Hash
Create a traditional worktree (not task-aware).
-
#create_branch(branch_name, options = {}) ⇒ Hash
Create a worktree for a branch (local or remote).
-
#create_pr(evidence, checkout, options = {}) ⇒ Hash
Create a worktree for a Pull Request from normalized evidence.
-
#create_task(task_ref, options = {}) ⇒ Hash
Create a task-aware worktree.
-
#get_status ⇒ Hash
Get worktree status and statistics.
-
#initialize(config: nil, project_root: Dir.pwd) ⇒ WorktreeManager
constructor
Initialize a new WorktreeManager.
-
#list_all(options = {}) ⇒ Hash
List all worktrees.
-
#prune ⇒ Hash
Prune deleted worktrees.
-
#reload_configuration ⇒ WorktreeConfig
Reload configuration.
-
#remove(identifier, options = {}) ⇒ Hash
Remove a worktree.
-
#remove_task(task_ref, options = {}) ⇒ Hash
Remove a task worktree with full cleanup.
-
#search(query, options = {}) ⇒ Hash
Search for worktrees.
-
#switch(identifier) ⇒ Hash
Switch to a worktree.
-
#validate_configuration ⇒ Hash
Validate worktree configuration.
Constructor Details
#initialize(config: nil, project_root: Dir.pwd) ⇒ WorktreeManager
Initialize a new WorktreeManager
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 29 def initialize(config: nil, project_root: Dir.pwd) @project_root = project_root # Initialize molecules @config_loader = Molecules::ConfigLoader.new(project_root) @config = config || load_configuration @worktree_creator = Molecules::WorktreeCreator.new(config: @config) @worktree_lister = Molecules::WorktreeLister.new @worktree_remover = Molecules::WorktreeRemover.new @task_fetcher = Molecules::TaskFetcher.new # Initialize orchestrator @task_orchestrator = Organisms::TaskWorktreeOrchestrator.new(config: @config, project_root: project_root) end |
Instance Method Details
#configuration ⇒ WorktreeConfig
Get configuration
515 516 517 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 515 def configuration @config end |
#create(branch_name, options = {}) ⇒ Hash
Create a traditional worktree (not task-aware)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 54 def create(branch_name, = {}) # Validate inputs return error_result("Branch name is required") if branch_name.nil? || branch_name.empty? # Check if worktree already exists existing = @worktree_lister.find_by_branch(branch_name) if existing return error_result("Worktree already exists for branch: #{branch_name}") end # Handle dry run if [:dry_run] return dry_run_traditional_creation(branch_name, ) end # Create the worktree result = @worktree_creator.create_traditional( branch_name, [:path], git_root: @project_root, source: [:source] ) if result[:success] result[:message] = "Worktree created successfully" # Execute after-create hooks if configured hooks = @config.after_create_hooks if hooks && hooks.any? require_relative "../molecules/hook_executor" hook_executor = Molecules::HookExecutor.new hook_result = hook_executor.execute_hooks( hooks, worktree_path: result[:worktree_path], project_root: @project_root, task_data: nil # No task data for traditional worktrees ) if hook_result[:success] result[:hooks_results] = hook_result[:results] else # Hooks are non-blocking - failures become warnings result[:warnings] = hook_result[:errors] if hook_result[:errors]&.any? result[:hooks_results] = hook_result[:results] end end end result rescue => e error_result("Unexpected error: #{e.}") end |
#create_branch(branch_name, options = {}) ⇒ Hash
Create a worktree for a branch (local or remote)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 212 def create_branch(branch_name, = {}) return error_result("Branch name is required") if branch_name.nil? || branch_name.empty? # Check if worktree already exists for this branch # Extract just the branch name (remove remote prefix if present) local_branch_name = branch_name.include?("/") ? branch_name.split("/").last : branch_name existing = @worktree_lister.find_by_branch(local_branch_name) || @worktree_lister.find_by_branch(branch_name) if existing && ![:force] return error_result("Worktree already exists at: #{existing.path}") end # Handle dry run if [:dry_run] return dry_run_branch_creation(branch_name, ) end # Create the worktree result = @worktree_creator.create_for_branch( branch_name, @config, git_root: @project_root ) if result[:success] result[:message] = "Branch worktree created successfully" # Execute after-create hooks if configured hooks = @config.after_create_hooks if hooks && hooks.any? require_relative "../molecules/hook_executor" hook_executor = Molecules::HookExecutor.new hook_result = hook_executor.execute_hooks( hooks, worktree_path: result[:worktree_path], project_root: @project_root, task_data: nil ) if hook_result[:success] result[:hooks_results] = hook_result[:results] else result[:warnings] = hook_result[:errors] if hook_result[:errors]&.any? result[:hooks_results] = hook_result[:results] end end end result rescue => e error_result("Unexpected error: #{e.}") end |
#create_pr(evidence, checkout, options = {}) ⇒ Hash
Create a worktree for a Pull Request from normalized evidence.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 138 def create_pr(evidence, checkout, = {}) return error_result("PR evidence is required") if evidence.nil? pr_number = evidence.number pr_data = normalized_pr_data(evidence) local_branch = preview_pr_branch(pr_data) # Check if worktree already exists for this PR's branch existing = @worktree_lister.find_by_branch("pr-#{pr_number}") || @worktree_lister.find_by_branch(local_branch) || @worktree_lister.find_by_branch(evidence.head_ref) if existing && ![:force] return error_result("Worktree already exists at: #{existing.path}") end # Handle dry run if [:dry_run] return dry_run_pr_creation(pr_number, pr_data, ) end return error_result("Verified checkout is required for PR worktree creation") unless checkout # Create the worktree result = @worktree_creator.create_for_pr( pr_data, @config, git_root: @project_root, source_ref: checkout[:local_ref], remote_tracking: checkout[:remote_tracking] ) if result[:success] result[:pr_number] = pr_number result[:pr_title] = pr_data[:title] result[:message] = "PR worktree created successfully" # Execute after-create hooks if configured hooks = @config.after_create_hooks if hooks && hooks.any? require_relative "../molecules/hook_executor" hook_executor = Molecules::HookExecutor.new hook_result = hook_executor.execute_hooks( hooks, worktree_path: result[:worktree_path], project_root: @project_root, task_data: pr_data ) if hook_result[:success] result[:hooks_results] = hook_result[:results] else result[:warnings] = hook_result[:errors] if hook_result[:errors]&.any? result[:hooks_results] = hook_result[:results] end end end result rescue => e error_result("Unexpected error: #{e.}") end |
#create_task(task_ref, options = {}) ⇒ Hash
Create a task-aware worktree
116 117 118 119 120 121 122 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 116 def create_task(task_ref, = {}) if [:dry_run] @task_orchestrator.dry_run_create(task_ref, ) else @task_orchestrator.create_for_task(task_ref, ) end end |
#get_status ⇒ Hash
Get worktree status and statistics
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 443 def get_status # Get all worktrees with task associations worktrees = @worktree_lister.list_with_tasks stats = @worktree_lister.get_statistics # Get task worktree status task_status = @task_orchestrator.get_task_worktree_status result = { success: true, worktrees: worktrees, statistics: stats, configuration: @config.to_h } result[:task_status] = task_status[:status] if task_status[:success] result rescue => e error_result("Failed to get worktree status: #{e.}") end |
#list_all(options = {}) ⇒ Hash
List all worktrees
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 280 def list_all( = {}) task_filter_requested = ![:task_associated].nil? # Get worktrees worktrees = if [:show_tasks] || task_filter_requested @worktree_lister.list_with_tasks else @worktree_lister.list_all end # Apply filters if ![:task_associated].nil? || ![:usable].nil? || [:search] || [:task_id] || [:pr_number] worktrees = @worktree_lister.filter( worktrees, task_associated: [:task_associated], usable: [:usable], branch_pattern: [:search], task_id: [:task_id], pr_number: [:pr_number] ) end # Format output formatted_output = @worktree_lister.format_for_display( worktrees, [:format] || :table ) # Get statistics stats = @worktree_lister.get_statistics(worktrees) { success: true, worktrees: worktrees, formatted_output: formatted_output, statistics: stats, count: worktrees.length } rescue => e error_result("Failed to list worktrees: #{e.}") end |
#prune ⇒ Hash
Prune deleted worktrees
428 429 430 431 432 433 434 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 428 def prune result = @worktree_remover.prune result[:message] = "Worktree pruning completed successfully" if result[:success] result rescue => e error_result("Failed to prune worktrees: #{e.}") end |
#reload_configuration ⇒ WorktreeConfig
Reload configuration
522 523 524 525 526 527 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 522 def reload_configuration @config = load_configuration @config_loader.reset_cache! @task_orchestrator = Organisms::TaskWorktreeOrchestrator.new(config: @config, project_root: @project_root) @config end |
#remove(identifier, options = {}) ⇒ Hash
Remove a worktree
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 375 def remove(identifier, = {}) return error_result("Worktree identifier is required") if identifier.nil? || identifier.empty? # Find the worktree worktree = find_worktree_by_identifier(identifier) unless worktree # Worktree not found - check if we should try branch-only deletion if [:delete_branch] result = attempt_branch_only_deletion(identifier, [:force]) return result if result[:success] end return error_result("Worktree not found: #{identifier}") end # Remove the worktree result = @worktree_remover.remove( worktree.path, force: [:force], remove_directory: [:remove_directory] != false, delete_branch: [:delete_branch] == true, ignore_untracked: [:ignore_untracked] == true ) if result[:success] result[:message] = "Worktree removed successfully: #{worktree.description}" end result rescue => e error_result("Unexpected error: #{e.}") end |
#remove_task(task_ref, options = {}) ⇒ Hash
Remove a task worktree with full cleanup
417 418 419 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 417 def remove_task(task_ref, = {}) @task_orchestrator.remove_task_worktree(task_ref, ) end |
#search(query, options = {}) ⇒ Hash
Search for worktrees
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 471 def search(query, = {}) return error_result("Search query is required") if query.nil? || query.empty? search_in = [:search_in] || [:branch, :path, :task_id] worktrees = @worktree_lister.search(query, search_in: search_in) { success: true, query: query, search_in: search_in, results: worktrees, count: worktrees.length } rescue => e error_result("Search failed: #{e.}") end |
#switch(identifier) ⇒ Hash
Switch to a worktree
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 332 def switch(identifier) return error_result("Worktree identifier is required") if identifier.nil? || identifier.empty? # Try different ways to find the worktree worktree = find_worktree_by_identifier(identifier) return error_result("Worktree not found: #{identifier}") unless worktree # Check if worktree exists and is usable unless worktree.exists? return error_result("Worktree directory does not exist: #{worktree.path}") end unless worktree.usable? return error_result("Worktree is not usable: #{worktree.description}") end # Return the path for the caller to use { success: true, message: "Found worktree: #{worktree.description}", worktree_path: worktree.path, branch: worktree.branch, task_id: worktree.task_id, description: worktree.description } rescue => e error_result("Unexpected error: #{e.}") end |
#validate_configuration ⇒ Hash
Validate worktree configuration
499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/ace/git/worktree/organisms/worktree_manager.rb', line 499 def validate_configuration errors = @config.validate { success: errors.empty?, valid: errors.empty?, errors: errors, configuration: @config.to_h } rescue => e error_result("Configuration validation failed: #{e.}") end |