Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lua/neogit/lib/git/branch.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local a = require("plenary.async")
local cli = require("neogit.lib.git.cli")
local input = require("neogit.lib.input")
local util = require("neogit.lib.util")
local config = require("neogit.config")
local M = {}

Expand All @@ -21,6 +22,22 @@ local function parse_branches(branches, include_current)
return other_branches
end

-- https://gist.github.com/joechrysler/6073741
function M.get_nearest_parent()
local head = util.pattern_escape(cli["rev-parse"].abbrev_ref().args("HEAD").call_sync():trim().stdout[1])

local parent
local result = cli["show-branch"].all.call_sync():trim().stdout
for _, branch in ipairs(result) do
if branch:match("%*") and not branch:match(head) then
parent = branch:match("%[(.*)[~%^]?%d*%]")
break
end
end

return parent
end

function M.get_local_branches(include_current)
local branches = cli.branch.list(config.values.sort_branches).call_sync():trim().stdout

Expand Down
28 changes: 28 additions & 0 deletions lua/neogit/lib/git/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local configurations = {
porcelain = "--porcelain",
},
},

log = config {
flags = {
oneline = "--oneline",
Expand All @@ -62,6 +63,7 @@ local configurations = {
end,
},
},

config = config {
flags = {
_get = "--get",
Expand All @@ -74,6 +76,7 @@ local configurations = {
end,
},
},

diff = config {
flags = {
cached = "--cached",
Expand All @@ -83,13 +86,15 @@ local configurations = {
no_ext_diff = "--no-ext-diff",
},
},

stash = config {
flags = {
apply = "apply",
drop = "drop",
index = "--index",
},
},

rebase = config {
flags = {
interactive = "-i",
Expand All @@ -98,6 +103,7 @@ local configurations = {
skip = "--skip",
},
},

reset = config {
flags = {
hard = "--hard",
Expand All @@ -110,6 +116,7 @@ local configurations = {
end,
},
},

checkout = config {
short_opts = {
b = "-b",
Expand All @@ -132,6 +139,7 @@ local configurations = {
end,
},
},

remote = config {
flags = {
push = "--push",
Expand All @@ -145,6 +153,7 @@ local configurations = {
end,
},
},

apply = config {
flags = {
cached = "--cached",
Expand All @@ -157,12 +166,14 @@ local configurations = {
end,
},
},

add = config {
flags = {
update = "-u",
all = "-A",
},
},

commit = config {
flags = {
amend = "--amend",
Expand All @@ -174,6 +185,7 @@ local configurations = {
commit_message_file = "--file",
},
},

push = config {
flags = {
delete = "--delete",
Expand All @@ -191,6 +203,7 @@ local configurations = {
end,
},
},

pull = config {
flags = {
no_commit = "--no-commit",
Expand All @@ -199,6 +212,7 @@ local configurations = {
flags = {},
},
},

branch = config {
flags = {
all = "-a",
Expand All @@ -221,6 +235,7 @@ local configurations = {
end,
},
},

["read-tree"] = config {
flags = {
merge = "-m",
Expand All @@ -236,7 +251,9 @@ local configurations = {
end,
},
},

["write-tree"] = config {},

["commit-tree"] = config {
flags = {
no_gpg_sign = "--no-gpg-sign",
Expand All @@ -261,18 +278,27 @@ local configurations = {
end,
},
},

["update-index"] = config {
flags = {
add = "--add",
remove = "--remove",
refresh = "--refresh",
},
},

["show-ref"] = config {
flags = {
verify = "--verify",
},
},

["show-branch"] = config {
flags = {
all = "--all",
},
},

["update-ref"] = config {
flags = {
create_reflog = "--create-reflog",
Expand All @@ -281,6 +307,7 @@ local configurations = {
message = "-m",
},
},

["ls-files"] = config {
flags = {
others = "--others",
Expand All @@ -290,6 +317,7 @@ local configurations = {
full_name = "--full-name",
},
},

["rev-parse"] = config {
flags = {
revs_only = "--revs-only",
Expand Down
10 changes: 10 additions & 0 deletions lua/neogit/lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ local function parse_command_args(args)
return tbl
end

local function pattern_escape(str)
local special_chars = { "(", ")", ".", "%", "+", "-", "*", "?", "[", "^", "$" }
for _, char in ipairs(special_chars) do
str = str:gsub("%" .. char, "%%" .. char)
end

return str
end

return {
time = time,
time_async = time_async,
Expand All @@ -195,4 +204,5 @@ return {
deepcopy = deepcopy,
trim = trim,
parse_command_args = parse_command_args,
pattern_escape = pattern_escape,
}
5 changes: 3 additions & 2 deletions lua/neogit/popups/rebase.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local cli = require("neogit.lib.git.cli")
local branch = require("neogit.lib.git.branch")
local git = require("neogit.lib.git")
local popup = require("neogit.lib.popup")
local CommitSelectViewBuffer = require("neogit.buffers.commit_select_view")
Expand Down Expand Up @@ -30,9 +31,9 @@ function M.create()
end)
:action(
"p",
"Rebase onto master",
"Rebase onto " .. branch.get_nearest_parent(),
a.void(function(popup)
rebase.rebase_onto("master", popup:get_arguments())
rebase.rebase_onto(branch.get_nearest_parent(), popup:get_arguments())
a.util.scheduler()
status.refresh(true, "rebase_master")
end)
Expand Down