commit_ai 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2cbf31db80a94d89561081d2e035165821e0bfd7851f318c132900e9fcaad09
4
- data.tar.gz: 571299591221bbf68455e1d5be5985797d6550d0aebfb0bdfc99427ebbeda914
3
+ metadata.gz: 43859139f57b17b863cc8b3f5a0ac1821bcde304f91071219b6ecef84b5ca3ff
4
+ data.tar.gz: fc3870e5b895d0a060bd39bd97f4eb6cabb32fe1ce30b39062dc3accc186e67a
5
5
  SHA512:
6
- metadata.gz: 0d56d24e2629d896a83fc637128c04dca3fd0044e1b84ac34a4edc28a1b230c90280530324f0c980cfbc54b8c080c2e980cb933eaa2dab32e4bc0cf10bb7f1e0
7
- data.tar.gz: 5d778db83320f38a912a984325777e27bab837a94deb0595dfe2cf3ebd57a38342501002deef4743a9452fc05252947f2269692394bea211201d8748de85415d
6
+ metadata.gz: f37fdd6fcf6420ef418cbf74832935e2c93409553d892e7a072c3521fa4fbae621ec786ff3d07c736aea064e87a5816b70e09359f4b28a81eeec78ead6898727
7
+ data.tar.gz: 3a7ede99534471bb07fc363c550c56ea917fd73b6d9251c670335327c63b99648b8cb521e4c4043e0bc5a231fb30252f8fcc063a84e80e7a373996e038d1b2ee
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CommitAi
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
data/lib/commit_ai.rb CHANGED
@@ -5,42 +5,25 @@ class CommitAI
5
5
  @client = OpenAI::Client.new(access_token: ENV['OPENAI_ACCESS_TOKEN'])
6
6
  end
7
7
 
8
- def generate_commit_message(diff)
9
- prompt = <<~PROMPT
10
- This is the git diff: #{diff}
11
-
12
- Please generate a **single-line** commit message that is concise and follows best practices.
13
- **Do not include any extra text or formatting** like triple backticks (```) or code block delimiters.
14
- Only return the commit message.
15
- PROMPT
16
-
17
- response = @client.chat(
18
- parameters: {
19
- model: "gpt-3.5-turbo",
20
- messages: [
21
- { role: "system", content: "You are a git commit message generator for ruby on rails projects." },
22
- { role: "user", content: prompt }
23
- ],
24
- max_tokens: 50,
25
- temperature: 0.5
26
- }
27
- )
28
-
29
- response.dig("choices", 0, "message", "content").strip
30
- end
31
-
32
8
  def execute
33
9
  system("git add --all")
34
- diff = `git diff --staged`
10
+ diff = `git diff -U99999 --staged`
35
11
 
36
12
  if diff.empty?
37
13
  puts "No changes to commit."
38
14
  return
39
15
  end
40
16
 
17
+ puts "Please provide a brief description of the change made (optional): "
18
+ user_description = STDIN.gets.chomp
19
+
20
+ puts "Do you want a multi-line commit message? (y/n) (optional): "
21
+ message_style_input = STDIN.gets.chomp.downcase
22
+ message_style = message_style_input == 'y' ? 'multi' : 'single'
23
+
41
24
  loop do
42
- commit_message = generate_commit_message(diff)
43
- puts "Generated Commit Message: #{commit_message}"
25
+ commit_message = generate_commit_message(diff, message_style, user_description)
26
+ puts "Generated Commit Message:\n#{commit_message}"
44
27
  print "Do you want to proceed with the commit? (y/n), regenerate (r), or edit (e): "
45
28
  response = STDIN.gets.chomp
46
29
  case response.downcase
@@ -58,4 +41,70 @@ class CommitAI
58
41
  end
59
42
  end
60
43
  end
44
+
45
+ private
46
+
47
+ def generate_commit_message(diff, message_style, user_description)
48
+ user_description_content = user_description.empty? ? '' : "Here is the user description of the change: \"#{user_description}\""
49
+ prompt = if message_style == 'single'
50
+ <<~PROMPT
51
+ This is the git diff: #{diff}
52
+
53
+ A git diff shows the changes made in a commit, where lines prefixed with a '+' indicate additions and lines prefixed with a '-' indicate deletions.
54
+
55
+ #{user_description_content}
56
+
57
+ Please generate a **single-line** commit message that is concise and follows best practices based on both the diff and user description.
58
+ **Do not include any extra text or formatting** like triple backticks (```) or code block delimiters.
59
+ Only return the commit message.
60
+ PROMPT
61
+ else
62
+ <<~PROMPT
63
+ This is the git diff: #{diff}
64
+
65
+ A git diff shows the changes made in a commit, where lines prefixed with a '+' indicate additions and lines prefixed with a '-' indicate deletions.
66
+
67
+ #{user_description_content}
68
+
69
+ Please generate a **multi-line** commit message that is clear, concise, and follows best practices based on both the diff and user description.
70
+ The first line should be a short summary of the change.
71
+ The second line should be blank.
72
+ The following lines should provide additional details about the change and its purpose.
73
+ PROMPT
74
+ end
75
+
76
+ response = @client.chat(
77
+ parameters: {
78
+ model: "gpt-3.5-turbo",
79
+ messages: [
80
+ { role: "system", content: "You are a highly experienced senior software engineer specializing in Ruby on Rails projects. Your only task is to generate clear and concise git commit messages following best practices." },
81
+ { role: "user", content: prompt }
82
+ ],
83
+ max_tokens: 150,
84
+ temperature: 0.5
85
+ }
86
+ )
87
+
88
+ response.dig("choices", 0, "message", "content").strip
89
+ end
90
+
91
+ def minify_diff(diff, context_lines = 3)
92
+ minified_diff = []
93
+ current_context = []
94
+
95
+ diff.lines.each do |line|
96
+ if line.start_with?('+', '-')
97
+ # Add the surrounding context before and after changes
98
+ minified_diff.concat(current_context.last(context_lines))
99
+ minified_diff << line
100
+ current_context.clear # Clear after using context lines
101
+ else
102
+ # Only keep a limited number of context lines
103
+ current_context << line unless line.strip.empty?
104
+ current_context = current_context.last(context_lines) # Trim extra lines
105
+ end
106
+ end
107
+
108
+ minified_diff.join
109
+ end
61
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commit_ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Vitug
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-04 00:00:00.000000000 Z
11
+ date: 2024-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -24,8 +24,11 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Commit AI automates the generation of insightful one-liner commit messages
28
- by analyzing staged git diffs with the help of OpenAI's advanced language models.
27
+ description: Commit AI automates the generation of insightful single-line and multi-line
28
+ commit messages by analyzing staged git diffs with OpenAI's advanced language models.
29
+ It allows users to provide optional change descriptions for better context, and
30
+ smartly minifies the git diff to focus on the essential changes, ensuring clarity
31
+ and best practices.
29
32
  email:
30
33
  - ken@narralabs.com
31
34
  executables:
@@ -63,5 +66,5 @@ rubygems_version: 3.5.3
63
66
  signing_key:
64
67
  specification_version: 4
65
68
  summary: A Ruby gem that utilizes OpenAI to create concise and meaningful commit messages
66
- based on the differences in your git changes.
69
+ based on staged git changes.
67
70
  test_files: []