feat: add git_stash picker (#800)
This commit is contained in:
committed by
GitHub
parent
c061c216bf
commit
9fd242db26
@@ -431,6 +431,7 @@ Built-in functions. Ready to be bound to any key you like. :smile:
|
|||||||
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. |
|
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. |
|
||||||
| `builtin.git_branches` | Lists all branches with log preview, checkout action (<cr>), track action (<c-t>) and rebase action(<c-r>). |
|
| `builtin.git_branches` | Lists all branches with log preview, checkout action (<cr>), track action (<c-t>) and rebase action(<c-r>). |
|
||||||
| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) |
|
| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) |
|
||||||
|
| `builtin.git_stash` | Lists stash items in current repository with ability to apply them on <CR> |
|
||||||
|
|
||||||
### Treesitter Picker
|
### Treesitter Picker
|
||||||
|
|
||||||
|
|||||||
@@ -236,6 +236,14 @@ actions.git_create_branch({prompt_bufnr}) *actions.git_create_branch()*
|
|||||||
{prompt_bufnr} (number) The prompt bufnr
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
|
actions.git_apply_stash({prompt_bufnr}) *actions.git_apply_stash()*
|
||||||
|
Applies an existing git stash
|
||||||
|
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
actions.git_checkout({prompt_bufnr}) *actions.git_checkout()*
|
actions.git_checkout({prompt_bufnr}) *actions.git_checkout()*
|
||||||
Checkout an existing git branch
|
Checkout an existing git branch
|
||||||
|
|
||||||
|
|||||||
@@ -340,6 +340,23 @@ actions.git_create_branch = function(prompt_bufnr)
|
|||||||
end
|
end
|
||||||
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
|
--- Checkout an existing git branch
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
actions.git_checkout = function(prompt_bufnr)
|
actions.git_checkout = function(prompt_bufnr)
|
||||||
|
|||||||
@@ -57,6 +57,25 @@ git.commits = function(opts)
|
|||||||
}):find()
|
}):find()
|
||||||
end
|
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)
|
git.bcommits = function(opts)
|
||||||
local results = utils.get_os_command_output({
|
local results = utils.get_os_command_output({
|
||||||
'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%')
|
'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%')
|
||||||
|
|||||||
@@ -240,6 +240,21 @@ do
|
|||||||
end
|
end
|
||||||
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()
|
function make_entry.gen_from_git_commits()
|
||||||
local displayer = entry_display.create {
|
local displayer = entry_display.create {
|
||||||
separator = " ",
|
separator = " ",
|
||||||
|
|||||||
@@ -502,6 +502,23 @@ previewers.git_branch_log = defaulter(function(opts)
|
|||||||
}
|
}
|
||||||
end, {})
|
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)
|
previewers.git_commit_diff = defaulter(function(opts)
|
||||||
return previewers.new_buffer_previewer {
|
return previewers.new_buffer_previewer {
|
||||||
get_buffer_by_name = function(_, entry)
|
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_branch_log = buffer_previewer.git_branch_log
|
||||||
previewers.git_commit_diff = buffer_previewer.git_commit_diff
|
previewers.git_commit_diff = buffer_previewer.git_commit_diff
|
||||||
previewers.git_file_diff = buffer_previewer.git_file_diff
|
previewers.git_file_diff = buffer_previewer.git_file_diff
|
||||||
|
previewers.git_stash_diff = buffer_previewer.git_stash_diff
|
||||||
|
|
||||||
|
|
||||||
previewers.ctags = buffer_previewer.ctags
|
previewers.ctags = buffer_previewer.ctags
|
||||||
|
|||||||
Reference in New Issue
Block a user