Skip to content

Commit

Permalink
Merge pull request NeogitOrg#1011 from NeogitOrg/fix/rebase-root
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey authored Dec 12, 2023
2 parents 0cdc73e + 98d3064 commit 38a62b3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions lua/neogit/lib/git/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ local configurations = {

["rev-list"] = config {
flags = {
merges = "--merges",
parents = "--parents",
},
options = {
Expand Down
7 changes: 7 additions & 0 deletions lua/neogit/lib/git/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ function M.is_ancestor(a, b)
return cli["merge-base"].is_ancestor.args(a, b).call_sync({ ignore_error = true, hidden = true }).code == 0
end

---Finds parent commit of a commit. If no parent exists, will return nil
---@param commit string
---@return string|nil
function M.parent(commit)
return vim.split(cli["rev-list"].max_count(1).parents.args(commit).call({ hidden = true }).stdout[1], " ")[2]
end

local function update_recent(state)
local count = config.values.status.recent_commit_count
if count < 1 then
Expand Down
4 changes: 4 additions & 0 deletions lua/neogit/lib/git/rebase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ local function rebase_command(cmd)
end

function M.rebase_interactive(commit, args)
if vim.tbl_contains(args, "--root") then
commit = ""
end

local result = rebase_command(cli.rebase.interactive.args(commit).arg_list(args))
if result.code ~= 0 then
notification.error("Rebasing failed. Resolve conflicts before continuing")
Expand Down
4 changes: 2 additions & 2 deletions lua/neogit/lib/git/remote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ function M.prune(name)
end

function M.list()
return cli.remote.call_sync().stdout
return cli.remote.call_sync({ hidden = true }).stdout
end

function M.get_url(name)
return cli.remote.get_url(name).call().stdout
return cli.remote.get_url(name).call({ hidden = true }).stdout
end

return M
33 changes: 29 additions & 4 deletions lua/neogit/popups/rebase/actions.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local git = require("neogit.lib.git")
local input = require("neogit.lib.input")
local notification = require("neogit.lib.notification")

local CommitSelectViewBuffer = require("neogit.buffers.commit_select_view")
local FuzzyFinderBuffer = require("neogit.buffers.fuzzy_finder")
Expand Down Expand Up @@ -65,14 +66,38 @@ function M.interactively(popup)
if popup.state.env.commit then
commit = popup.state.env.commit
else
commit = CommitSelectViewBuffer.new(git.log.list()):open_async()[1]
commit = CommitSelectViewBuffer.new(git.log.list({}, {}, {}, true)):open_async()[1]
end

if commit then
if not git.log.is_ancestor(commit, "HEAD") then
notification.warn("Commit isn't an ancestor of HEAD")
return
end

local args = popup:get_arguments()
local parent_commit = git.cli["rev-list"].max_count(1).parents.args(commit).call().stdout[1]
if commit ~= parent_commit then
commit = vim.split(parent_commit, " ")[2]

local merges = git.cli["rev-list"].merges.args(commit .. "..HEAD").call().stdout
if merges[1] then
local choice = input.get_choice("Proceed despite merge in rebase range?", {
values = { "&continue", "&select other", "&abort" },
default = 1,
})

-- selene: allow(empty_if)
if choice == "c" then
-- no-op
elseif choice == "s" then
popup.state.env.commit = nil
M.interactively(popup)
else
return
end
end

local parent = git.log.parent(commit)
if parent then
commit = commit .. "^"
else
table.insert(args, "--root")
end
Expand Down

0 comments on commit 38a62b3

Please sign in to comment.