Class: Committer::CommitGenerator
- Inherits:
-
Object
- Object
- Committer::CommitGenerator
- Defined in:
- lib/committer/commit_generator.rb
Instance Attribute Summary collapse
-
#commit_context ⇒ Object
readonly
Returns the value of attribute commit_context.
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
Class Method Summary collapse
Instance Method Summary collapse
- #build_commit_prompt ⇒ Object
-
#initialize(diff, commit_context = nil) ⇒ CommitGenerator
constructor
A new instance of CommitGenerator.
- #parse_response(response) ⇒ Object
- #prepare_commit_message ⇒ Object
- #template ⇒ Object
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_context ⇒ Object (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 |
#diff ⇒ Object (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_status ⇒ Object
32 33 34 35 36 37 |
# File 'lib/committer/commit_generator.rb', line 32 def self.check_git_status Committer::GitHelper.staged_diff rescue Committer::Error => e puts e. exit 1 end |
Instance Method Details
#build_commit_prompt ⇒ Object
18 19 20 21 22 |
# File 'lib/committer/commit_generator.rb', line 18 def build_commit_prompt format(template, diff: @diff, commit_context: @commit_context) end |
#parse_response(response) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/committer/commit_generator.rb', line 39 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 = text.split("\n\n", 2) summary = [0].strip body = [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 ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/committer/commit_generator.rb', line 58 def client = Clients::ClaudeClient.new prompt = build_commit_prompt response = client.post(prompt) parse_response(response) end |
#template ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/committer/commit_generator.rb', line 24 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 |