Module: Commiti::CommitExecution

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

Class Method Summary collapse

Class Method Details

.maybe_commit(initial_message, run_stage:, print_message:) ⇒ Object



5
6
7
8
9
10
11
12
13
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
# File 'lib/services/git/commit/commit_execution.rb', line 5

def self.maybe_commit(initial_message, run_stage:, print_message:)
  working_message = initial_message

  loop do
    action = Commiti::InteractivePrompt.ask_commit_action

    case action
    when :yes
      errors = Commiti::InteractivePrompt.commit_message_errors(working_message)
      unless errors.empty?
        puts "\n#{Commiti::TerminalUI.status(:warn, 'Current message needs fixes before commit:')}"
        errors.each { |error| puts "- #{error}" }

        if Commiti::InteractivePrompt.ask_yes_no('Open editor to fix now?', default: :yes)
          edited = edit_message_until_valid(working_message)
          if edited.nil?
            puts "\n#{Commiti::TerminalUI.status(:fail, 'Editor did not exit successfully. Commit skipped.')}\n\n"
            return :skipped
          end

          working_message = edited
          print_message.call(working_message)
          next
        end

        puts "\n#{Commiti::TerminalUI.status(:warn, 'Commit skipped.')}\n\n"
        return :skipped
      end

      output = run_stage.call('Writing commit') { Commiti::GitWriter.commit_with_message_file(working_message) }
      puts output unless output.to_s.strip.empty?
      puts "\n#{Commiti::TerminalUI.status(:success, 'Commit created.')}\n\n"
      return :committed
    when :edit
      edited = edit_message_until_valid(working_message)
      if edited.nil?
        puts "\n#{Commiti::TerminalUI.status(:fail, 'Editor did not exit successfully.')}\n\n"
        next
      end

      working_message = edited
      print_message.call(working_message)
    else
      puts "\n#{Commiti::TerminalUI.status(:warn, 'Commit skipped.')}\n\n"
      return :skipped
    end
  end
end