Class: Ace::Overseer::Organisms::PruneOrchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/overseer/organisms/prune_orchestrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(worktree_manager: nil, prune_checker: nil, tmux_executor: nil, config: nil, assignment_prune_checker: nil, assignment_manager: nil) ⇒ PruneOrchestrator

Returns a new instance of PruneOrchestrator.



7
8
9
10
11
12
13
14
15
# File 'lib/ace/overseer/organisms/prune_orchestrator.rb', line 7

def initialize(worktree_manager: nil, prune_checker: nil, tmux_executor: nil, config: nil,
  assignment_prune_checker: nil, assignment_manager: nil)
  @worktree_manager = worktree_manager || Ace::Git::Worktree::Organisms::WorktreeManager.new
  @prune_checker = prune_checker || Molecules::PruneSafetyChecker.new
  @tmux_executor = tmux_executor || Ace::Tmux::Molecules::TmuxExecutor.new
  @config = config || Ace::Overseer.config
  @assignment_prune_checker = assignment_prune_checker || Molecules::AssignmentPruneSafetyChecker.new
  @assignment_manager = assignment_manager || Ace::Assign::Molecules::AssignmentManager.new
end

Instance Method Details

#call(dry_run:, yes:, force: false, targets: [], assignment_id: nil, input: $stdin, output: $stdout, on_progress: nil) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/ace/overseer/organisms/prune_orchestrator.rb', line 17

def call(dry_run:, yes:, force: false, targets: [], assignment_id: nil,
  input: $stdin, output: $stdout, on_progress: nil)
  if assignment_id
    return prune_assignment(assignment_id: assignment_id, dry_run: dry_run,
      yes: yes, force: force, input: input, output: output,
      on_progress: on_progress)
  end

  progress = on_progress || ->(_msg) {}

  progress.call("Scanning worktrees...")
  (progress)
  result = @worktree_manager.list_all(show_tasks: true)
  raise Error, result[:error] || "Failed to list worktrees" unless result[:success]

  all_worktrees = Array(result[:worktrees]).reject(&:bare)
  worktrees = if targets.any?
    filter_by_targets(all_worktrees, targets)
  else
    all_worktrees.select(&:task_associated?)
  end

  progress.call("Checking #{worktrees.length} worktree(s)...")
  checked = worktrees.map { |worktree| check_candidate(worktree) }

  safe = checked.select(&:safe_to_prune?)
  unsafe = checked.reject(&:safe_to_prune?)
  forced = force ? unsafe : []

  if dry_run
    return {dry_run: true, safe: safe, unsafe: unsafe, forced: forced, pruned: [], failed: []}
  end

  print_candidates(safe, unsafe, force, output)

  to_prune = safe + forced
  unless yes
    output.print("Continue? [y/N] ")
    answer = input.gets.to_s.strip.downcase
    unless %w[y yes].include?(answer)
      return {dry_run: false, safe: safe, unsafe: unsafe, forced: forced, pruned: [], failed: [], aborted: true}
    end
  end

  pruned = []
  failed = []

  to_prune.each do |candidate|
    remove_result = @worktree_manager.remove(
      candidate.worktree_path,
      force: force,
      ignore_untracked: true,
      delete_branch: true
    )
    if remove_result[:success]
      close_tmux_window(candidate.worktree_path)
      pruned << candidate
    else
      failed << {candidate: candidate, error: remove_result[:error]}
    end
  end

  {
    dry_run: false,
    safe: safe,
    unsafe: unsafe,
    forced: forced,
    pruned: pruned,
    failed: failed,
    aborted: false
  }
end