Skip to content

Commit

Permalink
move logic to generate branches to BuildTree (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
mockdeep authored Jun 13, 2024
1 parent cdff082 commit 422ecfe
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 76 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Layout/LineLength:
Max: 80

Bundler/GemComment: { Enabled: false }
Layout/SingleLineBlockChain: { Enabled: false }
Lint/ConstantResolution: { Enabled: false }
RSpec/StubbedMock: { Enabled: false }
Style/ConstantVisibility: { Enabled: false }
Expand Down
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 400`
# on 2024-06-12 23:47:02 UTC using RuboCop version 1.64.1.
# on 2024-06-13 00:13:12 UTC using RuboCop version 1.64.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -28,11 +28,12 @@ Metrics/AbcSize:
Exclude:
- 'lib/baes/actions/bisect.rb'

# Offense count: 4
# Offense count: 5
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Exclude:
- 'lib/baes/actions/bisect.rb'
- 'lib/baes/actions/build_tree.rb'
- 'lib/baes/actions/run.rb'

# Offense count: 1
Expand All @@ -43,12 +44,11 @@ RSpec/BeNil:
Exclude:
- 'spec/baes_spec.rb'

# Offense count: 16
# Offense count: 10
# Configuration parameters: Max, CountAsOne.
RSpec/ExampleLength:
Exclude:
- 'spec/baes/actions/bisect_spec.rb'
- 'spec/baes/actions/build_tree_spec.rb'
- 'spec/baes/actions/rebase_spec.rb'
- 'spec/baes/branch_spec.rb'

Expand Down
9 changes: 1 addition & 8 deletions lib/baes/actions/bisect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class << self
# run the command and return the first branch that fails
def call(command_args)
output.puts("searching for branch that fails command: `#{command_args}`")
branches = generate_branches
branches = Baes::Actions::BuildTree.call
root_branch = find_root_branch(branches)
Baes::Actions::BuildTree.call(branches, root_branch: root_branch)

current_branch_name = git.current_branch_name
current_branch =
Expand All @@ -32,12 +31,6 @@ def call(command_args)

private

def generate_branches
git.branch_names.map do |branch_name|
Baes::Branch.new(branch_name)
end
end

def find_root_branch(branches)
if root_name
branches.find { |branch| branch.name == root_name }
Expand Down
19 changes: 18 additions & 1 deletion lib/baes/actions/build_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class << self
include Baes::Configuration::Helpers

# generate a tree of Branch records linked to their children
def call(branches, root_branch:)
def call
branches = generate_branches
root_branch = find_root_branch(branches)
indexed_branches = index_branches(branches)

branches.each do |branch|
Expand All @@ -20,10 +22,25 @@ def call(branches, root_branch:)
end

prune(root_branch)
branches
end

private

def find_root_branch(branches)
if root_name
branches.find { |branch| branch.name == root_name }
else
branches.find { |branch| ["main", "master"].include?(branch.name) }
end
end

def generate_branches
git.branch_names.map do |branch_name|
Baes::Branch.new(branch_name)
end
end

def prune(branch)
branch.children.delete_if do |child|
ignored_branch_names.include?(child.name)
Expand Down
9 changes: 1 addition & 8 deletions lib/baes/actions/rebase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class << self

# rebase branches
def call
branches = generate_branches
branches = Baes::Actions::BuildTree.call
root_branch = find_root_branch(branches)
Baes::Actions::BuildTree.call(branches, root_branch: root_branch)

if dry_run?
output.puts(root_branch.inspect)
Expand All @@ -22,12 +21,6 @@ def call

private

def generate_branches
git.branch_names.map do |branch_name|
Baes::Branch.new(branch_name)
end
end

def find_root_branch(branches)
if root_name
branches.find { |branch| branch.name == root_name }
Expand Down
69 changes: 24 additions & 45 deletions spec/baes/actions/build_tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,69 @@
RSpec.describe Baes::Actions::BuildTree do
describe "#call" do
it "links branches to the root branch" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch")
FakeGit.branch_names = ["main", "some_branch"]

described_class.call([branch1, branch2], root_branch: branch1)
branches = described_class.call

expect(branch1.children).to eq([branch2])
expect(branches.first.children.map(&:name)).to eq(["some_branch"])
end

it "links numbered branches to the previous number" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_1")
branch3 = Baes::Branch.new("some_branch_2")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_1", "some_branch_2"]

described_class.call(branches, root_branch: branch1)
branches = described_class.call

expect(branch2.children).to eq([branch3])
expect(branches[1].children.map(&:name)).to eq(["some_branch_2"])
end

it "links numbered branches to the root when no previous number" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_5")
FakeGit.branch_names = ["main", "some_branch_5"]

described_class.call([branch1, branch2], root_branch: branch1)
branches = described_class.call

expect(branch1.children).to eq([branch2])
expect(branches.first.children.map(&:name)).to eq(["some_branch_5"])
end

it "links numbered branches with leading zeros" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_09")
branch3 = Baes::Branch.new("some_branch_10")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_09", "some_branch_10"]

described_class.call(branches, root_branch: branch1)
branches = described_class.call

expect(branch2.children).to eq([branch3])
expect(branches[1].children.map(&:name)).to eq(["some_branch_10"])
end

it "links numbered branches when number of digits differs" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_9")
branch3 = Baes::Branch.new("some_branch_10")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_9", "some_branch_10"]

described_class.call(branches, root_branch: branch1)
branches = described_class.call

expect(branch2.children).to eq([branch3])
expect(branches[1].children.map(&:name)).to eq(["some_branch_10"])
end

it "raises an error when multiple branches have same index" do
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_9")
branch3 = Baes::Branch.new("some_branch_09")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_9", "some_branch_09"]
message = "duplicate branch index [\"some_branch_\", 9]"

expect { described_class.call(branches, root_branch: branch1) }
.to raise_error(Baes::Error, message)
expect { described_class.call }.to raise_error(Baes::Error, message)
end

it "prunes ignored branches" do
Baes::Configuration.ignored_branch_names = ["some_branch_10"]
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_9")
branch3 = Baes::Branch.new("some_branch_10")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_9", "some_branch_10"]

described_class.call(branches, root_branch: branch1)
branches = described_class.call

expect(branch1.children).to eq([branch2])
expect(branch2.children).to be_empty
expect(branches.first.children.map(&:name)).to eq(["some_branch_9"])
expect(branches[1].children).to be_empty
end

it "prunes descendant branches" do
Baes::Configuration.ignored_branch_names = ["some_branch_9"]
branch1 = Baes::Branch.new("main")
branch2 = Baes::Branch.new("some_branch_9")
branch3 = Baes::Branch.new("some_branch_10")
branches = [branch1, branch2, branch3]
FakeGit.branch_names = ["main", "some_branch_9", "some_branch_10"]

described_class.call(branches, root_branch: branch1)
branches = described_class.call

expect(branch1.children).to be_empty
expect(branches.first.children).to be_empty
end
end
end
9 changes: 3 additions & 6 deletions spec/baes/actions/rebase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
it "rebases branches on main" do
FakeGit.branch_names = ["main", "my_branch"]

expect { described_class.call }
.to rebase("my_branch").on("main")
expect { described_class.call }.to rebase("my_branch").on("main")
end

it "rebases branches on master" do
FakeGit.branch_names = ["master", "my_branch"]

expect { described_class.call }
.to rebase("my_branch").on("master")
expect { described_class.call }.to rebase("my_branch").on("master")
end

it "rebases chained branches" do
Expand Down Expand Up @@ -46,8 +44,7 @@
FakeGit.branch_names = ["main", "my_branch_1", "my_branch_2"]
Baes::Configuration.dry_run = true

expect { described_class.call }
.not_to(rebase("my_branch_1").on("main"))
expect { described_class.call }.not_to(rebase("my_branch_1").on("main"))
end
end

Expand Down
6 changes: 2 additions & 4 deletions spec/baes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
"Don't use system calls. Use `Open3.capture3` instead. " \
"Called with `echo 'blah'`"

expect { `echo 'blah'` }
.to raise_error(TestingError, message)
expect { `echo 'blah'` }.to raise_error(TestingError, message)
end

it "does not allow system calls" do
message =
"Don't use system calls. Use `Open3.capture3` instead. " \
"Called with `echo 'blah'`"

expect { system("echo 'blah'") }
.to raise_error(TestingError, message)
expect { system("echo 'blah'") }.to raise_error(TestingError, message)
end
end

0 comments on commit 422ecfe

Please sign in to comment.