diff --git a/.rubocop.yml b/.rubocop.yml index 9ece655..6556a60 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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 } diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 73fb175..e5870a9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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 @@ -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' diff --git a/lib/baes/actions/bisect.rb b/lib/baes/actions/bisect.rb index 9fcf11c..5267e72 100644 --- a/lib/baes/actions/bisect.rb +++ b/lib/baes/actions/bisect.rb @@ -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 = @@ -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 } diff --git a/lib/baes/actions/build_tree.rb b/lib/baes/actions/build_tree.rb index 82fabd9..c80f88a 100644 --- a/lib/baes/actions/build_tree.rb +++ b/lib/baes/actions/build_tree.rb @@ -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| @@ -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) diff --git a/lib/baes/actions/rebase.rb b/lib/baes/actions/rebase.rb index 283c2e9..bbb4232 100644 --- a/lib/baes/actions/rebase.rb +++ b/lib/baes/actions/rebase.rb @@ -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) @@ -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 } diff --git a/spec/baes/actions/build_tree_spec.rb b/spec/baes/actions/build_tree_spec.rb index 5ae6fc4..c5c398e 100644 --- a/spec/baes/actions/build_tree_spec.rb +++ b/spec/baes/actions/build_tree_spec.rb @@ -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 diff --git a/spec/baes/actions/rebase_spec.rb b/spec/baes/actions/rebase_spec.rb index 7a0943f..88d8341 100644 --- a/spec/baes/actions/rebase_spec.rb +++ b/spec/baes/actions/rebase_spec.rb @@ -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 @@ -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 diff --git a/spec/baes_spec.rb b/spec/baes_spec.rb index 1ae8237..70090c6 100644 --- a/spec/baes_spec.rb +++ b/spec/baes_spec.rb @@ -10,8 +10,7 @@ "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 @@ -19,7 +18,6 @@ "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