Class: Sxn::Commands::Worktrees

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sxn/commands/worktrees.rb

Overview

Manage git worktrees

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, local_options = {}, config = {}) ⇒ Worktrees

Returns a new instance of Worktrees.



11
12
13
14
15
16
17
18
19
20
# File 'lib/sxn/commands/worktrees.rb', line 11

def initialize(args = ARGV, local_options = {}, config = {})
  super
  @ui = Sxn::UI::Output.new
  @prompt = Sxn::UI::Prompt.new
  @table = Sxn::UI::Table.new
  @config_manager = Sxn::Core::ConfigManager.new
  @project_manager = Sxn::Core::ProjectManager.new(@config_manager)
  @session_manager = Sxn::Core::SessionManager.new(@config_manager)
  @worktree_manager = Sxn::Core::WorktreeManager.new(@config_manager, @session_manager)
end

Instance Method Details

#add(project_name = nil, branch = nil) ⇒ Object



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sxn/commands/worktrees.rb', line 46

def add(project_name = nil, branch = nil)
  ensure_initialized!

  # Interactive selection if project not provided
  if options[:interactive] || project_name.nil?
    project_name = select_project("Select project for worktree:")
    return if project_name.nil?
  end

  # Interactive branch selection if not provided and interactive mode
  # Note: If branch is nil, WorktreeManager will use session name as default
  if options[:interactive] && branch.nil?
    session_name = options[:session] || @config_manager.current_session
    branch = @prompt.branch_name("Enter branch name:", default: session_name)
  end

  session_name = options[:session] || @config_manager.current_session
  unless session_name
    @ui.error("No active session")
    @ui.recovery_suggestion("Use 'sxn use <session>' or specify --session")
    exit(1)
  end

  begin
    # Enable debug mode if verbose flag is set
    ENV["SXN_DEBUG"] = "true" if options[:verbose]

    @ui.progress_start("Creating worktree for #{project_name}")

    worktree = @worktree_manager.add_worktree(
      project_name,
      branch,
      session_name: session_name,
      verbose: options[:verbose]
    )

    @ui.progress_done
    @ui.success("Created worktree for #{project_name}")

    display_worktree_info(worktree)

    # Apply rules if requested
    apply_project_rules(project_name, session_name) if options[:apply_rules]
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)

    if options[:verbose] && e.respond_to?(:details)
      @ui.newline
      @ui.subsection("Debug Information")
      @ui.info(e.details)
    elsif !options[:verbose]
      @ui.recovery_suggestion("Run with --verbose flag for more details")
    end

    exit(e.exit_code)
  ensure
    ENV.delete("SXN_DEBUG") if options[:verbose]
  end
end

#listObject



169
170
171
172
173
174
175
176
177
# File 'lib/sxn/commands/worktrees.rb', line 169

def list
  ensure_initialized!

  if options[:all_sessions]
    list_all_worktrees
  else
    list_session_worktrees
  end
end

#remove(project_name = nil) ⇒ Object



111
112
113
114
115
116
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
# File 'lib/sxn/commands/worktrees.rb', line 111

def remove(project_name = nil)
  ensure_initialized!

  session_name = options[:session] || @config_manager.current_session
  unless session_name
    @ui.error("No active session")
    @ui.recovery_suggestion("Use 'sxn use <session>' or specify --session")
    exit(1)
  end

  # Interactive selection if project not provided
  if project_name.nil?
    worktrees = @worktree_manager.list_worktrees(session_name: session_name)
    if worktrees.empty?
      @ui.empty_state("No worktrees in current session")
      suggest_add_worktree
      return
    end

    choices = worktrees.map do |w|
      { name: "#{w[:project]} (#{w[:branch]})", value: w[:project] }
    end
    project_name = @prompt.select("Select worktree to remove:", choices)
  end

  # Check for uncommitted changes unless forced
  unless options[:force]
    worktree = @worktree_manager.get_worktree(project_name, session_name: session_name)
    if worktree && worktree[:status] != "clean" && !@prompt.ask_yes_no(
      "Worktree has uncommitted changes. Continue?", default: false
    )
      @ui.info("Cancelled")
      return
    end
  end

  unless @prompt.confirm_deletion(project_name, "worktree")
    @ui.info("Cancelled")
    return
  end

  begin
    @ui.progress_start("Removing worktree for #{project_name}")
    @worktree_manager.remove_worktree(project_name, session_name: session_name)
    @ui.progress_done
    @ui.success("Removed worktree for #{project_name}")
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#statusObject



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

def status
  ensure_initialized!

  session_name = options[:session] || @config_manager.current_session
  unless session_name
    @ui.error("No active session")
    exit(1)
  end

  begin
    worktrees = @worktree_manager.list_worktrees(session_name: session_name)

    @ui.section("Worktree Status - Session: #{session_name}")

    if worktrees.empty?
      @ui.empty_state("No worktrees in current session")
      suggest_add_worktree
    else
      display_worktree_status(worktrees)
    end
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#validate(project_name = nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/sxn/commands/worktrees.rb', line 182

def validate(project_name = nil)
  ensure_initialized!

  session_name = options[:session] || @config_manager.current_session
  unless session_name
    @ui.error("No active session")
    exit(1)
  end

  # Interactive selection if project not provided
  if project_name.nil?
    worktrees = @worktree_manager.list_worktrees(session_name: session_name)
    if worktrees.empty?
      @ui.empty_state("No worktrees in current session")
      return
    end

    choices = worktrees.map do |w|
      { name: "#{w[:project]} (#{w[:branch]})", value: w[:project] }
    end
    project_name = @prompt.select("Select worktree to validate:", choices)
  end

  begin
    result = @worktree_manager.validate_worktree(project_name, session_name: session_name)

    @ui.section("Worktree Validation: #{project_name}")

    if result[:valid]
      @ui.success("Worktree is valid")
    else
      @ui.error("Worktree has issues:")
      result[:issues].each { |issue| @ui.list_item(issue) }
    end

    display_worktree_info(result[:worktree], detailed: true) if result[:worktree]
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end