From 03f3bedf930ac74faee79f3b5bba7932c699042e Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 4 Aug 2023 13:43:33 +0200 Subject: [PATCH] Extract delete function to git lib, add spec --- lua/neogit/lib/git/branch.lua | 22 +++++ tests/specs/neogit/lib/git/branch_spec.lua | 103 +++++++++++++++++++-- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/lua/neogit/lib/git/branch.lua b/lua/neogit/lib/git/branch.lua index aff36b134..bc6fcea44 100644 --- a/lua/neogit/lib/git/branch.lua +++ b/lua/neogit/lib/git/branch.lua @@ -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 diff --git a/tests/specs/neogit/lib/git/branch_spec.lua b/tests/specs/neogit/lib/git/branch_spec.lua index b176b5fa0..da5be04a1 100644 --- a/tests/specs/neogit/lib/git/branch_spec.lua +++ b/tests/specs/neogit/lib/git/branch_spec.lua @@ -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() @@ -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 = {} @@ -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) @@ -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)