Class: SugarJar::Commands
- Inherits:
-
Object
- Object
- SugarJar::Commands
- Includes:
- Util
- Defined in:
- lib/sugarjar/commands.rb
Overview
This is the workhorse of SugarJar. Short of #initialize, all other public methods are “commands”. Anything in private is internal implementation details.
Constant Summary collapse
- MAIN_BRANCHES =
%w{master main}.freeze
Instance Method Summary collapse
- #amend(*args) ⇒ Object
- #bclean(name = nil) ⇒ Object
- #bcleanall ⇒ Object
- #binfo ⇒ Object
- #br ⇒ Object
- #co(*args) ⇒ Object
- #feature(name, base = nil) ⇒ Object (also: #f)
- #forcepush(remote = nil, branch = nil) ⇒ Object (also: #fpush)
-
#initialize(options) ⇒ Commands
constructor
A new instance of Commands.
- #lint ⇒ Object
- #pullsuggestions ⇒ Object (also: #ps)
- #qamend(*args) ⇒ Object (also: #amendq)
- #smartclone(repo, dir = nil, *args) ⇒ Object (also: #sclone)
-
#smartlog ⇒ Object
(also: #sl)
binfo for all branches.
- #smartpullrequest(*args) ⇒ Object (also: #spr, #smartpr)
- #smartpush(remote = nil, branch = nil) ⇒ Object (also: #spush)
- #subfeature(name) ⇒ Object (also: #sf)
- #unit ⇒ Object
- #up(branch = nil) ⇒ Object
- #upall ⇒ Object
- #version ⇒ Object
Methods included from Util
#gh, #gh_nofail, #git, #git_nofail, #hub, #hub_nofail, #in_repo, #repo_name, #repo_root, #which, #which_nofail
Constructor Details
#initialize(options) ⇒ Commands
Returns a new instance of Commands.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sugarjar/commands.rb', line 17 def initialize() SugarJar::Log.debug("Commands.initialize options: #{}") @ghuser = ['github_user'] @ghhost = ['github_host'] @ignore_dirty = ['ignore_dirty'] @ignore_prerun_failure = ['ignore_prerun_failure'] @repo_config = SugarJar::RepoConfig.config SugarJar::Log.debug("Repoconfig: #{@repo_config}") @color = ['color'] @pr_autofill = ['pr_autofill'] @pr_autostack = ['pr_autostack'] @feature_prefix = ['feature_prefix'] @checks = {} @main_branch = nil @main_remote_branches = {} return if ['no_change'] # technically this doesn't "change" things, but we won't have this # option on the no_change call @cli = determine_cli(['github_cli']) set_hub_host set_commit_template if @repo_config['commit_template'] end |
Instance Method Details
#amend(*args) ⇒ Object
181 182 183 184 185 |
# File 'lib/sugarjar/commands.rb', line 181 def amend(*args) assert_in_repo # This cannot use shellout since we need a full terminal for the editor exit(system(which('git'), 'commit', '--amend', *args)) end |
#bclean(name = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sugarjar/commands.rb', line 69 def bclean(name = nil) assert_in_repo name ||= current_branch name = fprefix(name) if clean_branch(name) SugarJar::Log.info("#{name}: #{color('reaped', :green)}") else die( "#{color("Cannot clean #{name}", :red)}! there are unmerged " + "commits; use 'git branch -D #{name}' to forcefully delete it.", ) end end |
#bcleanall ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sugarjar/commands.rb', line 83 def bcleanall assert_in_repo curr = current_branch all_local_branches.each do |branch| if MAIN_BRANCHES.include?(branch) SugarJar::Log.debug("Skipping #{branch}") next end if clean_branch(branch) SugarJar::Log.info("#{branch}: #{color('reaped', :green)}") else SugarJar::Log.info("#{branch}: skipped") SugarJar::Log.debug( "There are unmerged commits; use 'git branch -D #{branch}' to " + 'forcefully delete it)', ) end end # Return to the branch we were on, or main if all_local_branches.include?(curr) git('checkout', curr) else checkout_main_branch end end |
#binfo ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/sugarjar/commands.rb', line 131 def binfo assert_in_repo SugarJar::Log.info(git( 'log', '--graph', '--oneline', '--decorate', '--boundary', "#{tracked_branch}.." ).stdout.chomp) end |
#br ⇒ Object
126 127 128 129 |
# File 'lib/sugarjar/commands.rb', line 126 def br assert_in_repo SugarJar::Log.info(git('branch', '-v').stdout.chomp) end |
#co(*args) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sugarjar/commands.rb', line 111 def co(*args) assert_in_repo # Pop the last arguement, which is _probably_ a branch name # and then add any featureprefix, and if _that_ is a branch # name, replace the last arguement with that name = args.last bname = fprefix(name) if all_local_branches.include?(bname) SugarJar::Log.debug("Featurepefixing #{name} -> #{bname}") args[-1] = bname end s = git('checkout', *args) SugarJar::Log.info(s.stderr + s.stdout.chomp) end |
#feature(name, base = nil) ⇒ Object Also known as: f
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sugarjar/commands.rb', line 42 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 # If our base is a local branch, don't try to parse it for a remote name unless all_local_branches.include?(base) base_pieces = base.split('/') git('fetch', base_pieces[0]) if base_pieces.length > 1 end git('checkout', '-b', name, base) git('branch', '-u', base) SugarJar::Log.info( "Created feature branch #{color(name, :green)} based on " + color(base, :green), ) end |
#forcepush(remote = nil, branch = nil) ⇒ Object Also known as: fpush
306 307 308 309 |
# File 'lib/sugarjar/commands.rb', line 306 def forcepush(remote = nil, branch = nil) assert_in_repo _smartpush(remote, branch, true) end |
#lint ⇒ Object
289 290 291 292 |
# File 'lib/sugarjar/commands.rb', line 289 def lint assert_in_repo exit(1) unless run_check('lint') end |
#pullsuggestions ⇒ Object Also known as: ps
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/sugarjar/commands.rb', line 373 def pullsuggestions assert_in_repo if dirty? if @ignore_dirty SugarJar::Log.warn( 'Your repo is dirty, but --ignore-dirty was specified, so ' + 'carrying on anyway.', ) else SugarJar::Log.error( 'Your repo is dirty, so I am not going to push. Please commit ' + 'or amend first.', ) exit(1) end end src = "origin/#{current_branch}" fetch('origin') diff = git('diff', "..#{src}").stdout return unless diff && !diff.empty? puts "Will merge the following suggestions:\n\n#{diff}" loop do $stdout.print("\nAre you sure? [y/n] ") ans = $stdin.gets.strip case ans when /^[Yy]$/ system(which('git'), 'merge', '--ff', "origin/#{current_branch}") break when /^[Nn]$/, /^[Qq](uit)?/ puts 'Not merging at user request...' break else puts "Didn't understand '#{ans}'." end end end |
#qamend(*args) ⇒ Object Also known as: amendq
187 188 189 190 |
# File 'lib/sugarjar/commands.rb', line 187 def qamend(*args) assert_in_repo SugarJar::Log.info(git('commit', '--amend', '--no-edit', *args).stdout) end |
#smartclone(repo, dir = nil, *args) ⇒ Object Also known as: sclone
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/sugarjar/commands.rb', line 216 def smartclone(repo, dir = nil, *args) # If the user has specified a hub host, set the environment variable # since we don't have a repo to configure yet ENV['GITHUB_HOST'] = @ghhost if @ghhost reponame = File.basename(repo, '.git') dir ||= reponame org = extract_org(repo) SugarJar::Log.info("Cloning #{reponame}...") # GH's 'fork' command (with the --clone arg) will fork, if necessary, # then clone, and then setup the remotes with the appropriate names. So # we just let it do all the work for us and return. # # Unless the repo is in our own org and cannot be forked, then it # will fail. if gh? && org != @ghuser ghcli('repo', 'fork', '--clone', canonicalize_repo(repo), dir, *args) SugarJar::Log.info('Remotes "origin" and "upstream" configured.') return end # For 'hub' first we clone, using git, as 'hub' always needs a repo to # operate on. # # Or for 'gh' when we can't fork... git('clone', canonicalize_repo(repo), dir, *args) # Then we go into it and attempt to use the 'fork' capability # or if not Dir.chdir dir do # Now that we have a repo, if we have a hub host set it. set_hub_host SugarJar::Log.debug("Comparing org #{org} to ghuser #{@ghuser}") if org == @ghuser puts 'Cloned forked or self-owned repo. Not creating "upstream".' SugarJar::Log.info('Remotes "origin" and "upstream" configured.') return end s = ghcli_nofail('repo', 'fork', '--remote-name=origin') if s.error? if s.stdout.include?('SAML enforcement') SugarJar::Log.info( 'Forking the repo failed because the repo requires SAML ' + "authentication. Full output:\n\n\t#{s.stdout}", ) exit(1) else # gh as well as old versions of hub, it would fail if the upstream # fork already existed. If we got an error, but didn't recognize # that, we'll assume that's what happened and try to add the remote # ourselves. SugarJar::Log.info("Fork (#{@ghuser}/#{reponame}) detected.") SugarJar::Log.debug( 'The above is a bit of a lie. "hub" failed to fork and it was ' + 'not a SAML error, so our best guess is that a fork exists ' + 'and so we will try to configure it.', ) git('remote', 'rename', 'origin', 'upstream') git('remote', 'add', 'origin', forked_repo(repo, @ghuser)) end else SugarJar::Log.info("Forked #{reponame} to #{@ghuser}") end SugarJar::Log.info('Remotes "origin" and "upstream" configured.') end end |
#smartlog ⇒ Object Also known as: sl
binfo for all branches
140 141 142 143 144 145 146 |
# File 'lib/sugarjar/commands.rb', line 140 def smartlog assert_in_repo SugarJar::Log.info(git( 'log', '--graph', '--oneline', '--decorate', '--boundary', '--branches', "#{most_main}.." ).stdout.chomp) end |
#smartpullrequest(*args) ⇒ Object Also known as: spr, smartpr
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/sugarjar/commands.rb', line 321 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. ' + 'You should commit or amend and push it to your remote first.', ) exit(1) end if gh? curr = current_branch base = tracked_branch if @pr_autofill 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 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 SugarJar::Log.trace("Running: hub pull-request #{args.join(' ')}") system(which('hub'), 'pull-request', *args) end end |
#smartpush(remote = nil, branch = nil) ⇒ Object Also known as: spush
299 300 301 302 |
# File 'lib/sugarjar/commands.rb', line 299 def smartpush(remote = nil, branch = nil) assert_in_repo _smartpush(remote, branch, false) end |
#subfeature(name) ⇒ Object Also known as: sf
62 63 64 65 66 |
# File 'lib/sugarjar/commands.rb', line 62 def subfeature(name) assert_in_repo SugarJar::Log.debug("Subfature: #{name}") feature(name, current_branch) end |
#unit ⇒ Object
294 295 296 297 |
# File 'lib/sugarjar/commands.rb', line 294 def unit assert_in_repo exit(1) unless run_check('unit') end |
#up(branch = nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/sugarjar/commands.rb', line 150 def up(branch = nil) assert_in_repo branch ||= current_branch branch = fprefix(branch) # get a copy of our current branch, if rebase fails, we won't # be able to determine it without backing out curr = current_branch git('checkout', branch) result = gitup if result['so'].error? backout = '' if rebase_in_progress? backout = ' You can get out of this with a `git rebase --abort`.' end die( "#{color(curr, :red)}: Failed to rebase on " + "#{result['base']}. Leaving the repo as-is.#{backout} " + 'Output from failed rebase is: ' + "\nSTDOUT:\n#{result['so'].stdout.lines.map { |x| "\t#{x}" }.join}" + "\nSTDERR:\n#{result['so'].stderr.lines.map { |x| "\t#{x}" }.join}", ) else SugarJar::Log.info( "#{color(current_branch, :green)} rebased on #{result['base']}", ) # go back to where we were if we rebased a different branch git('checkout', curr) if branch != curr end end |
#upall ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/sugarjar/commands.rb', line 194 def upall assert_in_repo all_local_branches.each do |branch| next if MAIN_BRANCHES.include?(branch) git('checkout', branch) result = gitup if result['so'].error? SugarJar::Log.error( "#{color(branch, :red)} failed rebase. Reverting attempt and " + 'moving to next branch. Try `sj up` manually on that branch.', ) git('rebase', '--abort') if rebase_in_progress? else SugarJar::Log.info( "#{color(branch, :green)} rebased on " + color(result['base'], :green).to_s, ) end end end |
#version ⇒ Object
313 314 315 316 317 318 319 |
# File 'lib/sugarjar/commands.rb', line 313 def version puts "sugarjar version #{SugarJar::VERSION}" puts ghcli('version').stdout # 'hub' prints the 'git' version, but gh doesn't, so if we're on 'gh' # print out the git version directly puts git('version').stdout if gh? end |