Class: Committer::CommitGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/committer/commit_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff, commit_context = nil) ⇒ CommitGenerator

Returns a new instance of CommitGenerator.



13
14
15
16
# File 'lib/committer/commit_generator.rb', line 13

def initialize(diff, commit_context = nil)
  @diff = diff
  @commit_context = commit_context
end

Instance Attribute Details

#commit_contextObject (readonly)

Returns the value of attribute commit_context.



11
12
13
# File 'lib/committer/commit_generator.rb', line 11

def commit_context
  @commit_context
end

#diffObject (readonly)

Returns the value of attribute diff.



11
12
13
# File 'lib/committer/commit_generator.rb', line 11

def diff
  @diff
end

Class Method Details

.check_git_statusObject



31
32
33
34
35
36
# File 'lib/committer/commit_generator.rb', line 31

def self.check_git_status
  Committer::GitHelper.staged_diff
rescue Committer::Error => e
  puts e.message
  exit 1
end

Instance Method Details

#build_commit_promptObject



18
19
20
21
# File 'lib/committer/commit_generator.rb', line 18

def build_commit_prompt
  scopes = Committer::Config::Accessor.instance[:scopes] || []
  Committer::PromptTemplates.build_prompt(diff, scopes, commit_context)
end

#parse_response(response) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/committer/commit_generator.rb', line 38

def parse_response(response)
  text = response.dig('content', 0, 'text')

  # If user didn't provide context, response should only be a summary line
  if @commit_context.nil? || @commit_context.empty?
    { summary: text.strip, body: nil }
  else
    # Split the response into summary and body
    message_parts = text.split("\n\n", 2)
    summary = message_parts[0].strip
    body = message_parts[1]&.strip

    # Wrap body text at 80 characters
    body = body.gsub(/(.{1,80})(\s+|$)/, "\\1\n").strip if body

    { summary: summary, body: body }
  end
end

#prepare_commit_message(client_class = Clients::ClaudeClient) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/committer/commit_generator.rb', line 57

def prepare_commit_message(client_class = Clients::ClaudeClient)
  client = client_class.new

  prompt = build_commit_prompt
  response = client.post(prompt)
  parse_response(response)
end

#templateObject



23
24
25
26
27
28
29
# File 'lib/committer/commit_generator.rb', line 23

def template
  if @commit_context.nil? || @commit_context.empty?
    Committer::PromptTemplates.build_prompt_summary_only
  else
    Committer::PromptTemplates.build_prompt_summary_and_body
  end
end