feat: add git_stash picker (#800)
This commit is contained in:
committed by
GitHub
parent
c061c216bf
commit
9fd242db26
@@ -340,6 +340,23 @@ actions.git_create_branch = function(prompt_bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
--- Applies an existing git stash
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_apply_stash = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
actions.close(prompt_bufnr)
|
||||
local _, ret, stderr = utils.get_os_command_output({ 'git', 'stash', 'apply', '--index', selection.value })
|
||||
if ret == 0 then
|
||||
print("applied: " .. selection.value)
|
||||
else
|
||||
print(string.format(
|
||||
'Error when applying: %s. Git returned: "%s"',
|
||||
selection.value,
|
||||
table.concat(stderr, ' ')
|
||||
))
|
||||
end
|
||||
end
|
||||
|
||||
--- Checkout an existing git branch
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_checkout = function(prompt_bufnr)
|
||||
|
||||
@@ -57,6 +57,25 @@ git.commits = function(opts)
|
||||
}):find()
|
||||
end
|
||||
|
||||
git.stash = function(opts)
|
||||
local results = utils.get_os_command_output({
|
||||
'git', '--no-pager', 'stash', 'list',
|
||||
}, opts.cwd)
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt_title = 'Git Stash',
|
||||
finder = finders.new_table {
|
||||
results = results,
|
||||
entry_maker = opts.entry_maker or make_entry.gen_from_git_stash(),
|
||||
},
|
||||
previewer = previewers.git_stash_diff.new(opts),
|
||||
sorter = conf.file_sorter(opts),
|
||||
attach_mappings = function()
|
||||
actions.select_default:replace(actions.git_apply_stash)
|
||||
return true
|
||||
end
|
||||
}):find()
|
||||
end
|
||||
git.bcommits = function(opts)
|
||||
local results = utils.get_os_command_output({
|
||||
'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%')
|
||||
|
||||
@@ -240,6 +240,21 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
function make_entry.gen_from_git_stash()
|
||||
return function(entry)
|
||||
if entry == "" then
|
||||
return nil
|
||||
end
|
||||
local splitted = vim.split(entry, ':')
|
||||
return {
|
||||
value = splitted[1],
|
||||
ordinal = splitted[3],
|
||||
display = splitted[3]
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function make_entry.gen_from_git_commits()
|
||||
local displayer = entry_display.create {
|
||||
separator = " ",
|
||||
|
||||
@@ -502,6 +502,23 @@ previewers.git_branch_log = defaulter(function(opts)
|
||||
}
|
||||
end, {})
|
||||
|
||||
previewers.git_stash_diff = defaulter(function(opts)
|
||||
return previewers.new_buffer_previewer {
|
||||
get_buffer_by_name = function(_, entry)
|
||||
return entry.value
|
||||
end,
|
||||
|
||||
define_preview = function(self, entry, _)
|
||||
putils.job_maker({ 'git', '--no-pager', 'stash', 'show', '-p', entry.value }, self.state.bufnr, {
|
||||
value = entry.value,
|
||||
bufname = self.state.bufname,
|
||||
cwd = opts.cwd
|
||||
})
|
||||
putils.regex_highlighter(self.state.bufnr, 'diff')
|
||||
end
|
||||
}
|
||||
end, {})
|
||||
|
||||
previewers.git_commit_diff = defaulter(function(opts)
|
||||
return previewers.new_buffer_previewer {
|
||||
get_buffer_by_name = function(_, entry)
|
||||
|
||||
@@ -263,6 +263,7 @@ previewers.vim_buffer_qflist = buffer_previewer.qflist
|
||||
previewers.git_branch_log = buffer_previewer.git_branch_log
|
||||
previewers.git_commit_diff = buffer_previewer.git_commit_diff
|
||||
previewers.git_file_diff = buffer_previewer.git_file_diff
|
||||
previewers.git_stash_diff = buffer_previewer.git_stash_diff
|
||||
|
||||
|
||||
previewers.ctags = buffer_previewer.ctags
|
||||
|
||||
Reference in New Issue
Block a user