Skip to content

Commit 325692d

Browse files
committed
Fix first commit in preview
1 parent 500be1a commit 325692d

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

lua/advanced_git_search/git_utils.lua

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,44 +188,71 @@ 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+
191201
local determine_historic_file_name = function(commit_hash, bufnr)
192-
local current_file_name = file.relative_path(bufnr)
202+
local current_file_name = file.git_relative_path(bufnr)
193203

194-
local command = "git log -M --diff-filter=R --follow --name-status --summary "
204+
local command = "cd "
205+
.. file.git_dir()
206+
.. " && "
207+
.. "git --no-pager log -M --follow --name-status --summary "
195208
.. commit_hash
196-
.. ".. -- "
209+
.. "~.. -- "
197210
.. current_file_name
198-
.. " | grep ^R | tail -1 | cut -f2,2"
211+
.. " | tail -1"
199212

200213
local handle = io.popen(command)
201214
local output = handle:read("*a")
202215
handle:close()
203216

204217
output = string.gsub(output, "\n", "")
205-
if output == "" then
206-
output = file.git_relative_path(bufnr)
218+
219+
if output == nil or output == "" then
220+
return nil
207221
end
208222

209-
-- output is relative to git root
210-
return output
223+
local split_output = utils.split_string(output)
224+
return split_output[#split_output]
211225
end
212226

213227
M.git_diff_previewer_file = function(bufnr)
214228
return previewers.new_termopen_previewer({
215229
get_command = function(entry)
216230
local commit_hash = entry.opts.commit_hash
217231

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-
})
232+
local prev_commit = previous_commit_hash(commit_hash)
233+
if determine_historic_file_name(prev_commit, bufnr) == nil then
234+
return git_diff_command({
235+
"git",
236+
"diff",
237+
prev_commit,
238+
commit_hash,
239+
"--",
240+
file.git_dir()
241+
.. "/"
242+
.. determine_historic_file_name(commit_hash, bufnr),
243+
})
244+
else
245+
return git_diff_command({
246+
"git",
247+
"diff",
248+
prev_commit
249+
.. ":"
250+
.. determine_historic_file_name(prev_commit, bufnr),
251+
commit_hash
252+
.. ":"
253+
.. determine_historic_file_name(commit_hash, bufnr),
254+
})
255+
end
229256
end,
230257
})
231258
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)