summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorAmirreza Askarpour <raskarpour@gmail.com>2021-05-11 12:50:57 +0430
committerGitHub <noreply@github.com>2021-05-11 10:20:57 +0200
commit9fd242db260a63d8b788d1edbabd2d76a55a2d61 (patch)
tree82a47db9b567a0be1acfb3ccee129b1ff53e0e6a /lua
parentc061c216bfe082384d542a487ce02e9aed6177df (diff)
feat: add git_stash picker (#800)
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/actions/init.lua17
-rw-r--r--lua/telescope/builtin/git.lua19
-rw-r--r--lua/telescope/make_entry.lua15
-rw-r--r--lua/telescope/previewers/buffer_previewer.lua17
-rw-r--r--lua/telescope/previewers/init.lua1
5 files changed, 69 insertions, 0 deletions
diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua
index 1677e10..ea9ba4c 100644
--- a/lua/telescope/actions/init.lua
+++ b/lua/telescope/actions/init.lua
@@ -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)
diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua
index d708126..77bacd0 100644
--- a/lua/telescope/builtin/git.lua
+++ b/lua/telescope/builtin/git.lua
@@ -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('%')
diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua
index 9435354..39106e0 100644
--- a/lua/telescope/make_entry.lua
+++ b/lua/telescope/make_entry.lua
@@ -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 = " ",
diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua
index d480e0c..c85d2f2 100644
--- a/lua/telescope/previewers/buffer_previewer.lua
+++ b/lua/telescope/previewers/buffer_previewer.lua
@@ -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)
diff --git a/lua/telescope/previewers/init.lua b/lua/telescope/previewers/init.lua
index db801ea..853ad5b 100644
--- a/lua/telescope/previewers/init.lua
+++ b/lua/telescope/previewers/init.lua
@@ -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