Skip to content

Commit

Permalink
New feature: feature prefixes
Browse files Browse the repository at this point in the history
This adds a new feature, feature prefixes, which will create all
features with a prefix, which is useful in branch-based workflows
where developers are expected to create all branches with a specific
prefix like "$USER/".

Signed-off-by: Phil Dibowitz <phil@ipom.com>
  • Loading branch information
jaymzh committed Oct 23, 2023
1 parent d7ebf05 commit bff501f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions bin/sj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ parser = OptionParser.new do |opts|
options['fallthru'] = fallthru
end

opts.on('--feature-prefix', 'Prefix to use for feature branches') do |prefix|
options['feature_prefix'] = prefix
end

opts.on(
'--github-cli CLI',
%w{gh cli},
Expand Down Expand Up @@ -129,6 +133,12 @@ COMMANDS:
of preference it will be upstream/master, origin/master, master,
depending upon what remotes are available.
Note that you can specify "--feature-prefix" (or add
"feature_prefix" to your config) to have all features created
with a prefix. This is useful for branch-based workflows where
developers are expected to create branches names that, for
example, start with their username.
forcepush, fpush
The same as "smartpush", but uses "--force-with-lease". This is
a "safer" way of doing force-pushes and is the recommended way
Expand Down
15 changes: 14 additions & 1 deletion lib/sugarjar/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(options)
@ignore_prerun_failure = options['ignore_prerun_failure']
@repo_config = SugarJar::RepoConfig.config
@color = options['color']
@feature_prefix = options['feature_prefix']
@checks = {}
@main_branch = nil
@main_remote_branches = {}
Expand All @@ -35,6 +36,7 @@ def initialize(options)
def feature(name, base = nil)
assert_in_repo
SugarJar::Log.debug("Feature: #{name}, #{base}")
name = fprefix(name)
die("#{name} already exists!") if all_local_branches.include?(name)
base ||= most_main
base_pieces = base.split('/')
Expand All @@ -49,6 +51,7 @@ def feature(name, base = nil)
def bclean(name = nil)
assert_in_repo
name ||= current_branch
name = fprefix(name) unless all_local_branches.include?(name)
if clean_branch(name)
SugarJar::Log.info("#{name}: #{color('reaped', :green)}")
else
Expand Down Expand Up @@ -345,6 +348,16 @@ def pullsuggestions

private

def fprefix(name)
return name unless @feature_prefix

newname = "#{@feature_prefix}#{name}"
SugarJar::Log.debug(
"Munging feature name: #{name} -> #{newname} due to feature prefix",
)
newname
end

def _smartpush(remote, branch, force)
unless remote && branch
remote ||= 'origin'
Expand Down Expand Up @@ -692,7 +705,7 @@ def safe_to_clean(branch)
).stdout.lines.reject do |line|
line.start_with?('-')
end
if out.length.zero?
if out.empty?
SugarJar::Log.debug(
"cherry-pick shows branch #{branch} obviously safe to delete",
)
Expand Down
14 changes: 14 additions & 0 deletions spec/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,18 @@
to eq('git@github.com:org/repo.git')
end
end

context '#fprefix' do
it 'Adds prefixes when needed' do
sj = SugarJar::Commands.new(
{ 'no_change' => true, 'feature_prefix' => 'someuser/' },
)
expect(sj.send(:fprefix, 'test')).to eq('someuser/test')
end

it 'Does not add prefixes when not needed' do
sj = SugarJar::Commands.new({ 'no_change' => true })
expect(sj.send(:fprefix, 'test')).to eq('test')
end
end
end

0 comments on commit bff501f

Please sign in to comment.