Skip to content

Commit

Permalink
feat: add git reset action for git commits picker (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpalmer committed Jul 29, 2021
1 parent 82f4d30 commit b742c50
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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 `<cr>` |
| `builtin.git_commits` | Lists git commits with diff preview, checkout action `<cr>`, reset mixed `<C-r>m`, reset soft `<C-r>s` and reset hard `<C-r>h` |
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checks them out on `<cr>` |
| `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) |
Expand Down
36 changes: 36 additions & 0 deletions lua/telescope/actions/init.lua
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion lua/telescope/builtin/git.lua
Expand Up @@ -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", "<c-r>m", actions.git_reset_mixed)
map("n", "<c-r>m", actions.git_reset_mixed)
map("i", "<c-r>s", actions.git_reset_soft)
map("n", "<c-r>s", actions.git_reset_soft)
map("i", "<c-r>h", actions.git_reset_hard)
map("n", "<c-r>h", actions.git_reset_hard)
return true
end,
}):find()
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/builtin/init.lua
Expand Up @@ -144,6 +144,9 @@ builtin.git_files = require("telescope.builtin.git").files
--- Lists commits for current directory with diff preview
--- - Default keymaps:
--- - `<cr>`: checks out the currently selected commit
--- - `<C-r>m`: resets current branch to selected commit using mixed mode
--- - `<C-r>s`: resets current branch to selected commit using soft mode
--- - `<C-r>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
Expand Down

0 comments on commit b742c50

Please sign in to comment.