Skip to content

Commit 0385f43

Browse files
committed
Add checkout reflog fzf
Signed-off-by: Aaron Hallaert <[email protected]>
1 parent c6ca1df commit 0385f43

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

lua/advanced_git_search/actions.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ M.open_in_browser = function(commit_hash)
5858
vim.api.nvim_command(":GBrowse " .. commit_hash)
5959
end
6060

61+
---General action: Checkout commit
62+
---@param commit_hash string
63+
M.checkout_commit = function(commit_hash)
64+
vim.api.nvim_command(":!git checkout " .. commit_hash)
65+
end
66+
6167
return M

lua/advanced_git_search/commands/find.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ M.git_branches = function()
1212
}
1313
end
1414

15+
M.reflog = function()
16+
return {
17+
"git",
18+
"reflog",
19+
"--date=iso",
20+
}
21+
end
22+
1523
---@param prompt string|nil
1624
---@param author string|nil
1725
---@param bufnr number|nil

lua/advanced_git_search/fzf/pickers/init.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ M.changed_on_branch = function()
161161
)
162162
end
163163

164+
M.checkout_reflog = function()
165+
local opts = {
166+
func_async_callback = false,
167+
fn_transform = function(x)
168+
return picker_utils.make_reflog_entry(x)
169+
end,
170+
actions = {
171+
["default"] = function(selected)
172+
local selection = selected[1]
173+
local commit = string.sub(selection, 1, 7)
174+
175+
require("advanced_git_search.actions").checkout_commit(commit)
176+
end,
177+
},
178+
}
179+
180+
require("fzf-lua").fzf_exec(
181+
table.concat(require("advanced_git_search.commands.find").reflog(), " "),
182+
opts
183+
)
184+
end
185+
164186
--- Opens a selector for all advanced git search functions
165187
M.show_custom_functions = function()
166188
local keys = global_picker.keys("telescope")

lua/advanced_git_search/fzf/pickers/utils.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,33 @@ M.make_entry = function(entry)
3232
.. color.yellow(message)
3333
end
3434

35+
M.make_reflog_entry = function(entry)
36+
if entry == "" or entry == nil then
37+
return
38+
end
39+
40+
local cleaned = string.gsub(entry, "'", "")
41+
local split = utils.split_string(cleaned, " ")
42+
local hash = split[1]
43+
44+
local rest = split[2]
45+
for i = 3, #split do
46+
rest = rest .. " " .. split[i]
47+
end
48+
49+
local split_on_double = utils.split_string(rest, ":")
50+
local description = ""
51+
for i = 4, #split_on_double do
52+
description = description .. split_on_double[i]
53+
end
54+
55+
local meta = ""
56+
for i = 1, 3 do
57+
meta = meta .. split_on_double[i]
58+
end
59+
60+
-- NOTE: make sure the first value is the commit hash
61+
return color.magenta(hash) .. " " .. meta .. "" .. color.yellow(description)
62+
end
63+
3564
return M

lua/advanced_git_search/telescope/mappings/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ local checkout = function(prompt_bufnr)
102102
splitted_reflog_entry[count] = i
103103
count = count + 1
104104
end
105-
vim.api.nvim_command(":!git checkout " .. splitted_reflog_entry[1])
105+
106+
ags_actions.checkout_commit(splitted_reflog_entry[1])
106107
end
107108

108109
--- Checkout the selected reflog entry with <CR>

lua/advanced_git_search/telescope/pickers/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ M.checkout_reflog = function()
155155
pickers
156156
.new({
157157
results_title = "Git Reflog, <CR> to checkout",
158-
finder = finders.new_oneshot_job({ "git", "reflog", "--date=iso" }),
158+
finder = finders.new_oneshot_job(
159+
require("advanced_git_search.commands.find").reflog()
160+
),
159161
sorter = sorters.get_fuzzy_file(),
160162
attach_mappings = function(_, map)
161163
ags_mappings.checkout_reflog_entry(map)

0 commit comments

Comments
 (0)