Class: Ace::Git::Worktree::Commands::SwitchCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/worktree/commands/switch_command.rb

Overview

Switch command

Switches to a worktree by various identifiers (task ID, branch name, directory name, or path). Outputs the path for navigation.

Examples:

Switch by task ID

SwitchCommand.new.run(["081"])

Switch by branch name

SwitchCommand.new.run(["feature-branch"])

Switch and change directory

cd $(ace-git-worktree switch 081)

Instance Method Summary collapse

Constructor Details

#initialize(manager: nil) ⇒ SwitchCommand

Initialize a new SwitchCommand

Parameters:

  • manager (Object) (defaults to: nil)

    Optional manager dependency for testing



24
25
26
# File 'lib/ace/git/worktree/commands/switch_command.rb', line 24

def initialize(manager: nil)
  @manager = manager || Organisms::WorktreeManager.new
end

Instance Method Details

#run(args = []) ⇒ Integer

Run the switch command

Parameters:

  • args (Array<String>) (defaults to: [])

    Command arguments

Returns:

  • (Integer)

    Exit code (0 for success, 1 for error)



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
# File 'lib/ace/git/worktree/commands/switch_command.rb', line 32

def run(args = [])
  options = parse_arguments(args)
  return show_help if options[:help]

  validate_options(options)

  # Handle list option
  if options[:list]
    result = @manager.list_all(format: :simple)
    if result[:success] && result[:worktrees].any?
      result[:worktrees].each do |worktree|
        prefix = worktree.task_associated? ? "Task #{worktree.task_id}: " : ""
        puts "  #{prefix}#{worktree.branch || "detached"} (#{worktree.path})"
      end
      return 0
    else
      puts "No worktrees found. Use 'ace-git-worktree create' to create one."
      return 0
    end
  end

  result = @manager.switch(options[:identifier])

  if result[:success]
    display_switch_result(result, options)
    0
  else
    puts "Failed to switch worktree: #{result[:error]}"
    display_alternatives(options[:identifier]) unless result[:error].include?("not found")
    1
  end
rescue ArgumentError => e
  puts "Error: #{e.message}"
  puts
  show_help
  1
rescue => e
  puts "Error: #{e.message}"
  1
end

#show_helpInteger

Show help for the switch command

Returns:

  • (Integer)

    Exit code



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ace/git/worktree/commands/switch_command.rb', line 76

def show_help
  puts <<~HELP
    ace-git-worktree switch - Switch to a worktree

    USAGE:
        ace-git-worktree switch <identifier>

    IDENTIFIERS:
        Task ID:                081, task.081, v.0.9.0+081
        Branch name:            feature-branch, main
        Directory name:        task.081, feature-branch
        Full path:              /path/to/worktree

    OPTIONS:
        --help, -h              Show this help message
        --list, -l              List available worktrees
        --verbose, -v           Show detailed information

    EXAMPLES:
        # Switch by task ID
        ace-git-worktree switch 081

        # Switch by branch name
        ace-git-worktree switch feature-branch

        # Switch and change directory
        cd $(ace-git-worktree switch 081)

        # List available worktrees
        ace-git-worktree switch --list

    OUTPUT:
        The command outputs the worktree path for use with cd:
        $ ace-git-worktree switch 081
        /project/.ace-wt/task.081

        To change directory:
        $ cd $(ace-git-worktree switch 081)

    CONFIGURATION:
        Worktree paths and naming are controlled by .ace/git/worktree.yml
  HELP
  0
end