Skip to content

Commit b742c50

Browse files
authoredJul 29, 2021
feat: add git reset action for git commits picker (#999)
1 parent 82f4d30 commit b742c50

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ document symbols not recognized as methods by treesitter.
468468

469469
| Functions | Description |
470470
|-------------------------------------|------------------------------------------------------------------------------------------------------------|
471-
| `builtin.git_commits` | Lists git commits with diff preview and checks them out on `<cr>` |
471+
| `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` |
472472
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checks them out on `<cr>` |
473473
| `builtin.git_branches` | Lists all branches with log preview, checkout action `<cr>`, track action `<C-t>` and rebase action`<C-r>` |
474474
| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) |

‎lua/telescope/actions/init.lua

+36
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,42 @@ actions.git_rebase_branch = function(prompt_bufnr)
526526
end
527527
end
528528

529+
local git_reset_branch = function(prompt_bufnr, mode)
530+
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
531+
local selection = action_state.get_selected_entry()
532+
533+
local confirmation = vim.fn.input("Do you really wanna " .. mode .. " reset to " .. selection.value .. "? [Y/n] ")
534+
if confirmation ~= "" and string.lower(confirmation) ~= "y" then
535+
return
536+
end
537+
538+
actions.close(prompt_bufnr)
539+
local _, ret, stderr = utils.get_os_command_output({ "git", "reset", mode, selection.value }, cwd)
540+
if ret == 0 then
541+
print("Reset to: " .. selection.value)
542+
else
543+
print(string.format('Error when resetting to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " ")))
544+
end
545+
end
546+
547+
--- Reset to selected git commit using mixed mode
548+
---@param prompt_bufnr number: The prompt bufnr
549+
actions.git_reset_mixed = function(prompt_bufnr)
550+
git_reset_branch(prompt_bufnr, "--mixed")
551+
end
552+
553+
--- Reset to selected git commit using soft mode
554+
---@param prompt_bufnr number: The prompt bufnr
555+
actions.git_reset_soft = function(prompt_bufnr)
556+
git_reset_branch(prompt_bufnr, "--soft")
557+
end
558+
559+
--- Reset to selected git commit using hard mode
560+
---@param prompt_bufnr number: The prompt bufnr
561+
actions.git_reset_hard = function(prompt_bufnr)
562+
git_reset_branch(prompt_bufnr, "--hard")
563+
end
564+
529565
actions.git_checkout_current_buffer = function(prompt_bufnr)
530566
local cwd = actions.get_current_picker(prompt_bufnr).cwd
531567
local selection = actions.get_selected_entry()

‎lua/telescope/builtin/git.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ git.commits = function(opts)
6565
previewers.git_commit_message.new(opts),
6666
},
6767
sorter = conf.file_sorter(opts),
68-
attach_mappings = function()
68+
attach_mappings = function(_, map)
6969
actions.select_default:replace(actions.git_checkout)
70+
map("i", "<c-r>m", actions.git_reset_mixed)
71+
map("n", "<c-r>m", actions.git_reset_mixed)
72+
map("i", "<c-r>s", actions.git_reset_soft)
73+
map("n", "<c-r>s", actions.git_reset_soft)
74+
map("i", "<c-r>h", actions.git_reset_hard)
75+
map("n", "<c-r>h", actions.git_reset_hard)
7076
return true
7177
end,
7278
}):find()

‎lua/telescope/builtin/init.lua

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ builtin.git_files = require("telescope.builtin.git").files
144144
--- Lists commits for current directory with diff preview
145145
--- - Default keymaps:
146146
--- - `<cr>`: checks out the currently selected commit
147+
--- - `<C-r>m`: resets current branch to selected commit using mixed mode
148+
--- - `<C-r>s`: resets current branch to selected commit using soft mode
149+
--- - `<C-r>h`: resets current branch to selected commit using hard mode
147150
---@param opts table: options to pass to the picker
148151
---@field cwd string: specify the path of the repo
149152
builtin.git_commits = require("telescope.builtin.git").commits

0 commit comments

Comments
 (0)
Please sign in to comment.