Skip to content

Commit e325c29

Browse files
committed
🐛 fix (commit): Handle empty diffs correctly
1 parent 326a986 commit e325c29

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
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/utils.lua

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ 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
10-
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
15-
end
16-
17-
-- Check if there are any changes
18-
if staged_diff == "" then
19-
log.warn("No changes detected")
20-
return nil
21-
end
22-
23-
return staged_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
10+
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
15+
end
16+
17+
-- Check if there are any changes
18+
if staged_diff == "" then
19+
log.error("No staged changes found")
20+
return nil
21+
end
22+
23+
return staged_diff
2424
end
2525

2626
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
33-
34-
if type(api_key) ~= 'string' or api_key == '' then
35-
return nil
36-
end
37-
38-
return api_key
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
33+
34+
if type(api_key) ~= 'string' or api_key == '' then
35+
return nil
36+
end
37+
38+
return api_key
3939
end
4040

4141
return M

0 commit comments

Comments
 (0)