Skip to content

Commit 5be1eee

Browse files
committed
Fix first commit in preview
1 parent 500be1a commit 5be1eee

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

lua/advanced_git_search/git_utils.lua

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,44 +188,84 @@ M.git_log_grepper_on_file = function(bufnr)
188188
end, git_log_entry_maker)
189189
end
190190

191+
local previous_commit_hash = function(commit_hash)
192+
local command = "git rev-parse " .. commit_hash .. "~"
193+
local handle = io.popen(command)
194+
local output = handle:read("*a")
195+
handle:close()
196+
197+
output = string.gsub(output, "\n", "")
198+
return output
199+
end
200+
201+
--- Determine the historic file name for a given commit hash and buffer number
202+
--- @param commit_hash string
203+
--- @param bufnr number
204+
--- @return string | nil file_path historic file name relative to git root
191205
local determine_historic_file_name = function(commit_hash, bufnr)
192-
local current_file_name = file.relative_path(bufnr)
206+
local current_file_name = file.git_relative_path(bufnr)
193207

194-
local command = "git log -M --diff-filter=R --follow --name-status --summary "
208+
local command = "cd "
209+
.. file.git_dir()
210+
.. " && "
211+
.. "git --no-pager log -M --follow --name-status --oneline "
195212
.. commit_hash
196-
.. ".. -- "
213+
.. "~.. -- "
197214
.. current_file_name
198-
.. " | grep ^R | tail -1 | cut -f2,2"
215+
.. " | tail -2"
199216

200217
local handle = io.popen(command)
201218
local output = handle:read("*a")
202219
handle:close()
203220

204-
output = string.gsub(output, "\n", "")
205-
if output == "" then
206-
output = file.git_relative_path(bufnr)
221+
-- first line contains the commit_hash and message
222+
-- second line contains the file status and file name (when rename, 2 filenames are present)
223+
output = utils.split_string(output, "\n")
224+
225+
if output[1] == nil or output[2] == nil then
226+
return nil
207227
end
208228

209-
-- output is relative to git root
210-
return output
229+
local returned_hash = utils.split_string(output[1])[1]
230+
231+
-- only return the file name if the commit hash matches, otherwise return nil
232+
if string.sub(returned_hash, 1, 7) == string.sub(commit_hash, 1, 7) then
233+
local split_output = utils.split_string(output[2])
234+
return split_output[#split_output]
235+
else
236+
return nil
237+
end
211238
end
212239

213240
M.git_diff_previewer_file = function(bufnr)
214241
return previewers.new_termopen_previewer({
215242
get_command = function(entry)
216243
local commit_hash = entry.opts.commit_hash
217244

218-
local prev_commit = string.format("%s~", commit_hash)
219-
return git_diff_command({
220-
"git",
221-
"diff",
222-
prev_commit
223-
.. ":"
224-
.. determine_historic_file_name(prev_commit, bufnr),
225-
commit_hash
226-
.. ":"
227-
.. determine_historic_file_name(commit_hash, bufnr),
228-
})
245+
local prev_commit = previous_commit_hash(commit_hash)
246+
if determine_historic_file_name(prev_commit, bufnr) == nil then
247+
return git_diff_command({
248+
"git",
249+
"diff",
250+
prev_commit,
251+
commit_hash,
252+
"--",
253+
file.git_dir()
254+
.. "/"
255+
.. determine_historic_file_name(commit_hash, bufnr),
256+
})
257+
else
258+
return git_diff_command({
259+
"git",
260+
"diff",
261+
prev_commit
262+
.. ":"
263+
.. determine_historic_file_name(prev_commit, bufnr),
264+
commit_hash
265+
.. ":"
266+
.. determine_historic_file_name(commit_hash, bufnr),
267+
})
268+
end
229269
end,
230270
})
231271
end

lua/advanced_git_search/utils/file.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ M.relative_path = function(bufnr)
88
return vim.fn.expand("#" .. bufnr .. ":~:.")
99
end
1010

11+
M.git_dir = function()
12+
return M.find_first_ancestor_dir_or_file(vim.fn.getcwd(), ".git")
13+
end
14+
1115
M.file_name = function(bufnr)
1216
return vim.fn.expand("#" .. bufnr .. ":t")
1317
end

0 commit comments

Comments
 (0)