Class: Worktrees::Commands::Switch

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/worktrees/commands/switch.rb

Instance Method Summary collapse

Instance Method Details

#call(name:, **options) ⇒ Object



14
15
16
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
# File 'lib/worktrees/commands/switch.rb', line 14

def call(name:, **options)
  begin
    manager = WorktreeManager.new

    # Check if target worktree exists
    target_worktree = manager.find_worktree(name)
    unless target_worktree
      raise ValidationError, "Worktree '#{name}' not found"
    end

    # Check current state for warnings/blocking
    current = manager.current_worktree
    if current && current.dirty? && !options[:force]
      # Per requirements: allow switch with warning (not blocking)
      warn "Warning: Previous worktree '#{current.name}' has uncommitted changes"
    end

    # Perform the switch
    switched_worktree = manager.switch_to_worktree(name)

    puts "Switched to worktree: #{switched_worktree.name}"
    puts "  Path: #{switched_worktree.path}"
    puts "  Branch: #{switched_worktree.branch}"
    puts "  Status: #{switched_worktree.status}"

    # Show previous worktree warning if applicable
    if current && current.dirty?
      puts "\nWarning: Previous worktree '#{current.name}' has uncommitted changes"
    end

  rescue ValidationError => e
    warn "ERROR: Validation: #{e.message}"
    exit(2)
  rescue StateError => e
    warn "ERROR: State: #{e.message}"
    exit(3)
  rescue StandardError => e
    warn "ERROR: #{e.message}"
    exit(1)
  end
end