diff --git a/lib/baes/actions/build_tree.rb b/lib/baes/actions/build_tree.rb index b4c7e1e..07b2322 100644 --- a/lib/baes/actions/build_tree.rb +++ b/lib/baes/actions/build_tree.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -SKIP_BRANCHES = ["staging", "main", "master"].freeze - # class that generates a tree of dependent branches class Baes::Actions::BuildTree class << self @@ -43,7 +41,7 @@ def prune(branch) end def link_branch_to_parent(branch, indexed_branches, root_branch:) - return if branch == root_branch || SKIP_BRANCHES.include?(branch.name) + return if branch == root_branch parent_branch = indexed_branches.fetch(parent_name(branch), root_branch) diff --git a/lib/baes/branch_collection.rb b/lib/baes/branch_collection.rb index c510758..f219db5 100644 --- a/lib/baes/branch_collection.rb +++ b/lib/baes/branch_collection.rb @@ -12,11 +12,7 @@ def initialize # return the root branch def root - if root_name - find_by_name(root_name) - else - find_by_name("main") || find_by_name("master") - end + find_by_name(root_name) end # find a branch by name diff --git a/lib/baes/configuration.rb b/lib/baes/configuration.rb index 4b67654..eecd5d8 100644 --- a/lib/baes/configuration.rb +++ b/lib/baes/configuration.rb @@ -34,7 +34,15 @@ def self.output=(output) # return the configured root_name def self.root_name - @root_name + @root_name ||= + begin + root = (["main", "master"] & git.branch_names).first + + message = "unable to infer root branch, please specify with -r" + raise Baes::Git::GitError, message unless root + + root + end end # allow setting the root_name diff --git a/spec/baes/configuration_spec.rb b/spec/baes/configuration_spec.rb index fd1a9b6..a57e386 100644 --- a/spec/baes/configuration_spec.rb +++ b/spec/baes/configuration_spec.rb @@ -1,6 +1,32 @@ # frozen_string_literal: true RSpec.describe Baes::Configuration do + describe ".root_name" do + it "returns the root branch name when set" do + described_class.root_name = "moon" + + expect(described_class.root_name).to eq("moon") + end + + it "defaults to 'main' when present" do + FakeGit.branch_names = ["main", "master"] + + expect(described_class.root_name).to eq("main") + end + + it "defaults to 'master' when 'main' is not present" do + FakeGit.branch_names = ["master"] + + expect(described_class.root_name).to eq("master") + end + + it "raises a GitError when neither 'main' nor 'master' are present" do + FakeGit.branch_names = ["moon"] + + expect { described_class.root_name }.to raise_error(Baes::Git::GitError) + end + end + describe ".auto_skip?" do it "defaults to false" do expect(described_class.auto_skip?).to be(false)