From 9fd242db260a63d8b788d1edbabd2d76a55a2d61 Mon Sep 17 00:00:00 2001 From: Amirreza Askarpour Date: Tue, 11 May 2021 12:50:57 +0430 Subject: [PATCH] feat: add git_stash picker (#800) --- README.md | 1 + doc/telescope.txt | 8 ++++++++ lua/telescope/actions/init.lua | 17 +++++++++++++++++ lua/telescope/builtin/git.lua | 19 +++++++++++++++++++ lua/telescope/make_entry.lua | 15 +++++++++++++++ lua/telescope/previewers/buffer_previewer.lua | 17 +++++++++++++++++ lua/telescope/previewers/init.lua | 1 + 7 files changed, 78 insertions(+) diff --git a/README.md b/README.md index adf08a7..1077886 100644 --- a/README.md +++ b/README.md @@ -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 (), track action () and rebase action(). | | `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 | ### Treesitter Picker diff --git a/doc/telescope.txt b/doc/telescope.txt index 4ad4d42..e82bfb4 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -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 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