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(client_class = Clients::ClaudeClient) ⇒ 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
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. exit 1 end |
Instance Method Details
#build_commit_prompt ⇒ Object
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 = 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(client_class = Clients::ClaudeClient) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/committer/commit_generator.rb', line 57 def (client_class = Clients::ClaudeClient) client = client_class.new prompt = build_commit_prompt response = client.post(prompt) parse_response(response) end |
#template ⇒ Object
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 |