Skip to content

Commit

Permalink
Support stacked PRs for subfeatures
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Dibowitz <[email protected]>
  • Loading branch information
jaymzh committed Apr 26, 2024
1 parent 43f9272 commit b4b0547
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ AllCops:
Layout/LineLength:
Max: 80

Metrics/BlockNesting:
Enabled: false

Naming/FileName:
Enabled: false

Expand Down
10 changes: 10 additions & 0 deletions bin/sj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ parser = OptionParser.new do |opts|
options['pr_autofill'] = autofill
end

opts.on(
'--[no-]pr-autostack',
'When creating a PR, if this is a subfeature, should we make it a ' +
'PR on the PR for the parent feature. If not specified, we prompt ' +
'when this happens, when true always do this, when false never do ' +
'this. Only applicable when usiing "gh".',
) do |autostack|
options['pr_autofill'] = autostack
end

opts.on('--[no-]use-color', 'Enable color. [default: true]') do |color|
options['color'] = color
end
Expand Down
24 changes: 22 additions & 2 deletions lib/sugarjar/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(options)
SugarJar::Log.debug("Repoconfig: #{@repo_config}")
@color = options['color']
@pr_autofill = options['pr_autofill']
@pr_autostack = options['pr_autostack']
@feature_prefix = options['feature_prefix']
@checks = {}
@main_branch = nil
Expand Down Expand Up @@ -330,9 +331,9 @@ def smartpullrequest(*args)
end

if gh?
curr = current_branch
base = tracked_branch
if @pr_autofill
curr = current_branch
base = tracked_branch
num_commits = git(
'rev-list', '--count', curr, "^#{base}"
).stdout.strip.to_i
Expand All @@ -345,6 +346,19 @@ def smartpullrequest(*args)
args.unshift('--fill')
end
end
if subfeature?(base)
# nil is prompt, true is always, false is never
if @pr_autostack.nil?
$stdout.print(
'It looks like this is a subfeature, would you like to base ' +
"this PR on #{base}? [y/n] ",
)
ans = $stdin.gets.strip
args += ['--base', base] if %w{Y y}.include?(ans)
elsif @pr_autostack
args += ['--base', base]
end
end
SugarJar::Log.trace("Running: gh pr create #{args.join(' ')}")
system(which('gh'), 'pr', 'create', *args)
else
Expand Down Expand Up @@ -857,6 +871,12 @@ def gitup
}
end

# determine if this branch is based on another local branch (i.e. is a
# subfeature). Used to figure out of we should stack the PR
def subfeature?(base)
all_local_branches.reject { |x| x == most_main }.include?(base)
end

def rebase_in_progress?
# for rebase without -i
rebase_file = git('rev-parse', '--git-path', 'rebase-apply').stdout.strip
Expand Down
1 change: 1 addition & 0 deletions lib/sugarjar/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Config
'github_user' => ENV.fetch('USER'),
'fallthru' => true,
'pr_autofill' => true,
'pr_autostack' => nil,
}.freeze

def self._find_ordered_files
Expand Down

0 comments on commit b4b0547

Please sign in to comment.