Class: Worktrees::Commands::Create

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

Instance Method Summary collapse

Instance Method Details

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



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
# File 'lib/worktrees/commands/create.rb', line 17

def call(name:, base_ref: nil, **options)
  validate_arguments(name, base_ref)

  begin
    # Create manager with options
    config = Models::WorktreeConfig.load
    if options[:'worktrees-root']
      config = Models::WorktreeConfig.new(
        worktrees_root: options[:'worktrees-root'],
        default_base: config.default_base,
        force_cleanup: config.force_cleanup
      )
    end

    manager = WorktreeManager.new(nil, config)
    create_options = options.select { |k, _| i[force].include?(k) }

    worktree = manager.create_worktree(name, base_ref, create_options)

    # Display success message
    puts "Created worktree: #{worktree.name}"
    puts "  Path: #{worktree.path}"
    puts "  Branch: #{worktree.branch}"
    puts "  Base: #{worktree.base_ref}"
    puts "  Status: #{worktree.status}"

    # Switch to worktree if requested
    if options[:switch]
      manager.switch_to_worktree(name)
      puts "\nSwitched to worktree: #{name}"
    end

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