Skip to content

Instantly share code, notes, and snippets.

@tyler-dot-earth
Last active April 4, 2025 15:56
Show Gist options
  • Save tyler-dot-earth/3d41116cfc09b470e24e4e6446f66448 to your computer and use it in GitHub Desktop.
Save tyler-dot-earth/3d41116cfc09b470e24e4e6446f66448 to your computer and use it in GitHub Desktop.
Telescope picker for Neovim which only shows files in the most recent commit.
-- Telescope picker for files in most recent git commit
local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders'
local conf = require('telescope.config').values
local Job = require 'plenary.job'
-- Telescope picker for files in most recent git commit
local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders'
local conf = require('telescope.config').values
local Job = require 'plenary.job'
local function git_last_commit_files()
---@diagnostic disable-next-line: missing-fields
local git_root = Job:new({
command = 'git',
args = { 'rev-parse', '--show-toplevel' },
})
:sync()[1]
if not git_root then
vim.notify('Not inside a git repo', vim.log.levels.ERROR)
return
end
---@diagnostic disable-next-line: missing-fields
Job:new({
command = 'git',
cwd = git_root,
args = { 'diff-tree', '--no-commit-id', '--name-only', '-r', 'HEAD' },
on_exit = function(j)
local rel_paths = j:result()
local entries = vim.tbl_map(function(path)
return {
display = path, -- what telescope shows
value = path, -- what the picker returns
ordinal = path, -- what fuzzy match uses
filename = git_root .. '/' .. path, -- used by file_previewer
}
end, rel_paths)
vim.schedule(function()
pickers
.new({}, {
prompt_title = 'Files in last commit',
finder = finders.new_table {
results = entries,
entry_maker = function(entry)
return entry
end,
},
sorter = conf.generic_sorter {},
previewer = conf.file_previewer {},
})
:find()
end)
end,
}):start()
end
vim.keymap.set('n', '<leader>sc', git_last_commit_files, { desc = '[S]earch [c]hanged files in last commit' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment