diff --git a/lib/git_chain/commands.rb b/lib/git_chain/commands.rb index f70c5f4..71b5e58 100644 --- a/lib/git_chain/commands.rb +++ b/lib/git_chain/commands.rb @@ -8,6 +8,7 @@ module Commands autoload :Prune, "git_chain/commands/prune" autoload :Push, "git_chain/commands/push" autoload :Setup, "git_chain/commands/setup" + autoload :Teardown, "git_chain/commands/teardown" ArgError = Class.new(ArgumentError) diff --git a/lib/git_chain/commands/teardown.rb b/lib/git_chain/commands/teardown.rb new file mode 100644 index 0000000..d3bc994 --- /dev/null +++ b/lib/git_chain/commands/teardown.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true +require "optparse" + +module GitChain + module Commands + class Teardown < Command + include Options::ChainName + + def description + "Teardown chain" + end + + def run(options) + if Git.rebase_in_progress? + raise(Abort, "A rebase is in progress. Please finish the rebase first and run 'git chain rebase' after.") + end + + chain = current_chain(options) + + puts_debug("Tearing down chain #{chain.formatted}}}") + + chain.branch_names.each do |b| + Git.set_config("branch.#{b}.chain", nil, scope: :local) + Git.set_config("branch.#{b}.parentBranch", nil, scope: :local) + Git.set_config("branch.#{b}.branchPoint", nil, scope: :local) + end + + puts_success("Removed chain #{chain.formatted}") + end + end + end +end diff --git a/test/git_chain/command/teardown_test.rb b/test/git_chain/command/teardown_test.rb new file mode 100644 index 0000000..bf9c414 --- /dev/null +++ b/test/git_chain/command/teardown_test.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true +require "test_helper" + +module GitChain + module Commands + class TeardownTest < MiniTest::Test + include RepositoryTestHelper + + def test_tearing_down_a_clean_chain + capture_io do + with_test_repository("a-b-chain") do + assert_equal(%w(master a b), Models::Chain.from_config("default").branch_names) + + Teardown.new.call + + assert(Models::Chain.from_config("default").empty?) + end + end + end + + def test_rebase_in_progress + capture_io do + with_test_repository("a-b-conflicts") do + %x(git rebase --onto a b^ b) + exception = assert_raises(Abort) do + Teardown.new.call + end + + assert_match(/rebase is in progress/, exception.message) + end + end + end + end + end +end