Skip to content

Commit

Permalink
infer root branch globally (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
mockdeep authored Aug 30, 2024
1 parent 72f72e7 commit 761cb30
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/baes/actions/build_tree.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 1 addition & 5 deletions lib/baes/branch_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/baes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/baes/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 761cb30

Please sign in to comment.