Skip to content

Commit

Permalink
Extract delete function to git lib, add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey committed Aug 4, 2023
1 parent 023dc56 commit 03f3bed
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lua/neogit/lib/git/branch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ function M.create(name)
cli.branch.name(name).call_interactive()
end

function M.delete(name)
local input = require("neogit.lib.input")
local success = false

if M.is_unmerged(name) then
if
input.get_confirmation(
string.format("'%s' contains unmerged commits! Are you sure you want to delete it?", name),
{ values = { "&Yes", "&No" }, default = 2 }
)
then
cli.branch.delete.force.name(name).call_sync()
success = true
end
else
cli.branch.delete.name(name).call_sync()
success = true
end

return success
end

function M.current()
local head = require("neogit.lib.git").repo.head.branch
if head then
Expand Down
103 changes: 97 additions & 6 deletions tests/specs/neogit/lib/git/branch_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ local status = require("neogit.status")
local plenary_async = require("plenary.async")
local git_harness = require("tests.util.git_harness")
local neogit_util = require("neogit.lib.util")
local util = require("tests.util.util")
local input = require("tests.mocks.input")

describe("git branch lib", function()
describe("#exists", function()
before_each(function()
git_harness.prepare_repository()
plenary_async.util.block_on(status.reset)
require("neogit").setup()
end)

it("returns true when branch exists", function()
Expand All @@ -21,6 +22,97 @@ describe("git branch lib", function()
end)
end)

describe("#is_unmerged", function()
before_each(function()
git_harness.prepare_repository()
plenary_async.util.block_on(status.reset)
end)

it("returns true when feature branch has commits base branch doesn't", function()
util.system([[
git checkout -b a-new-branch
git reset --hard origin/master
touch feature.js
git add .
git commit -m 'some feature'
]])

assert.True(gb.is_unmerged("a-new-branch"))
end)

it("returns false when feature branch is fully merged into base", function()
util.system([[
git checkout -b a-new-branch
git reset --hard origin/master
touch feature.js
git add .
git commit -m 'some feature'
git switch master
git merge a-new-branch
]])

assert.False(gb.is_unmerged("a-new-branch"))
end)

it("allows specifying alternate base branch", function()
util.system([[
git checkout -b main
git checkout -b a-new-branch
touch feature.js
git add .
git commit -m 'some feature'
git switch master
git merge a-new-branch
]])

assert.True(gb.is_unmerged("a-new-branch", "main"))
assert.False(gb.is_unmerged("a-new-branch", "master"))
end)
end)

describe("#delete", function()
before_each(function()
git_harness.prepare_repository()
plenary_async.util.block_on(status.reset)
end)

describe("when branch is unmerged", function()
before_each(function()
util.system([[
git checkout -b a-new-branch
git reset --hard origin/master
touch feature.js
git add .
git commit -m 'some feature'
git switch master
]])
end)

it("prompts user for confirmation (yes) and deletes branch", function()
input.confirmed = true

assert.True(gb.delete("a-new-branch"))
assert.False(vim.tbl_contains(gb.get_local_branches(true), "a-new-branch"))
end)

it("prompts user for confirmation (no) and doesn't delete branch", function()
input.confirmed = false

assert.False(gb.delete("a-new-branch"))
assert.True(vim.tbl_contains(gb.get_local_branches(true), "a-new-branch"))
end)
end)

describe("when branch is merged", function()
it("deletes branch", function()
util.system("git branch a-new-branch")

assert.True(gb.delete("a-new-branch"))
assert.False(vim.tbl_contains(gb.get_local_branches(true), "a-new-branch"))
end)
end)
end)

describe("local branches", function()
local branches = {}

Expand Down Expand Up @@ -49,13 +141,12 @@ describe("git branch lib", function()
git_harness.prepare_repository()
plenary_async.util.block_on(status.reset)
setup_local_git_branches()
require("neogit").setup()
end)

it("properly detects all local branches", function()
local branches_detected = gb.get_local_branches(true)
print("Branches:\n " .. vim.inspect(branches))
print("Branches Detected:\n " .. vim.inspect(branches_detected))
-- print("Branches:\n " .. vim.inspect(branches))
-- print("Branches Detected:\n " .. vim.inspect(branches_detected))
assert.True(neogit_util.lists_equal(branches, branches_detected))
end)

Expand All @@ -67,8 +158,8 @@ describe("git branch lib", function()
neogit_util.remove_item_from_table(branches, "master")

local branches_detected = gb.get_local_branches(false)
print("Branches:\n " .. vim.inspect(branches))
print("Branches Detected:\n " .. vim.inspect(branches_detected))
-- print("Branches:\n " .. vim.inspect(branches))
-- print("Branches Detected:\n " .. vim.inspect(branches_detected))
assert.True(neogit_util.lists_equal(branches, branches_detected))
end)
end)
Expand Down

0 comments on commit 03f3bed

Please sign in to comment.