From 7eb41a572c1abea0f4184ee56dbd304fb65e4aae Mon Sep 17 00:00:00 2001 From: Phil Dibowitz Date: Thu, 21 Mar 2024 11:44:40 -0700 Subject: [PATCH] SPR: don't autofill when more than one commit exists * If > 1 commit exists, don't pass in --fill * Add a new option to allow people to opt-out of --fill in all cases * Tell people when we are autofilling Closes #152 Signed-off-by: Phil Dibowitz --- bin/sj | 8 ++++++++ lib/sugarjar/commands.rb | 20 +++++++++++++++++++- lib/sugarjar/config.rb | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bin/sj b/bin/sj index 629efe2..fa86e8f 100755 --- a/bin/sj +++ b/bin/sj @@ -91,6 +91,14 @@ parser = OptionParser.new do |opts| options['log_level'] = level end + opts.on( + '--[no-]pr-autofill', + 'When creating a PR, auto fill the title & description from the commit ' + + 'if there is a single commit and if we are using "gh". [default: true]', + ) do |autofill| + options['pr_autofill'] = autofill + end + opts.on('--[no-]use-color', 'Enable color. [default: true]') do |color| options['color'] = color end diff --git a/lib/sugarjar/commands.rb b/lib/sugarjar/commands.rb index 901a0fb..658c016 100644 --- a/lib/sugarjar/commands.rb +++ b/lib/sugarjar/commands.rb @@ -23,6 +23,7 @@ def initialize(options) @repo_config = SugarJar::RepoConfig.config SugarJar::Log.debug("Repoconfig: #{@repo_config}") @color = options['color'] + @pr_autofill = options['pr_autofill'] @feature_prefix = options['feature_prefix'] @checks = {} @main_branch = nil @@ -318,6 +319,7 @@ def version def smartpullrequest(*args) assert_in_repo assert_common_main_branch + if dirty? SugarJar::Log.warn( 'Your repo is dirty, so I am not going to create a pull request. ' + @@ -325,9 +327,25 @@ def smartpullrequest(*args) ) exit(1) end + if gh? + if @pr_autofill + curr = current_branch + base = tracked_branch + num_commits = git( + 'rev-list', '--count', curr, "^#{base}" + ).stdout.strip.to_i + if num_commits > 1 + SugarJar::Log.debug( + "Not using --fill because there are #{num_commits} commits", + ) + else + SugarJar::Log.info('Autofilling in PR from commit message') + args.unshift('--fill') + end + end SugarJar::Log.trace("Running: gh pr create #{args.join(' ')}") - system(which('gh'), 'pr', 'create', '--fill', *args) + system(which('gh'), 'pr', 'create', *args) else SugarJar::Log.trace("Running: hub pull-request #{args.join(' ')}") system(which('hub'), 'pull-request', *args) diff --git a/lib/sugarjar/config.rb b/lib/sugarjar/config.rb index 9777e30..71c168b 100644 --- a/lib/sugarjar/config.rb +++ b/lib/sugarjar/config.rb @@ -9,6 +9,7 @@ class Config 'github_cli' => 'auto', 'github_user' => ENV.fetch('USER'), 'fallthru' => true, + 'pr_autofill' => true, }.freeze def self._find_ordered_files