Skip to content

Commit 6d207e6

Browse files
authored
Merge pull request #8 from Abizrh/6-bug-prompt
6 bug prompt
2 parents 326a986 + 51215a6 commit 6d207e6

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

lua/commit-ai/commit.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ end
3434
local function generate_commit_suggestions(cb)
3535
local diff = Utils.get_git_diff()
3636
if not diff or diff == "" then
37-
print("No staged changes found")
3837
return {}
3938
end
4039

lua/commit-ai/prompt.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ Analyze the following Git diff and suggest commit messages.
88
Follow this format exactly:
99
- <icon> <prefix> (<scope>): <commit message>
1010
11-
Use one of the options below for <icon> and <prefix>, depending on the change type:
11+
Choose an appropriate <icon> and <prefix> from the list below based on the type of change:
1212
1313
%s
1414
15-
Rules:
15+
Commit rules:
1616
1. Only use the above icons and prefixes.
1717
2. Use "chore" for configuration files (*.yml, *.json, *.env), CI/CD changes, build systems, or general non-code tasks.
1818
3. Use "fix" for bug fixes.
1919
4. Use "feat" ONLY for user-facing features or newly introduced functionality.
2020
5. Use "docs" for documentation-only changes, including README.md and LICENSE.
2121
6. Use "refactor" for breaking internal code changes that don’t affect functionality.
2222
7. Use "enhance" for performance improvements or minor internal enhancements.
23-
8. The scope in parentheses should reflect the affected module or file group (e.g., doctor, config, auth).
24-
9. Keep commit message short and imperative (e.g., "add config for <context>
23+
8. The <scope> should desribe the affected module or file group or folder.
24+
9. Keep commit message should be concise and written in the imperative mood (e.g., "add config for <context>
25+
10. Only use lowercase letters in the commit message.
2526
2627
Git diff:
2728
%s

lua/commit-ai/utils.lua

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,63 @@ local log = require("commit-ai.log")
22
local M = {}
33

44
function M.get_git_diff()
5-
local staged_diff = vim.fn.system('git diff --cached --no-color')
6-
if staged_diff == "" then
7-
staged_diff = vim.fn.system('git diff --no-color')
8-
end
9-
staged_diff = staged_diff:gsub("^%s*(.-)%s*$", "%1") -- Remove trailing newlines
5+
local status_lines = vim.fn.systemlist('git status --porcelain')
6+
local untracked_files = vim.tbl_filter(function(line)
7+
return line:match("^%?%?")
8+
end, status_lines)
109

11-
-- Check if we're in a git repository
12-
if vim.fn.system('git rev-parse --is-inside-work-tree 2>/dev/null'):find("true") == nil then
13-
log.error("Not in a git repository")
14-
return nil
10+
if #untracked_files > 0 then
11+
for _, line in ipairs(untracked_files) do
12+
local file = line:sub(4)
13+
log.info("‼️ Untracked file: " .. file)
1514
end
16-
17-
-- Check if there are any changes
18-
if staged_diff == "" then
19-
log.warn("No changes detected")
20-
return nil
15+
local answer = vim.fn.input("Do you want to add them to the staging area? (y/n) ")
16+
if answer:lower() == "y" then
17+
for _, line in ipairs(untracked_files) do
18+
local file = line:sub(4)
19+
vim.fn.system("git add " .. file)
20+
log.info("📝 Added " .. file .. " to the staging area")
21+
end
22+
else
23+
log.error("Aborting")
24+
return nil
2125
end
26+
return nil
27+
end
28+
local staged_diff = vim.fn.system('git diff --cached --no-color')
29+
if staged_diff == "" then
30+
staged_diff = vim.fn.system('git diff --no-color')
31+
end
32+
staged_diff = staged_diff:gsub("^%s*(.-)%s*$", "%1") -- Remove trailing newlines
33+
34+
-- Check if we're in a git repository
35+
if vim.fn.system('git rev-parse --is-inside-work-tree 2>/dev/null'):find("true") == nil then
36+
log.error("Not in a git repository")
37+
return nil
38+
end
2239

23-
return staged_diff
40+
-- Check if there are any changes
41+
if staged_diff == "" then
42+
log.error("No staged changes found")
43+
return nil
44+
end
45+
46+
return staged_diff
2447
end
2548

2649
function M.get_api_key(env_var)
27-
local api_key
28-
if type(env_var) == 'function' then
29-
api_key = env_var()
30-
elseif type(env_var) == 'string' then
31-
api_key = vim.env[env_var]
32-
end
50+
local api_key
51+
if type(env_var) == 'function' then
52+
api_key = env_var()
53+
elseif type(env_var) == 'string' then
54+
api_key = vim.env[env_var]
55+
end
3356

34-
if type(api_key) ~= 'string' or api_key == '' then
35-
return nil
36-
end
57+
if type(api_key) ~= 'string' or api_key == '' then
58+
return nil
59+
end
3760

38-
return api_key
61+
return api_key
3962
end
4063

4164
return M

0 commit comments

Comments
 (0)