diff --git a/lib/baes/actions/bisect.rb b/lib/baes/actions/bisect.rb index 3798609..386402a 100644 --- a/lib/baes/actions/bisect.rb +++ b/lib/baes/actions/bisect.rb @@ -51,10 +51,7 @@ def find_middle_branch(success_branch, fail_branch) start_number = success_branch.number child_branch = - success_branch.children.find do |next_child_branch| - next_child_branch.base_name == fail_branch.base_name - end - + success_branch.children.find_by_base_name(fail_branch.base_name) start_number ||= child_branch.number middle_number = (start_number + end_number) / 2 @@ -63,9 +60,7 @@ def find_middle_branch(success_branch, fail_branch) while next_branch.number < middle_number next_branch = - next_branch.children.find do |next_child_branch| - next_child_branch.base_name == fail_branch.base_name - end + next_branch.children.find_by_base_name(fail_branch.base_name) end next_branch diff --git a/lib/baes/branch.rb b/lib/baes/branch.rb index cf89122..ef1b7f3 100644 --- a/lib/baes/branch.rb +++ b/lib/baes/branch.rb @@ -12,7 +12,7 @@ def initialize(name) _, base_name, number = name.match(/(\A[a-zA-Z_-]+)(\d+)$/).to_a self.base_name = base_name || name self.number = number && Integer(number, 10) - self.children = [] + self.children = Baes::BranchCollection.new end # return an array for use as a hash key diff --git a/lib/baes/branch_collection.rb b/lib/baes/branch_collection.rb index 1dffc6c..c510758 100644 --- a/lib/baes/branch_collection.rb +++ b/lib/baes/branch_collection.rb @@ -24,11 +24,21 @@ def find_by_name(name) branches.find { |branch| branch.name == name } end + # find a branch by base name + def find_by_base_name(base_name) + branches.find { |branch| branch.base_name == base_name } + end + # add a branch def <<(branch) branches << branch end + # delete a branch if the block returns true + def delete_if(&block) + branches.delete_if(&block) + end + # iterate over each branch with object def each_with_object(obj, &block) branches.each_with_object(obj, &block) @@ -39,6 +49,16 @@ def each(&block) branches.each(&block) end + # return true if there are no branches + def empty? + branches.empty? + end + + # iterate over each branch and return the result + def map(&block) + branches.map(&block) + end + # return the first branch def first branches.first