autogitc 0.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/autogitc +5 -0
  3. data/lib/autogitc.rb +75 -0
  4. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbf55c47faf6c3a5236a2754de61ff38566d82904ec026d78842d395a2c6b7cf
4
+ data.tar.gz: 74056aabb123af82cd1d6d84af9e19837962c4a3c88c62812efb12f6bd76081a
5
+ SHA512:
6
+ metadata.gz: 4ffc02e86519074b7512080a1da198a305227e23fc6a40debac52f94394d8a7cbb85a5bdf3fc70f3067244164072ccbe57c8574eebabaa5560ae3b4204f0186d
7
+ data.tar.gz: ba19f2c91933b36726029b9bcb06aa3a1187673692f0a1946dd66265b80d31263107320732b1a8e222178636c85741b27e91012b0e46bd16db95f77836612fde
data/bin/autogitc ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rake'
3
+ load 'Rakefile'
4
+
5
+ Rake::Task['autogitc:run'].invoke
data/lib/autogitc.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ module Autogitc
6
+ def self.openai_api_key
7
+ ENV['AUTOGITC_KEY']
8
+ end
9
+
10
+ def self.get_staged_files
11
+ `git diff --name-only --staged`.split("\n")
12
+ end
13
+
14
+ def self.get_file_diff(file_path)
15
+ `git diff --staged #{file_path}`
16
+ end
17
+
18
+ def self.generate_commit_message(changes_summary)
19
+ uri = URI('https://api.openai.com/v1/chat/completions')
20
+
21
+ http = Net::HTTP.new(uri.host, uri.port)
22
+ http.use_ssl = true
23
+
24
+ request = Net::HTTP::Post.new(
25
+ uri.path, {
26
+ 'Content-Type' => 'application/json',
27
+ 'Authorization' => "Bearer #{openai_api_key}"
28
+ }
29
+ )
30
+
31
+ request.body = {
32
+ model: 'gpt-4o-mini',
33
+ messages: [
34
+ {
35
+ role: 'system',
36
+ content: 'You are a helpful assistant that generates concise and semantic git commit messages.'
37
+ },
38
+ {
39
+ role: 'user',
40
+ content: "Generate a conventional git commit message limited to 72 characters for the following changes in a JSON format with a key 'commit_message':\n\n#{changes_summary}"
41
+ }
42
+ ],
43
+ response_format: { "type": 'json_object' }
44
+ }.to_json
45
+
46
+ response = http.request(request)
47
+ result = JSON.parse(response.body)
48
+ output_text = result.dig('choices', 0, 'message', 'content')
49
+ JSON.parse(output_text)['commit_message']
50
+ end
51
+
52
+ def self.main
53
+ if openai_api_key.nil?
54
+ puts 'No AUTOGITC_KEY key found in environment variables.'
55
+ return
56
+ end
57
+
58
+ staged_files = get_staged_files
59
+
60
+ if staged_files.empty?
61
+ puts 'No files are staged for commit.'
62
+ return
63
+ end
64
+
65
+ changes_summary = staged_files.map do |file|
66
+ diff = get_file_diff(file)
67
+ "Changes in #{file}:\n#{diff}\n"
68
+ end.join("\n")
69
+
70
+ commit_message = generate_commit_message(changes_summary)
71
+ puts "Generated commit message:\n#{commit_message}"
72
+
73
+ `git commit -m "#{commit_message}"`
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autogitc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Goh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem simplifies the commit process by automatically generating descriptive
14
+ commit messages based on the files that have been added to the Git staging area.
15
+ It leverages LLM to analyze the changes and create meaningful commit messages, helping
16
+ you maintain a clean and organized commit history.
17
+ email: hello@adriangohjw.com
18
+ executables:
19
+ - autogitc
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/autogitc
24
+ - lib/autogitc.rb
25
+ homepage: https://rubygems.org/gems/autogitc
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.4.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Auto-generate Git commit messages with LLM
48
+ test_files: []