Module: Commiti::GroupEditor

Defined in:
lib/services/git/commit/group_editor.rb

Constant Summary collapse

HELP_TEXT =
<<~TEXT.freeze
  Select files by number, then choose where to move them.

  Examples:
    1,3,5     (select files 1, 3, 5)
    2-4       (select files 2 through 4)

  Targets:
    1..N move to an existing group
    n    new group
    a    auto-reassign (best matching group or new)
    c    cancel
TEXT

Class Method Summary collapse

Class Method Details

.edit(groups) ⇒ Object



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
# File 'lib/services/git/commit/group_editor.rb', line 19

def self.edit(groups)
  return groups if groups.empty?
  return groups unless Commiti::InteractivePrompt.ask_yes_no('Edit auto-split groups before committing?', default: :no)

  working = deep_copy(groups)
  chunk_map = build_chunk_map(working)
  all_paths = working.flat_map { |group| group[:files] }.uniq
  path_order = all_paths.each_with_index.to_h

  puts "\n#{Commiti::TerminalUI.panel('Group editor', HELP_TEXT)}\n"

  loop do
    index_map, indexed_groups = index_groups(working)
    print_groups(indexed_groups, total: working.length)

    input = Commiti::InteractivePrompt.ask_text('Move which files? (numbers, Enter to finish)')
    break if input.nil? || input.empty?

    selected_indices = parse_indices(input, max_index: index_map.length)
    if selected_indices.empty?
      puts Commiti::TerminalUI.status(:warn, 'No valid file numbers selected.')
      next
    end

    selected_paths = selected_indices.map { |index| index_map[index] }.compact
    target = Commiti::InteractivePrompt.ask_text(
      "Move to group [1-#{working.length}], n=new, a=auto, c=cancel"
    ).to_s.strip.downcase

    next if target.empty? || target == 'c'

    case target
    when 'a'
      auto_reassign(working, selected_paths, chunk_map)
    when 'n'
      move_to_new_group(working, selected_paths, chunk_map)
    else
      group_index = integer_or_nil(target)
      unless group_index && working[group_index - 1]
        puts Commiti::TerminalUI.status(:warn, "Group #{target} does not exist.")
        next
      end

      move_to_group(working, group_index, selected_paths)
    end

    normalize_groups(working, chunk_map, path_order)
  end

  normalize_groups(working, chunk_map, path_order)
end