diff --git a/lua/neogit/lib/git/branch.lua b/lua/neogit/lib/git/branch.lua index f03af4ad5..1215cc0ce 100644 --- a/lua/neogit/lib/git/branch.lua +++ b/lua/neogit/lib/git/branch.lua @@ -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 = {} @@ -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 diff --git a/lua/neogit/lib/git/cli.lua b/lua/neogit/lib/git/cli.lua index 82bf4e697..c89fbc388 100644 --- a/lua/neogit/lib/git/cli.lua +++ b/lua/neogit/lib/git/cli.lua @@ -41,6 +41,7 @@ local configurations = { porcelain = "--porcelain", }, }, + log = config { flags = { oneline = "--oneline", @@ -62,6 +63,7 @@ local configurations = { end, }, }, + config = config { flags = { _get = "--get", @@ -74,6 +76,7 @@ local configurations = { end, }, }, + diff = config { flags = { cached = "--cached", @@ -83,6 +86,7 @@ local configurations = { no_ext_diff = "--no-ext-diff", }, }, + stash = config { flags = { apply = "apply", @@ -90,6 +94,7 @@ local configurations = { index = "--index", }, }, + rebase = config { flags = { interactive = "-i", @@ -98,6 +103,7 @@ local configurations = { skip = "--skip", }, }, + reset = config { flags = { hard = "--hard", @@ -110,6 +116,7 @@ local configurations = { end, }, }, + checkout = config { short_opts = { b = "-b", @@ -132,6 +139,7 @@ local configurations = { end, }, }, + remote = config { flags = { push = "--push", @@ -145,6 +153,7 @@ local configurations = { end, }, }, + apply = config { flags = { cached = "--cached", @@ -157,12 +166,14 @@ local configurations = { end, }, }, + add = config { flags = { update = "-u", all = "-A", }, }, + commit = config { flags = { amend = "--amend", @@ -174,6 +185,7 @@ local configurations = { commit_message_file = "--file", }, }, + push = config { flags = { delete = "--delete", @@ -191,6 +203,7 @@ local configurations = { end, }, }, + pull = config { flags = { no_commit = "--no-commit", @@ -199,6 +212,7 @@ local configurations = { flags = {}, }, }, + branch = config { flags = { all = "-a", @@ -221,6 +235,7 @@ local configurations = { end, }, }, + ["read-tree"] = config { flags = { merge = "-m", @@ -236,7 +251,9 @@ local configurations = { end, }, }, + ["write-tree"] = config {}, + ["commit-tree"] = config { flags = { no_gpg_sign = "--no-gpg-sign", @@ -261,6 +278,7 @@ local configurations = { end, }, }, + ["update-index"] = config { flags = { add = "--add", @@ -268,11 +286,19 @@ local configurations = { refresh = "--refresh", }, }, + ["show-ref"] = config { flags = { verify = "--verify", }, }, + + ["show-branch"] = config { + flags = { + all = "--all", + }, + }, + ["update-ref"] = config { flags = { create_reflog = "--create-reflog", @@ -281,6 +307,7 @@ local configurations = { message = "-m", }, }, + ["ls-files"] = config { flags = { others = "--others", @@ -290,6 +317,7 @@ local configurations = { full_name = "--full-name", }, }, + ["rev-parse"] = config { flags = { revs_only = "--revs-only", diff --git a/lua/neogit/lib/util.lua b/lua/neogit/lib/util.lua index 578254f88..722c6184b 100644 --- a/lua/neogit/lib/util.lua +++ b/lua/neogit/lib/util.lua @@ -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, @@ -195,4 +204,5 @@ return { deepcopy = deepcopy, trim = trim, parse_command_args = parse_command_args, + pattern_escape = pattern_escape, } diff --git a/lua/neogit/popups/rebase.lua b/lua/neogit/popups/rebase.lua index 0725c1a4e..70999e2cd 100644 --- a/lua/neogit/popups/rebase.lua +++ b/lua/neogit/popups/rebase.lua @@ -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") @@ -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)