Skip to content

Commit

Permalink
feat(git_branches): actions to add|delete a branch (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Apr 2, 2024
1 parent c838d9c commit abbd1fa
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,15 @@ require'fzf-lua'.setup {
preview = "git log --graph --pretty=oneline --abbrev-commit --color {1}",
actions = {
["default"] = actions.git_switch,
["ctrl-x"] = { fn = actions.git_branch_del, reload = true },
["ctrl-a"] = { fn = actions.git_branch_add, field_index = "{q}", reload = true },
},
-- If you wish to add branch and switch immediately
-- cmd_add = { "git", "checkout", "-b" },
cmd_add = { "git", "branch" },
-- If you wish to delete unmerged branches add "--force"
-- cmd_del = { "git", "branch", "--delete", "--force" },
cmd_del = { "git", "branch", "--delete" },
},
tags = {
prompt = "Tags> ",
Expand Down
55 changes: 46 additions & 9 deletions lua/fzf-lua/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ end


M.git_switch = function(selected, opts)
if not selected[1] then return end
local cmd = path.git_cwd({ "git", "checkout" }, opts)
local git_ver = utils.git_version()
-- git switch was added with git version 2.23
Expand All @@ -590,8 +591,46 @@ M.git_switch = function(selected, opts)
utils.err(unpack(output))
else
utils.info(unpack(output))
if #vim.api.nvim_buf_get_name(0) > 0 then
vim.cmd("edit!")
vim.cmd("checktime")
end
end

M.git_branch_add = function(selected, opts)
-- "reload" actions (fzf version >= 0.36) use field_index = "{q}"
-- so the prompt input will be found in `selected[1]`
-- previous fzf versions (or skim) restart the process instead
-- so the prompt input will be found in `opts.last_query`
local branch = opts.last_query or selected[1]
if type(branch) ~= "string" or #branch == 0 then
utils.warn("Branch name cannot be empty, use prompt for input.")
else
local cmd_add_branch = path.git_cwd(opts.cmd_add, opts)
table.insert(cmd_add_branch, branch)
local output, rc = utils.io_systemlist(cmd_add_branch)
if rc ~= 0 then
utils.err(unpack(output))
else
utils.info(string.format("Created branch '%s'.", branch))
end
end
end

M.git_branch_del = function(selected, opts)
local cmd_del_branch = path.git_cwd(opts.cmd_del, opts)
local cmd_cur_branch = path.git_cwd({ "git", "rev-parse", "--abbrev-ref", "HEAD" }, opts)
local branch = selected[1]:match("[^%s%*]+")
local cur_branch = utils.io_systemlist(cmd_cur_branch)[1]
if branch == cur_branch then
utils.warn(string.format("Cannot delete active branch '%s'", branch))
return
end
if utils.input("Delete branch " .. branch .. "? [y/n] ") == "y" then
table.insert(cmd_del_branch, branch)
local output, rc = utils.io_systemlist(cmd_del_branch)
if rc ~= 0 then
utils.err(unpack(output))
else
utils.info(unpack(output))
end
end
end
Expand All @@ -618,21 +657,19 @@ M.git_yank_commit = function(selected, opts)
end

M.git_checkout = function(selected, opts)
local cmd_checkout = path.git_cwd({ "git", "checkout" }, opts)
local cmd_cur_commit = path.git_cwd({ "git", "rev-parse", "--short HEAD" }, opts)
local cmd_cur_commit = path.git_cwd({ "git", "rev-parse", "--short", "HEAD" }, opts)
local commit_hash = match_commit_hash(selected[1], opts)
local current_commit = utils.io_systemlist(cmd_cur_commit)[1]
if commit_hash == current_commit then return end
if utils.input("Checkout commit " .. commit_hash .. "? [y/n] ") == "y" then
local current_commit = utils.io_systemlist(cmd_cur_commit)
if (commit_hash == current_commit) then return end
local cmd_checkout = path.git_cwd({ "git", "checkout" }, opts)
table.insert(cmd_checkout, commit_hash)
local output, rc = utils.io_systemlist(cmd_checkout)
if rc ~= 0 then
utils.err(unpack(output))
else
utils.info(unpack(output))
if #vim.api.nvim_buf_get_name(0) > 0 then
vim.cmd("edit!")
end
vim.cmd("checktime")
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ M._action_to_helpstr = {
[actions.man] = "man-open",
[actions.man_vert] = "man-vertical",
[actions.man_tab] = "man-tab",
[actions.git_branch_add] = "git-branch-add",
[actions.git_branch_del] = "git-branch-del",
[actions.git_switch] = "git-switch",
[actions.git_checkout] = "git-checkout",
[actions.git_reset] = "git-reset",
Expand Down
2 changes: 2 additions & 0 deletions lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ M.ACTION_DEFINITIONS = {
[actions.git_stage_unstage] = { "[un-]stage", pos = 1 },
[actions.git_stash_drop] = { "drop a stash" },
[actions.git_yank_commit] = { "copy commit hash" },
[actions.git_branch_add] = { "add branch" },
[actions.git_branch_del] = { "delete branch" },
}

-- converts contents array sent to `fzf_exec` into a single contents
Expand Down
4 changes: 4 additions & 0 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ M.defaults.git = {
fzf_opts = { ["--no-multi"] = true },
actions = {
["default"] = actions.git_switch,
["ctrl-x"] = { fn = actions.git_branch_del, reload = true },
["ctrl-a"] = { fn = actions.git_branch_add, field_index = "{q}", reload = true },
},
cmd_add = { "git", "branch" },
cmd_del = { "git", "branch", "--delete" },
},
tags = {
prompt = "Tags> ",
Expand Down
1 change: 1 addition & 0 deletions lua/fzf-lua/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ M.branches = function(opts)
return opts.__preview:gsub("{.*}", branch)
end, nil, opts.debug)
end
opts.headers = opts.headers or { "cwd", "actions" }
return git_cmd(opts)
end

Expand Down

0 comments on commit abbd1fa

Please sign in to comment.