@@ -133,22 +133,57 @@ function Git.is_amending()
133
133
return false
134
134
end
135
135
136
- -- Check if COMMIT_EDITMSG exists and we're in a rebase/merge state
137
- local git_dir = vim .fn .system (" git rev-parse --git-dir" ):gsub (" \n " , " " )
138
- if vim .v .shell_error ~= 0 then
139
- return false
140
- end
141
-
142
- -- Check for COMMIT_EDITMSG file which indicates we're editing a commit
143
- local commit_editmsg = git_dir .. " /COMMIT_EDITMSG"
144
- local stat = vim .uv .fs_stat (commit_editmsg )
145
- if not stat then
146
- return false
147
- end
148
-
149
- -- Additional check: see if we have HEAD commit (not initial commit)
150
- vim .fn .system (" git rev-parse --verify HEAD" )
151
- return vim .v .shell_error == 0
136
+ -- Check if we're in an amend scenario by examining the COMMIT_EDITMSG content
137
+ -- During amend, git pre-populates COMMIT_EDITMSG with the previous commit message
138
+ local git_dir = vim .trim (vim .fn .system (" git rev-parse --git-dir" ))
139
+ if vim .v .shell_error ~= 0 then
140
+ return false
141
+ end
142
+
143
+ -- Use platform-appropriate path separator
144
+ local path_sep = package.config :sub (1 , 1 )
145
+ local commit_editmsg = git_dir .. path_sep .. " COMMIT_EDITMSG"
146
+ local stat = vim .uv .fs_stat (commit_editmsg )
147
+ if not stat then
148
+ return false
149
+ end
150
+
151
+ -- Additional check: verify HEAD commit exists (not initial commit)
152
+ local redirect = (vim .uv .os_uname ().sysname == " Windows_NT" ) and " 2>nul" or " 2>/dev/null"
153
+ vim .fn .system (" git rev-parse --verify HEAD" .. redirect )
154
+ if vim .v .shell_error ~= 0 then
155
+ return false
156
+ end
157
+
158
+ -- Read COMMIT_EDITMSG content and check if it contains a previous commit message
159
+ -- During amend, git pre-populates this file with the previous commit message
160
+ local fd = vim .uv .fs_open (commit_editmsg , " r" , 438 )
161
+ if not fd then
162
+ return false
163
+ end
164
+
165
+ local content = vim .uv .fs_read (fd , stat .size , 0 )
166
+ vim .uv .fs_close (fd )
167
+
168
+ if not content then
169
+ return false
170
+ end
171
+
172
+ -- Check if there's non-comment content in COMMIT_EDITMSG
173
+ -- During amend, this indicates we're editing an existing commit
174
+ local lines = vim .split (content , " \n " )
175
+ local has_existing_message = false
176
+ for _ , line in ipairs (lines ) do
177
+ local trimmed = vim .trim (line )
178
+ -- Skip empty lines and comment lines (starting with #)
179
+ if trimmed ~= " " and not trimmed :match (" ^#" ) then
180
+ has_existing_message = true
181
+ break
182
+ end
183
+ end
184
+
185
+ -- If COMMIT_EDITMSG has existing content and HEAD exists, likely an amend
186
+ return has_existing_message
152
187
end )
153
188
154
189
return ok and result or false
0 commit comments