feat: add git_stash picker (#800)

This commit is contained in:
Amirreza Askarpour
2021-05-11 12:50:57 +04:30
committed by GitHub
parent c061c216bf
commit 9fd242db26
7 changed files with 78 additions and 0 deletions

View File

@@ -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_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_stash` | Lists stash items in current repository with ability to apply them on <CR> |
### Treesitter Picker

View File

@@ -236,6 +236,14 @@ actions.git_create_branch({prompt_bufnr}) *actions.git_create_branch()*
{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()*
Checkout an existing git branch

View File

@@ -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)

View File

@@ -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('%')

View File

@@ -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 = " ",

View File

@@ -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)

View File

@@ -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