From b742c50bf119d7c442a553cc52e8afde0086ae02 Mon Sep 17 00:00:00 2001 From: Joel Palmer Date: Thu, 29 Jul 2021 16:16:45 -0500 Subject: [PATCH] feat: add git reset action for git commits picker (#999) --- README.md | 2 +- lua/telescope/actions/init.lua | 36 ++++++++++++++++++++++++++++++++++ lua/telescope/builtin/git.lua | 8 +++++++- lua/telescope/builtin/init.lua | 3 +++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 101ce5d..c1e03c0 100644 --- a/README.md +++ b/README.md @@ -468,7 +468,7 @@ document symbols not recognized as methods by treesitter. | Functions | Description | |-------------------------------------|------------------------------------------------------------------------------------------------------------| -| `builtin.git_commits` | Lists git commits with diff preview and checks them out on `` | +| `builtin.git_commits` | Lists git commits with diff preview, checkout action ``, reset mixed `m`, reset soft `s` and reset hard `h` | | `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checks them out on `` | | `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) | diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index c048d50..4ee6134 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -526,6 +526,42 @@ actions.git_rebase_branch = function(prompt_bufnr) end end +local git_reset_branch = function(prompt_bufnr, mode) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local selection = action_state.get_selected_entry() + + local confirmation = vim.fn.input("Do you really wanna " .. mode .. " reset to " .. selection.value .. "? [Y/n] ") + if confirmation ~= "" and string.lower(confirmation) ~= "y" then + return + end + + actions.close(prompt_bufnr) + local _, ret, stderr = utils.get_os_command_output({ "git", "reset", mode, selection.value }, cwd) + if ret == 0 then + print("Reset to: " .. selection.value) + else + print(string.format('Error when resetting to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))) + end +end + +--- Reset to selected git commit using mixed mode +---@param prompt_bufnr number: The prompt bufnr +actions.git_reset_mixed = function(prompt_bufnr) + git_reset_branch(prompt_bufnr, "--mixed") +end + +--- Reset to selected git commit using soft mode +---@param prompt_bufnr number: The prompt bufnr +actions.git_reset_soft = function(prompt_bufnr) + git_reset_branch(prompt_bufnr, "--soft") +end + +--- Reset to selected git commit using hard mode +---@param prompt_bufnr number: The prompt bufnr +actions.git_reset_hard = function(prompt_bufnr) + git_reset_branch(prompt_bufnr, "--hard") +end + actions.git_checkout_current_buffer = function(prompt_bufnr) local cwd = actions.get_current_picker(prompt_bufnr).cwd local selection = actions.get_selected_entry() diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index 4bf0283..513dbed 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -65,8 +65,14 @@ git.commits = function(opts) previewers.git_commit_message.new(opts), }, sorter = conf.file_sorter(opts), - attach_mappings = function() + attach_mappings = function(_, map) actions.select_default:replace(actions.git_checkout) + map("i", "m", actions.git_reset_mixed) + map("n", "m", actions.git_reset_mixed) + map("i", "s", actions.git_reset_soft) + map("n", "s", actions.git_reset_soft) + map("i", "h", actions.git_reset_hard) + map("n", "h", actions.git_reset_hard) return true end, }):find() diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index c581ac7..163a258 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -144,6 +144,9 @@ builtin.git_files = require("telescope.builtin.git").files --- Lists commits for current directory with diff preview --- - Default keymaps: --- - ``: checks out the currently selected commit +--- - `m`: resets current branch to selected commit using mixed mode +--- - `s`: resets current branch to selected commit using soft mode +--- - `h`: resets current branch to selected commit using hard mode ---@param opts table: options to pass to the picker ---@field cwd string: specify the path of the repo builtin.git_commits = require("telescope.builtin.git").commits