summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorKyoichiro Yamada <me@kyoh86.dev>2021-05-11 17:55:41 +0900
committerGitHub <noreply@github.com>2021-05-11 01:55:41 -0700
commitd5aa53dcd3fdb7ea4b12f043b787a6419ab8eb84 (patch)
treedc8cb006359417e72f882b6bfbeb8cc6b97f6e81 /lua
parent9fd242db260a63d8b788d1edbabd2d76a55a2d61 (diff)
create new action: git switch (#798)
* create new action: git switch If the branch already exists in local, switch to that. If the branch is only in remote, create new branch tracking remote and switch to new one. * fix a point of review * fix a point of review: map to git-switch action * Revert "fix a point of review" This reverts commit 017ce424a3adfe1b3712a421385cfc3f4258a0fb. * undocument header comment
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/actions/init.lua26
-rw-r--r--lua/telescope/builtin/git.lua4
2 files changed, 29 insertions, 1 deletions
diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua
index ea9ba4c..174f390 100644
--- a/lua/telescope/actions/init.lua
+++ b/lua/telescope/actions/init.lua
@@ -375,6 +375,32 @@ actions.git_checkout = function(prompt_bufnr)
end
end
+-- TODO: add this function header back once the treesitter max-query bug is resolved
+-- Switch to git branch
+-- If the branch already exists in local, switch to that.
+-- If the branch is only in remote, create new branch tracking remote and switch to new one.
+--@param prompt_bufnr number: The prompt bufnr
+actions.git_switch = function(prompt_bufnr)
+ local cwd = action_state.get_current_picker(prompt_bufnr).cwd
+ local selection = action_state.get_selected_entry()
+ actions.close(prompt_bufnr)
+ local pattern = '^refs/remotes/%w+/'
+ local branch = selection.value
+ if string.match(selection.refname, pattern) then
+ branch = string.gsub(selection.refname, pattern, '')
+ end
+ local _, ret, stderr = utils.get_os_command_output({ 'git', 'switch', branch }, cwd)
+ if ret == 0 then
+ print("Switched to: " .. branch)
+ else
+ print(string.format(
+ 'Error when switching to: %s. Git returned: "%s"',
+ selection.value,
+ table.concat(stderr, ' ')
+ ))
+ end
+end
+
--- Tell git to track the currently selected remote branch in Telescope
---@param prompt_bufnr number: The prompt bufnr
actions.git_track_branch = function(prompt_bufnr)
diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua
index 77bacd0..ae03019 100644
--- a/lua/telescope/builtin/git.lua
+++ b/lua/telescope/builtin/git.lua
@@ -199,9 +199,11 @@ git.branches = function(opts)
map('i', '<c-a>', actions.git_create_branch)
map('n', '<c-a>', actions.git_create_branch)
+ map('i', '<c-s>', actions.git_switch_branch)
+ map('n', '<c-s>', actions.git_switch_branch)
+
map('i', '<c-d>', actions.git_delete_branch)
map('n', '<c-d>', actions.git_delete_branch)
-
return true
end
}):find()