Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to find branches #79

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions lib/baes/actions/bisect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ class << self
def call(command_args)
output.puts("searching for branch that fails command: `#{command_args}`")
branches = Baes::Actions::BuildTree.call
root_branch = find_root_branch(branches)

current_branch_name = git.current_branch_name
current_branch =
branches.find { |branch| branch.name == current_branch_name }
current_branch = branches.find_by_name(current_branch_name)

_, _, status = Open3.capture3(command_args)

Expand All @@ -23,22 +21,14 @@ def call(command_args)
end

fail_branch =
find_failing_branch(root_branch, current_branch, command_args)
find_failing_branch(branches.root, current_branch, command_args)

git.checkout(fail_branch.name)
output.puts("first failing branch: #{fail_branch.name}")
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 find_failing_branch(success_branch, fail_branch, command_args)
next_branch = find_middle_branch(success_branch, fail_branch)

Expand Down
13 changes: 2 additions & 11 deletions lib/baes/actions/build_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ class << self
# generate a tree of Branch records linked to their children
def call
branches = generate_branches
root_branch = find_root_branch(branches)
indexed_branches = index_branches(branches)

branches.each do |branch|
link_branch_to_parent(
branch,
indexed_branches,
root_branch: root_branch
root_branch: branches.root
)
end

prune(root_branch)
prune(branches.root)
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
branches = Baes::BranchCollection.new
git.branch_names.each do |branch_name|
Expand Down
10 changes: 1 addition & 9 deletions lib/baes/actions/rebase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class << self
# rebase branches
def call
branches = Baes::Actions::BuildTree.call
root_branch = find_root_branch(branches)
root_branch = branches.root

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

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 rebase_children(branch)
branch.children.each do |child_branch|
child_branch.rebase(branch)
Expand Down
21 changes: 16 additions & 5 deletions lib/baes/branch_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

# class to encapsulate a collection of branches
class Baes::BranchCollection
include Baes::Configuration::Helpers

attr_accessor :branches

def initialize
self.branches = []
end

# return the root branch
def root
if root_name
find_by_name(root_name)
else
find_by_name("main") || find_by_name("master")
end
end

# find a branch by name
def find_by_name(name)
branches.find { |branch| branch.name == name }
end

# add a branch
def <<(branch)
branches << branch
end

# find a branch
def find(&block)
branches.find(&block)
end

# iterate over each branch with object
def each_with_object(obj, &block)
branches.each_with_object(obj, &block)
Expand Down
Loading