commit_ai 0.1.7 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/commit_ai/version.rb +1 -1
- data/lib/commit_ai.rb +76 -27
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 163dcacd836441ac91cfa6903ab7795889e9db51562cbc9055bc1cddc3998dca
|
4
|
+
data.tar.gz: 489a51e90bad0f1901cc751c95545e9dbb2491e72811f7773062ad8f86dd3cea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '010013338f7f4be32cb1a05920fa6749a953c9c15d29da3a396884baaf694ba6fd1456ab2ff9a75845f7a38938f5b0f4d58c7275a5f2074ebbe462bab3a4e22f'
|
7
|
+
data.tar.gz: b6686fd820b574f4c66458abe2686b9cc168dc4c13190c19035c938e87db91ffa4a3771bd7803e7dea23ea3741081ed059fd58b9c2019934da6581cde90bcf28
|
data/lib/commit_ai/version.rb
CHANGED
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 -U10 --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
|
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.
|
4
|
+
version: 0.1.9
|
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-
|
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
|
28
|
-
by analyzing staged git diffs with
|
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
|
69
|
+
based on staged git changes.
|
67
70
|
test_files: []
|