Class: Ace::Git::Worktree::Molecules::WorktreeCreator
- Inherits:
-
Object
- Object
- Ace::Git::Worktree::Molecules::WorktreeCreator
- Defined in:
- lib/ace/git/worktree/molecules/worktree_creator.rb
Overview
Worktree creator molecule
Creates git worktrees with proper validation, naming, and error handling. Integrates with git commands and provides task-aware creation capabilities.
Constant Summary collapse
- DEFAULT_TIMEOUT =
Default timeout for git commands
60
Instance Method Summary collapse
-
#create_for_branch(branch_name, config, git_root: nil) ⇒ Hash
Create a worktree for a specific branch (local or remote).
-
#create_for_pr(pr_data, config, git_root: nil) ⇒ Hash
Create a worktree for a Pull Request.
-
#create_for_task(task_data, config, counter: nil, git_root: nil, source: nil, target_branch: nil) ⇒ Hash
Create a worktree for a specific task.
-
#create_traditional(branch_name, worktree_path = nil, git_root: nil, source: nil) ⇒ Hash
Create a traditional worktree (not task-aware).
-
#generate_unique_path(task_data, config, git_root) ⇒ String
Generate a unique worktree path for a task (handles conflicts).
-
#initialize(config: nil, timeout: DEFAULT_TIMEOUT) ⇒ WorktreeCreator
constructor
Initialize a new WorktreeCreator.
-
#validate_worktree_path(worktree_path, git_root) ⇒ Hash
Validate a worktree path for creation.
-
#worktree_exists?(task_data: nil, branch_name: nil, worktree_path: nil) ⇒ WorktreeInfo?
Check if a worktree already exists for the given criteria.
Constructor Details
#initialize(config: nil, timeout: DEFAULT_TIMEOUT) ⇒ WorktreeCreator
Initialize a new WorktreeCreator
30 31 32 33 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 30 def initialize(config: nil, timeout: DEFAULT_TIMEOUT) @config = config @timeout = timeout end |
Instance Method Details
#create_for_branch(branch_name, config, git_root: nil) ⇒ Hash
Create a worktree for a specific branch (local or remote)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 189 def create_for_branch(branch_name, config, git_root: nil) return error_result("Branch name is required") if branch_name.nil? || branch_name.empty? return error_result("Configuration is required") unless config begin # Determine git repository root git_root ||= detect_git_root return error_result("Not in a git repository") unless git_root # Detect if this is a remote branch remote_info = detect_remote_branch(branch_name) if remote_info # Remote branch - create with tracking create_for_remote_branch(branch_name, remote_info, config, git_root) else # Local branch - create without tracking create_for_local_branch(branch_name, config, git_root) end rescue => e error_result("Unexpected error: #{e.}") end end |
#create_for_pr(pr_data, config, git_root: nil) ⇒ Hash
Create a worktree for a Pull Request
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 117 def create_for_pr(pr_data, config, git_root: nil) return error_result("PR data is required") unless pr_data return error_result("Configuration is required") unless config begin # Determine git repository root git_root ||= detect_git_root return error_result("Not in a git repository") unless git_root # Get PR-specific configuration (fallback to defaults) pr_config = config.pr_config || {} remote_name = pr_config[:remote_name] || "origin" directory_format = pr_config[:directory_format] || "ace-pr-{number}" branch_format = pr_config[:branch_format] || "pr-{number}-{slug}" # Format directory and branch names directory_name = format_pr_name(directory_format, pr_data) local_branch_name = format_pr_name(branch_format, pr_data) # Build full path worktree_path = File.join(config.absolute_root_path, directory_name) # Validate worktree path validation = validate_worktree_path(worktree_path, git_root) return error_result(validation[:error]) unless validation[:valid] # Fetch the remote branch head_branch = pr_data[:head_branch] fetch_result = fetch_remote_branch(remote_name, head_branch, git_root) return error_result(fetch_result[:error]) unless fetch_result[:success] # Create worktree with remote tracking result = create_worktree_with_tracking( worktree_path, local_branch_name, "#{remote_name}/#{head_branch}", git_root, configure_push: config.configure_push_for_mismatch? ) return result unless result[:success] # Success - return worktree information { success: true, worktree_path: worktree_path, branch: local_branch_name, tracking: "#{remote_name}/#{head_branch}", directory_name: directory_name, pr_number: pr_data[:number], pr_title: pr_data[:title], git_root: git_root, error: nil } rescue => e error_result("Unexpected error: #{e.}") end end |
#create_for_task(task_data, config, counter: nil, git_root: nil, source: nil, target_branch: nil) ⇒ Hash
Create a worktree for a specific task
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 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 60 def create_for_task(task_data, config, counter: nil, git_root: nil, source: nil, target_branch: nil) return error_result("Task data is required") unless task_data return error_result("Configuration is required") unless config begin # Determine git repository root git_root ||= detect_git_root return error_result("Not in a git repository") unless git_root # Generate names based on configuration directory_name = config.format_directory(task_data, counter) branch_name = config.format_branch(task_data) # Build full path worktree_path = File.join(config.absolute_root_path, directory_name) # Ensure parent directory exists before validation # (PathExpander rejects paths whose parent doesn't exist yet) parent_dir = File.dirname(worktree_path) FileUtils.mkdir_p(parent_dir) unless File.exist?(parent_dir) # Validate worktree path validation = validate_worktree_path(worktree_path, git_root) return error_result(validation[:error]) unless validation[:valid] # Create the worktree with source as start-point result = create_worktree(worktree_path, branch_name, git_root, start_point: source) return result unless result[:success] # Success - return worktree information { success: true, worktree_path: worktree_path, branch: branch_name, start_point: result[:start_point], directory_name: directory_name, task_id: extract_task_id_from_data(task_data), git_root: git_root, target_branch: target_branch, error: nil } rescue => e error_result("Unexpected error: #{e.}") end end |
#create_traditional(branch_name, worktree_path = nil, git_root: nil, source: nil) ⇒ Hash
Create a traditional worktree (not task-aware)
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 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 227 def create_traditional(branch_name, worktree_path = nil, git_root: nil, source: nil) return error_result("Branch name is required") if branch_name.nil? || branch_name.empty? begin # Determine git repository root git_root ||= detect_git_root return error_result("Not in a git repository") unless git_root # Auto-generate worktree path if not provided if worktree_path.nil? worktree_path = generate_default_worktree_path(branch_name, git_root) end # Validate worktree path validation = validate_worktree_path(worktree_path, git_root) return error_result(validation[:error]) unless validation[:valid] # Validate branch name return error_result("Invalid branch name") unless valid_branch_name?(branch_name) # Check if branch already exists (locally or remotely) if branch_exists?(branch_name) # Branch exists - create worktree for existing branch create_worktree_for_existing_branch(worktree_path, branch_name, git_root) else # Branch doesn't exist - create new branch with worktree create_worktree(worktree_path, branch_name, git_root, start_point: source) end rescue => e error_result("Unexpected error: #{e.}") end end |
#generate_unique_path(task_data, config, git_root) ⇒ String
Generate a unique worktree path for a task (handles conflicts)
308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 308 def generate_unique_path(task_data, config, git_root) counter = 1 loop do directory_name = config.format_directory(task_data, (counter > 1) ? counter : nil) worktree_path = File.join(config.absolute_root_path, directory_name) # Check if path already exists existing = worktree_exists?(worktree_path: worktree_path) break worktree_path unless existing counter += 1 end end |
#validate_worktree_path(worktree_path, git_root) ⇒ Hash
Validate a worktree path for creation
331 332 333 334 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 331 def validate_worktree_path(worktree_path, git_root) require_relative "../atoms/path_expander" Atoms::PathExpander.validate_for_worktree(worktree_path, git_root) end |
#worktree_exists?(task_data: nil, branch_name: nil, worktree_path: nil) ⇒ WorktreeInfo?
Check if a worktree already exists for the given criteria
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/ace/git/worktree/molecules/worktree_creator.rb', line 270 def worktree_exists?(task_data: nil, branch_name: nil, worktree_path: nil) require_relative "worktree_lister" lister = WorktreeLister.new worktrees = lister.list_all # Check by task ID if task_data task_id = extract_task_id_from_data(task_data) existing = Models::WorktreeInfo.find_by_task_id(worktrees, task_id) return existing if existing end # Check by branch name if branch_name existing = Models::WorktreeInfo.find_by_branch(worktrees, branch_name) return existing if existing end # Check by path if worktree_path = File.(worktree_path) existing = worktrees.find { |wt| File.(wt.path) == } return existing if existing end nil end |