hunkify 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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +75 -0
- data/bin/hunkify +6 -0
- data/lib/hunkify/anthropic_api.rb +102 -0
- data/lib/hunkify/cli.rb +136 -0
- data/lib/hunkify/color.rb +24 -0
- data/lib/hunkify/diff_parser.rb +71 -0
- data/lib/hunkify/git.rb +74 -0
- data/lib/hunkify/hunk.rb +18 -0
- data/lib/hunkify/ui.rb +76 -0
- data/lib/hunkify/version.rb +5 -0
- data/lib/hunkify.rb +13 -0
- metadata +55 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 462d60419d0723593916ed35414e3f70ba61df1284f13aeaf640d7d45e0aaed3
|
|
4
|
+
data.tar.gz: 720035e85d1c6f7b8770a361e0e2a6cde7a98b6e9eeb3e1fffbe95054bfda91f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7a51c9e785a105b186221212c7eb5a5111a2da5a58462cb7359bedd8a3312ebb9f34f4f73dfd1ee0e244ffe01f7f772d4501b1439daf772443a0e0b8df445e5e
|
|
7
|
+
data.tar.gz: 6f0fafc113ff3398179b232a198372e0fcd4249bac99981e5dedf704a638adb3472d58b43f02c7825cfbd95e6d0ba3f33f07dcbe76c042cf8a6a3d47ff769e03
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tom SCHIAVI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# hunkify
|
|
2
|
+
|
|
3
|
+
Split your staged changes into atomic, well-scoped commits — powered by Claude.
|
|
4
|
+
|
|
5
|
+
Instead of cramming everything into a single `git commit -m "wip"`, `hunkify` parses your staged diff hunk by hunk, asks Claude to group them by intent, and produces conventional commits (with gitmojis) ready to apply.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Hunk-level analysis** — each change block is treated independently, so one file can span multiple commits if needed.
|
|
10
|
+
- **AI-powered grouping** via Claude Haiku 4.5 — fast, cheap, accurate.
|
|
11
|
+
- **Gitmoji + conventional commits** — `:sparkles: feat(scope): …`, `:bug: fix(scope): …`, etc.
|
|
12
|
+
- **Interactive review** — confirm, edit, or skip each proposed commit before it's created.
|
|
13
|
+
- **Free-form context argument** — pass a ticket ID, feature name, or any directive to steer the output.
|
|
14
|
+
- **Safe rollback** — if anything fails mid-run, your original staged state is restored.
|
|
15
|
+
|
|
16
|
+
## Requirements
|
|
17
|
+
|
|
18
|
+
- Ruby ≥ 2.7
|
|
19
|
+
- An [Anthropic API key](https://console.anthropic.com/)
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gem install hunkify
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Or add to your `Gemfile`:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem "hunkify"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Setup
|
|
34
|
+
|
|
35
|
+
Export your Anthropic API key (add to your `~/.zshrc` or `~/.bashrc`):
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
git add .
|
|
45
|
+
hunkify
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
With a context hint (ticket ID, feature name, or any free-form directive):
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
hunkify my_ticket_id
|
|
52
|
+
hunkify "user onboarding"
|
|
53
|
+
hunkify "focus on refactoring"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Workflow
|
|
57
|
+
|
|
58
|
+
1. `hunkify` parses your staged diff into hunks.
|
|
59
|
+
2. Claude proposes a grouping into logical commits.
|
|
60
|
+
3. For each proposed commit, you can **confirm**, **edit the message**, **skip**, or **quit**.
|
|
61
|
+
4. After a final confirmation, the plan is applied — each commit is created via `git apply --cached`.
|
|
62
|
+
|
|
63
|
+
If anything goes wrong during apply, the original staged state is restored.
|
|
64
|
+
|
|
65
|
+
## Debug
|
|
66
|
+
|
|
67
|
+
Set `HUNKIFY_DEBUG=1` to print the raw AI response and the patches being applied:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
HUNKIFY_DEBUG=1 hunkify
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
data/bin/hunkify
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Hunkify
|
|
8
|
+
module AnthropicAPI
|
|
9
|
+
API_URL = "https://api.anthropic.com/v1/messages"
|
|
10
|
+
MODEL = "claude-haiku-4-5-20251001"
|
|
11
|
+
|
|
12
|
+
SYSTEM_PROMPT = <<~PROMPT
|
|
13
|
+
You are a Git expert. You are given a list of hunks (blocks of modifications)
|
|
14
|
+
extracted from a git diff. Your job is to group them into coherent logical commits.
|
|
15
|
+
|
|
16
|
+
GROUPING RULES:
|
|
17
|
+
- One commit = one unique intent (feat, fix, refactor, style, etc.)
|
|
18
|
+
- Hunks in different files CAN belong to the same commit if they serve the same intent
|
|
19
|
+
- Hunks in the SAME file can belong to DIFFERENT commits if they are semantically distinct
|
|
20
|
+
- Prefer atomic and independent commits
|
|
21
|
+
|
|
22
|
+
RESPONSE FORMAT (strict JSON, no surrounding text):
|
|
23
|
+
{
|
|
24
|
+
"commits": [
|
|
25
|
+
{
|
|
26
|
+
"message": ":sparkles: feat(scope): description in English",
|
|
27
|
+
"hunk_ids": [1, 3, 5],
|
|
28
|
+
"reasoning": "brief explanation of the grouping"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"message": ":bug: fix(scope): description in English",
|
|
32
|
+
"hunk_ids": [2, 4],
|
|
33
|
+
"reasoning": "brief explanation"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
AVAILABLE GITMOJIS:
|
|
39
|
+
:sparkles: feat | :bug: fix | :recycle: refactor | :lipstick: style
|
|
40
|
+
:white_check_mark: test | :memo: docs | :wrench: config | :package: build
|
|
41
|
+
:zap: perf | :lock: security | :fire: remove | :art: format
|
|
42
|
+
:construction: wip | :card_file_box: db | :green_heart: ci | :rocket: deploy
|
|
43
|
+
|
|
44
|
+
MESSAGE RULES:
|
|
45
|
+
- In English, imperative, no leading capital, no trailing period
|
|
46
|
+
- Max 72 characters
|
|
47
|
+
- Scope = module / component / main file concerned
|
|
48
|
+
- If a user context is provided, use it as a hint to steer scope, wording,
|
|
49
|
+
or grouping. It may be a ticket ID (include it as the scope, e.g. feat(EA4-370): ...),
|
|
50
|
+
a feature name, or a free-form directive.
|
|
51
|
+
|
|
52
|
+
RESPOND ONLY WITH THE JSON. No markdown, no explanation.
|
|
53
|
+
PROMPT
|
|
54
|
+
|
|
55
|
+
def self.group_hunks(hunks, context: nil)
|
|
56
|
+
api_key = ENV["ANTHROPIC_API_KEY"]
|
|
57
|
+
raise "ANTHROPIC_API_KEY missing! Add it to your ~/.zshrc or ~/.bashrc" if api_key.nil? || api_key.empty?
|
|
58
|
+
|
|
59
|
+
user_ctx = context && !context.empty? ? "\nUser context: #{context}" : ""
|
|
60
|
+
hunks_summary = hunks.map(&:to_summary).join("\n\n---\n\n")
|
|
61
|
+
user_message = "#{user_ctx}\n\nHere are the hunks to group:\n\n#{hunks_summary}"
|
|
62
|
+
|
|
63
|
+
uri = URI(API_URL)
|
|
64
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
65
|
+
http.use_ssl = true
|
|
66
|
+
http.read_timeout = 30
|
|
67
|
+
|
|
68
|
+
request = Net::HTTP::Post.new(uri.path)
|
|
69
|
+
request["Content-Type"] = "application/json"
|
|
70
|
+
request["x-api-key"] = api_key
|
|
71
|
+
request["anthropic-version"] = "2023-06-01"
|
|
72
|
+
request.body = JSON.generate({
|
|
73
|
+
model: MODEL,
|
|
74
|
+
max_tokens: 1024,
|
|
75
|
+
system: SYSTEM_PROMPT,
|
|
76
|
+
messages: [{role: "user", content: user_message}]
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
response = http.request(request)
|
|
80
|
+
body = JSON.parse(response.body)
|
|
81
|
+
|
|
82
|
+
raise "API Error #{response.code}: #{body["error"]&.dig("message")}" unless response.code == "200"
|
|
83
|
+
|
|
84
|
+
raw = body.dig("content", 0, "text")&.strip
|
|
85
|
+
|
|
86
|
+
if ENV["HUNKIFY_DEBUG"]
|
|
87
|
+
warn "\n--- RAW AI RESPONSE ---\n#{raw}\n-----------------------\n"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
cleaned = raw
|
|
91
|
+
.gsub(/\A```(?:json)?\s*/i, "")
|
|
92
|
+
.gsub(/\s*```\z/, "")
|
|
93
|
+
.strip
|
|
94
|
+
|
|
95
|
+
if (match = cleaned.match(/(\{.+\})/m))
|
|
96
|
+
cleaned = match[1]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
JSON.parse(cleaned)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/hunkify/cli.rb
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "color"
|
|
4
|
+
require_relative "diff_parser"
|
|
5
|
+
require_relative "git"
|
|
6
|
+
require_relative "anthropic_api"
|
|
7
|
+
require_relative "ui"
|
|
8
|
+
|
|
9
|
+
module Hunkify
|
|
10
|
+
module CLI
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def run(argv)
|
|
14
|
+
context = argv[0]
|
|
15
|
+
|
|
16
|
+
UI.print_header
|
|
17
|
+
|
|
18
|
+
begin
|
|
19
|
+
Git.root
|
|
20
|
+
rescue RuntimeError
|
|
21
|
+
puts Color.red(" ✗ This directory is not a Git repository.")
|
|
22
|
+
exit 1
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
unless Git.has_staged_changes?
|
|
26
|
+
puts Color.yellow(" ⚠️ No staged changes.")
|
|
27
|
+
puts Color.dim(" Run first: git add <files>")
|
|
28
|
+
exit 0
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
original_staged_files = Git.staged_files
|
|
32
|
+
|
|
33
|
+
puts Color.dim(" Parsing diff...")
|
|
34
|
+
raw_diff = Git.staged_diff
|
|
35
|
+
hunks = DiffParser.parse(raw_diff)
|
|
36
|
+
|
|
37
|
+
if hunks.empty?
|
|
38
|
+
puts Color.yellow(" ⚠️ No hunks found in staged diff.")
|
|
39
|
+
exit 0
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
UI.print_hunks_overview(hunks)
|
|
43
|
+
|
|
44
|
+
hunks_by_id = hunks.each_with_object({}) { |h, acc| acc[h.id] = h }
|
|
45
|
+
|
|
46
|
+
puts Color.dim(" Analyzing and grouping via Claude #{AnthropicAPI::MODEL}...")
|
|
47
|
+
puts
|
|
48
|
+
|
|
49
|
+
begin
|
|
50
|
+
result = AnthropicAPI.group_hunks(hunks, context: context)
|
|
51
|
+
rescue JSON::ParserError
|
|
52
|
+
puts Color.red(" ✗ Invalid AI response (malformed JSON). Try again.")
|
|
53
|
+
exit 1
|
|
54
|
+
rescue RuntimeError => e
|
|
55
|
+
puts Color.red(" ✗ #{e.message}")
|
|
56
|
+
exit 1
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
commits_data = result["commits"]
|
|
60
|
+
|
|
61
|
+
if commits_data.nil? || commits_data.empty?
|
|
62
|
+
puts Color.red(" ✗ The AI returned no commits.")
|
|
63
|
+
exit 1
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
commits_data.each do |c|
|
|
67
|
+
c["hunk_ids"] = c["hunk_ids"].select { |id| hunks_by_id.key?(id) }
|
|
68
|
+
end
|
|
69
|
+
commits_data.reject! { |c| c["hunk_ids"].empty? }
|
|
70
|
+
|
|
71
|
+
UI.print_grouping(commits_data, hunks_by_id)
|
|
72
|
+
|
|
73
|
+
plan = []
|
|
74
|
+
commits_data.each_with_index do |c, i|
|
|
75
|
+
puts Color.bold(" Commit #{i + 1}/#{commits_data.size}:")
|
|
76
|
+
msg = UI.prompt_edit_commit(c, i + 1, commits_data.size)
|
|
77
|
+
plan << [msg, c["hunk_ids"]] if msg
|
|
78
|
+
puts
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
if plan.empty?
|
|
82
|
+
puts Color.yellow(" No commit selected.")
|
|
83
|
+
exit 0
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
UI.confirm_plan(plan)
|
|
87
|
+
|
|
88
|
+
execute_commits(plan, hunks_by_id, original_staged_files)
|
|
89
|
+
|
|
90
|
+
puts
|
|
91
|
+
puts Color.bold(Color.green(" ✅ #{plan.size} commit(s) successfully created!"))
|
|
92
|
+
puts
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def execute_commits(plan, hunks_by_id, original_staged_files)
|
|
96
|
+
puts
|
|
97
|
+
total = plan.size
|
|
98
|
+
|
|
99
|
+
Git.unstage_all
|
|
100
|
+
|
|
101
|
+
plan.each_with_index do |(message, hunk_ids), i|
|
|
102
|
+
print " #{Color.dim("Commit #{i + 1}/#{total}...")} "
|
|
103
|
+
|
|
104
|
+
hunks_by_file = hunk_ids
|
|
105
|
+
.map { |id| hunks_by_id[id] }
|
|
106
|
+
.group_by(&:file_path)
|
|
107
|
+
|
|
108
|
+
patch = hunks_by_file.map do |_file_path, file_hunks|
|
|
109
|
+
first = file_hunks.first
|
|
110
|
+
hunk_bodies = file_hunks.map do |h|
|
|
111
|
+
body = h.lines.join("\n")
|
|
112
|
+
body += "\n" unless body.end_with?("\n")
|
|
113
|
+
"#{h.hunk_header}\n#{body}"
|
|
114
|
+
end.join
|
|
115
|
+
"#{first.file_header}\n#{hunk_bodies}"
|
|
116
|
+
end.join("\n")
|
|
117
|
+
|
|
118
|
+
if ENV["HUNKIFY_DEBUG"]
|
|
119
|
+
warn "\n--- PATCH ---\n#{patch}\n-------------\n"
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
begin
|
|
123
|
+
Git.apply_patch_to_index(patch)
|
|
124
|
+
Git.commit(message)
|
|
125
|
+
puts Color.green("✓ #{message}")
|
|
126
|
+
rescue RuntimeError => e
|
|
127
|
+
puts Color.red("✗ #{e.message}")
|
|
128
|
+
puts Color.yellow(" ⚠️ Rollback: restaging original files...")
|
|
129
|
+
Git.unstage_all
|
|
130
|
+
Git.restage_files(original_staged_files)
|
|
131
|
+
raise
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Hunkify
|
|
4
|
+
module Color
|
|
5
|
+
RESET = "\e[0m"
|
|
6
|
+
BOLD = "\e[1m"
|
|
7
|
+
DIM = "\e[2m"
|
|
8
|
+
CYAN = "\e[36m"
|
|
9
|
+
GREEN = "\e[32m"
|
|
10
|
+
YELLOW = "\e[33m"
|
|
11
|
+
RED = "\e[31m"
|
|
12
|
+
MAGENTA = "\e[35m"
|
|
13
|
+
BLUE = "\e[34m"
|
|
14
|
+
|
|
15
|
+
def self.cyan(s) = "#{CYAN}#{s}#{RESET}"
|
|
16
|
+
def self.green(s) = "#{GREEN}#{s}#{RESET}"
|
|
17
|
+
def self.yellow(s) = "#{YELLOW}#{s}#{RESET}"
|
|
18
|
+
def self.red(s) = "#{RED}#{s}#{RESET}"
|
|
19
|
+
def self.bold(s) = "#{BOLD}#{s}#{RESET}"
|
|
20
|
+
def self.dim(s) = "#{DIM}#{s}#{RESET}"
|
|
21
|
+
def self.magenta(s) = "#{MAGENTA}#{s}#{RESET}"
|
|
22
|
+
def self.blue(s) = "#{BLUE}#{s}#{RESET}"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "hunk"
|
|
4
|
+
|
|
5
|
+
module Hunkify
|
|
6
|
+
module DiffParser
|
|
7
|
+
def self.parse(raw_diff)
|
|
8
|
+
hunks = []
|
|
9
|
+
hunk_id = 0
|
|
10
|
+
file_header = nil
|
|
11
|
+
file_path = nil
|
|
12
|
+
hunk_header = nil
|
|
13
|
+
hunk_lines = []
|
|
14
|
+
|
|
15
|
+
raw_diff.each_line do |line|
|
|
16
|
+
line = line.chomp
|
|
17
|
+
line = " " if line.empty? && hunk_header
|
|
18
|
+
|
|
19
|
+
if line.start_with?("diff --git")
|
|
20
|
+
if hunk_header && !hunk_lines.empty?
|
|
21
|
+
hunk_id += 1
|
|
22
|
+
hunks << Hunk.new(
|
|
23
|
+
id: hunk_id,
|
|
24
|
+
file_header: file_header,
|
|
25
|
+
file_path: file_path,
|
|
26
|
+
hunk_header: hunk_header,
|
|
27
|
+
lines: hunk_lines.dup
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
hunk_header = nil
|
|
31
|
+
hunk_lines = []
|
|
32
|
+
file_header = line
|
|
33
|
+
file_path = line.split(" b/").last
|
|
34
|
+
|
|
35
|
+
elsif line.start_with?("--- ", "+++ ", "index ", "new file", "deleted file", "old mode", "new mode", "rename ")
|
|
36
|
+
file_header = "#{file_header}\n#{line}"
|
|
37
|
+
|
|
38
|
+
elsif line.start_with?("@@")
|
|
39
|
+
if hunk_header && !hunk_lines.empty?
|
|
40
|
+
hunk_id += 1
|
|
41
|
+
hunks << Hunk.new(
|
|
42
|
+
id: hunk_id,
|
|
43
|
+
file_header: file_header,
|
|
44
|
+
file_path: file_path,
|
|
45
|
+
hunk_header: hunk_header,
|
|
46
|
+
lines: hunk_lines.dup
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
hunk_header = line
|
|
50
|
+
hunk_lines = []
|
|
51
|
+
|
|
52
|
+
elsif hunk_header
|
|
53
|
+
hunk_lines << line
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
if hunk_header && !hunk_lines.empty?
|
|
58
|
+
hunk_id += 1
|
|
59
|
+
hunks << Hunk.new(
|
|
60
|
+
id: hunk_id,
|
|
61
|
+
file_header: file_header,
|
|
62
|
+
file_path: file_path,
|
|
63
|
+
hunk_header: hunk_header,
|
|
64
|
+
lines: hunk_lines.dup
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
hunks
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/hunkify/git.rb
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "tempfile"
|
|
5
|
+
|
|
6
|
+
module Hunkify
|
|
7
|
+
module Git
|
|
8
|
+
def self.run(cmd)
|
|
9
|
+
stdout, stderr, status = Open3.capture3(cmd)
|
|
10
|
+
raise "Git error: #{stderr.strip}" unless status.success?
|
|
11
|
+
stdout.strip
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.staged_diff
|
|
15
|
+
run("git diff --staged")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.staged_files
|
|
19
|
+
run("git diff --staged --name-only").split("\n")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.has_staged_changes?
|
|
23
|
+
!staged_files.empty?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.root
|
|
27
|
+
run("git rev-parse --show-toplevel")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.apply_patch_to_index(patch_content)
|
|
31
|
+
Tempfile.create(["hunkify_patch", ".patch"]) do |f|
|
|
32
|
+
f.write(patch_content)
|
|
33
|
+
f.flush
|
|
34
|
+
stdout, stderr, status = Open3.capture3("git", "apply", "--cached", "--whitespace=nowarn", f.path)
|
|
35
|
+
raise "Git error: #{stderr.strip}" unless status.success?
|
|
36
|
+
stdout.strip
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.unstage_all
|
|
41
|
+
if head_exists?
|
|
42
|
+
run("git reset HEAD")
|
|
43
|
+
else
|
|
44
|
+
stdout, _stderr, status = Open3.capture3("git", "ls-files", "-z", "--cached")
|
|
45
|
+
raise "Git error: unable to list index" unless status.success?
|
|
46
|
+
stdout.split("\0").reject(&:empty?).each do |path|
|
|
47
|
+
_o, err, st = Open3.capture3("git", "rm", "--cached", "-f", "--", path)
|
|
48
|
+
raise "Git error: #{err.strip}" unless st.success?
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.head_exists?
|
|
54
|
+
_o, _e, status = Open3.capture3("git", "rev-parse", "--verify", "HEAD")
|
|
55
|
+
status.success?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.commit(message)
|
|
59
|
+
stdout, stderr, status = Open3.capture3("git", "commit", "-m", message)
|
|
60
|
+
raise "Git error: #{stderr.strip}" unless status.success?
|
|
61
|
+
stdout.strip
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.restage_files(files)
|
|
65
|
+
files.each { |f|
|
|
66
|
+
begin
|
|
67
|
+
run("git add \"#{f}\"")
|
|
68
|
+
rescue
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
data/lib/hunkify/hunk.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Hunkify
|
|
4
|
+
Hunk = Struct.new(:id, :file_header, :file_path, :hunk_header, :lines, keyword_init: true) do
|
|
5
|
+
def to_patch
|
|
6
|
+
body = lines.join("\n")
|
|
7
|
+
body += "\n" unless body.end_with?("\n")
|
|
8
|
+
"#{file_header}\n#{hunk_header}\n#{body}"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def to_summary
|
|
12
|
+
added = lines.count { |l| l.start_with?("+") }
|
|
13
|
+
removed = lines.count { |l| l.start_with?("-") }
|
|
14
|
+
preview = lines.first(6).join("\n")
|
|
15
|
+
"[HUNK #{id}] #{file_path} #{hunk_header}\n#{preview}\n(+#{added} / -#{removed} lines)"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/hunkify/ui.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "color"
|
|
4
|
+
|
|
5
|
+
module Hunkify
|
|
6
|
+
module UI
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def print_header
|
|
10
|
+
version = defined?(Hunkify::VERSION) ? Hunkify::VERSION : ""
|
|
11
|
+
puts
|
|
12
|
+
puts " #{Color.magenta("▲")} #{Color.bold("hunkify")} #{Color.dim("v#{version}")}"
|
|
13
|
+
puts " #{Color.dim("─" * 40)}"
|
|
14
|
+
puts " #{Color.dim("Atomic commits, grouped by Claude.")}"
|
|
15
|
+
puts
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def print_hunks_overview(hunks)
|
|
19
|
+
puts Color.bold("🔍 #{hunks.size} hunk(s) detected:")
|
|
20
|
+
hunks.each do |h|
|
|
21
|
+
added = h.lines.count { |l| l.start_with?("+") }
|
|
22
|
+
removed = h.lines.count { |l| l.start_with?("-") }
|
|
23
|
+
puts " #{Color.dim("[#{h.id}]")} #{Color.cyan(h.file_path)} #{Color.green("+#{added}")} #{Color.red("-#{removed}")}"
|
|
24
|
+
end
|
|
25
|
+
puts
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def print_grouping(commits_data, hunks_by_id)
|
|
29
|
+
puts Color.bold("🤖 AI-proposed grouping:")
|
|
30
|
+
puts
|
|
31
|
+
|
|
32
|
+
commits_data.each_with_index do |c, i|
|
|
33
|
+
hunk_list = c["hunk_ids"].map { |id| Color.dim("[#{id}]") }.join(" ")
|
|
34
|
+
files = c["hunk_ids"].map { |id| hunks_by_id[id]&.file_path }.compact.uniq
|
|
35
|
+
|
|
36
|
+
puts " #{Color.bold(Color.blue("Commit #{i + 1}"))} #{hunk_list}"
|
|
37
|
+
puts " #{Color.green(c["message"])}"
|
|
38
|
+
puts " #{Color.dim("↳ " + c["reasoning"])}"
|
|
39
|
+
puts " #{Color.dim("Files: " + files.join(", "))}"
|
|
40
|
+
puts
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def prompt_edit_commit(commit_data, _index, _total)
|
|
45
|
+
print " #{Color.yellow("[Enter]")} Confirm #{Color.yellow("[e]")} Edit message #{Color.yellow("[s]")} Skip #{Color.yellow("[q]")} Quit: "
|
|
46
|
+
input = $stdin.gets.chomp.downcase
|
|
47
|
+
|
|
48
|
+
case input
|
|
49
|
+
when ""
|
|
50
|
+
commit_data["message"]
|
|
51
|
+
when "e"
|
|
52
|
+
print " ✏️ New message: "
|
|
53
|
+
new_msg = $stdin.gets.chomp
|
|
54
|
+
new_msg.empty? ? commit_data["message"] : new_msg
|
|
55
|
+
when "s"
|
|
56
|
+
nil
|
|
57
|
+
when "q"
|
|
58
|
+
puts Color.red("\n Cancelled.")
|
|
59
|
+
exit 0
|
|
60
|
+
else
|
|
61
|
+
commit_data["message"]
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def confirm_plan(plan)
|
|
66
|
+
puts Color.bold("📋 Final plan:")
|
|
67
|
+
plan.each_with_index do |(msg, _), i|
|
|
68
|
+
puts " #{Color.dim("#{i + 1}.")} #{Color.green(msg)}"
|
|
69
|
+
end
|
|
70
|
+
puts
|
|
71
|
+
print " #{Color.yellow("[Enter]")} Run #{Color.yellow("[q]")} Cancel: "
|
|
72
|
+
input = $stdin.gets.chomp.downcase
|
|
73
|
+
exit 0 if input == "q"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/hunkify.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "hunkify/version"
|
|
4
|
+
require_relative "hunkify/color"
|
|
5
|
+
require_relative "hunkify/hunk"
|
|
6
|
+
require_relative "hunkify/diff_parser"
|
|
7
|
+
require_relative "hunkify/git"
|
|
8
|
+
require_relative "hunkify/anthropic_api"
|
|
9
|
+
require_relative "hunkify/ui"
|
|
10
|
+
require_relative "hunkify/cli"
|
|
11
|
+
|
|
12
|
+
module Hunkify
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hunkify
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tom SCHIAVI
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: hunkify analyzes staged hunks, asks Claude to group them into logical
|
|
13
|
+
commits, and applies them via git apply --cached.
|
|
14
|
+
executables:
|
|
15
|
+
- hunkify
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- bin/hunkify
|
|
22
|
+
- lib/hunkify.rb
|
|
23
|
+
- lib/hunkify/anthropic_api.rb
|
|
24
|
+
- lib/hunkify/cli.rb
|
|
25
|
+
- lib/hunkify/color.rb
|
|
26
|
+
- lib/hunkify/diff_parser.rb
|
|
27
|
+
- lib/hunkify/git.rb
|
|
28
|
+
- lib/hunkify/hunk.rb
|
|
29
|
+
- lib/hunkify/ui.rb
|
|
30
|
+
- lib/hunkify/version.rb
|
|
31
|
+
homepage: https://github.com/tomschiavi/smartcommit
|
|
32
|
+
licenses:
|
|
33
|
+
- MIT
|
|
34
|
+
metadata:
|
|
35
|
+
source_code_uri: https://github.com/tomschiavi/smartcommit
|
|
36
|
+
bug_tracker_uri: https://github.com/tomschiavi/smartcommit/issues
|
|
37
|
+
changelog_uri: https://github.com/tomschiavi/smartcommit/releases
|
|
38
|
+
rdoc_options: []
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '2.7'
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
51
|
+
requirements: []
|
|
52
|
+
rubygems_version: 4.0.8
|
|
53
|
+
specification_version: 4
|
|
54
|
+
summary: Split staged changes into atomic commits using Claude.
|
|
55
|
+
test_files: []
|