@@ -2,40 +2,63 @@ local log = require("commit-ai.log")
2
2
local M = {}
3
3
4
4
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 )
10
9
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 )
15
14
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
21
25
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
22
39
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
24
47
end
25
48
26
49
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
33
56
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
37
60
38
- return api_key
61
+ return api_key
39
62
end
40
63
41
64
return M
0 commit comments