From 9be5b064a83a44f50d18b56e3b356df6699cd00e Mon Sep 17 00:00:00 2001 From: Phil Dibowitz Date: Tue, 25 Aug 2026 12:43:14 -0700 Subject: [PATCH] Add ability to specify primary branch in repoconfig Closes: #291 Signed-off-by: Phil Dibowitz --- examples/sample_repoconfig.yaml | 8 ++++++++ lib/sugarjar/commands.rb | 4 ++-- lib/sugarjar/commands/bclean.rb | 12 ++++++++---- lib/sugarjar/commands/up.rb | 5 ++--- spec/commands/up_spec.rb | 4 ++++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/examples/sample_repoconfig.yaml b/examples/sample_repoconfig.yaml index 7c0f243..8839bb7 100644 --- a/examples/sample_repoconfig.yaml +++ b/examples/sample_repoconfig.yaml @@ -20,6 +20,14 @@ include_from: .sugarjar_local.yaml overwrite_from: .sugarjar_local_overwrite.yaml +# `primary_branch` allows you to configure a primary branch that is +# non-standard. If your primary branch is `main` or `master`, you do not need +# to set this, sugarjar will figure out out automatically. +# +# This can also be used to force a primary branch in the event you have +# both `main` and `master` around. +primary_branch: somethingweird + # `release_branches` tells SugarJar several things: # 1. These branches should not be repead when running `bclean`/`bcleanall`. # 2. When a feature-branch is made from a release branch (e.g. `2.x-branch`), diff --git a/lib/sugarjar/commands.rb b/lib/sugarjar/commands.rb index 369c59c..7d854d4 100644 --- a/lib/sugarjar/commands.rb +++ b/lib/sugarjar/commands.rb @@ -23,8 +23,6 @@ class SugarJar # methods are "commands". Anything in private is internal implementation # details. class Commands - MAIN_BRANCHES = %w{master main}.freeze - def initialize(options) SugarJar::Log.debug("Commands.initialize options: #{options}") @config = options @@ -196,6 +194,8 @@ def dirty_check! end def determine_main_branch(branches) + return @repo_config['primary_branch'] if @repo_config['primary_branch'] + if branches.include?('main') 'main' elsif branches.include?('master') diff --git a/lib/sugarjar/commands/bclean.rb b/lib/sugarjar/commands/bclean.rb index 1ac24b2..e821312 100644 --- a/lib/sugarjar/commands/bclean.rb +++ b/lib/sugarjar/commands/bclean.rb @@ -1,5 +1,8 @@ class SugarJar class Commands + # only used for REMOTE branch cleanup. use main_branch() for everything else + MAIN_BRANCHES = %w{master main}.freeze + def lbclean(name = nil) assert_in_repo! name ||= current_branch @@ -66,7 +69,7 @@ def lbcleanall all_local_branches.each do |branch| # skip_branch info will check for MAIN_BRANCHES, but we # quietly skip them. - next if MAIN_BRANCHES.include?(branch) + next if main_branch == branch should_skip, why = skip_branch_info(branch) if should_skip @@ -103,7 +106,8 @@ def rbcleanall(remote = nil) curr = current_branch remote ||= 'origin' all_remote_branches(remote).each do |branch| - if (MAIN_BRANCHES + ['HEAD']).include?(branch) + # for remote branches, exclude all POSSIBLE primary branch names + if MAIN_BRANCHES.union([main_branch, 'HEAD']).include?(branch) SugarJar::Log.debug("Skipping #{branch}") next end @@ -140,7 +144,7 @@ def gbcleanall(remote = nil) # rubocop:disable Naming/PredicateMethod def clean_branch(name, type = :local) - undeleteable = MAIN_BRANCHES.dup + undeleteable = [main_branch] undeleteable << 'HEAD' if type == :remote die("Cannot remove #{name} branch") if undeleteable.include?(name) SugarJar::Log.debug('Fetch relevant remote...') @@ -233,7 +237,7 @@ def cleanup_tmp_branch(tmp, backto, tracked = nil) end def skip_branch_info(name) - return true, 'main branch' if MAIN_BRANCHES.include?(name) + return true, 'primary branch' if main_branch == name wt_branches = worktree_branches rel_branches = release_branches diff --git a/lib/sugarjar/commands/up.rb b/lib/sugarjar/commands/up.rb index 7bf34d8..c1a61b4 100644 --- a/lib/sugarjar/commands/up.rb +++ b/lib/sugarjar/commands/up.rb @@ -34,7 +34,7 @@ def up(branch = nil) def upall assert_in_repo! all_local_branches.each do |branch| - next if MAIN_BRANCHES.include?(branch) + next if main_branch == branch git('checkout', branch) result = rebase @@ -126,8 +126,7 @@ def rebase(base = nil) # If this is a subfeature based on a local branch which has since # been deleted, 'tracked branch' will automatically return # so we don't need any special handling for that - if !MAIN_BRANCHES.include?(curr) && base == "origin/#{curr}" && - !skip_base_warning + if main_branch != curr && base == "origin/#{curr}" && !skip_base_warning SugarJar::Log.warn( "This branch is tracking origin/#{curr}, which is probably your " + 'downstream (where you push _to_) as opposed to your upstream ' + diff --git a/spec/commands/up_spec.rb b/spec/commands/up_spec.rb index ff844fc..dd15d5d 100644 --- a/spec/commands/up_spec.rb +++ b/spec/commands/up_spec.rb @@ -11,6 +11,8 @@ expect(sj).to receive(:current_branch).and_return('foo') expect(sj).to receive(:tracked_branch).with(:fallback => false). and_return('upstream/main') + expect(sj).to receive(:all_local_branches).at_least(1).times. + and_return(%w{main foo}) expect(sj).to receive(:git_nofail).with('rebase', 'upstream/main') sj.send(:rebase) end @@ -20,6 +22,8 @@ expect(sj).to receive(:current_branch).and_return('foo') expect(sj).to receive(:tracked_branch).with(:fallback => false). and_return('bar') + expect(sj).to receive(:all_local_branches).at_least(1).times. + and_return(%w{main foo}) expect(sj).to receive(:git_nofail).with('rebase', 'bar') sj.send(:rebase) end