Class: SugarJar::Commands

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Methods included from Util

#hub, #hub_nofail, #in_repo, #repo_root, #which, #which_nofail

Constructor Details

#initialize(options) ⇒ Commands

Returns a new instance of Commands.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sugarjar/commands.rb', line 15

def initialize(options)
  SugarJar::Log.debug("Commands.initialize options: #{options}")
  @ghuser = options['github_user']
  @ghhost = options['github_host']
  @ignore_dirty = options['ignore_dirty']
  @ignore_prerun_failure = options['ignore_prerun_failure']
  @repo_config = SugarJar::RepoConfig.config
  @color = options['color']
  return if options['no_change']

  set_hub_host if @ghhost
  set_commit_template if @repo_config['commit_template']
end

Instance Method Details

#amend(*args) ⇒ Object



126
127
128
129
130
# File 'lib/sugarjar/commands.rb', line 126

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sugarjar/commands.rb', line 43

def bclean(name = nil)
  assert_in_repo
  name ||= current_branch
  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

#bcleanallObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sugarjar/commands.rb', line 56

def bcleanall
  assert_in_repo
  curr = current_branch
  all_branches.each do |branch|
    if branch == 'master'
      SugarJar::Log.debug('Skipping master')
      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 master
  if all_branches.include?(curr)
    hub('checkout', curr)
  else
    hub('checkout', 'master')
  end
end

#binfoObject



95
96
97
98
99
100
101
# File 'lib/sugarjar/commands.rb', line 95

def binfo
  assert_in_repo
  SugarJar::Log.info(hub(
    'log', '--graph', '--oneline', '--decorate', '--boundary',
    "#{tracked_branch}.."
  ).stdout.chomp)
end

#brObject



90
91
92
93
# File 'lib/sugarjar/commands.rb', line 90

def br
  assert_in_repo
  SugarJar::Log.info(hub('branch', '-v').stdout.chomp)
end

#co(*args) ⇒ Object



84
85
86
87
88
# File 'lib/sugarjar/commands.rb', line 84

def co(*args)
  assert_in_repo
  s = hub('checkout', *args)
  SugarJar::Log.info(s.stderr + s.stdout.chomp)
end

#feature(name, base = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sugarjar/commands.rb', line 29

def feature(name, base = nil)
  assert_in_repo
  SugarJar::Log.debug("Feature: #{name}, #{base}")
  die("#{name} already exists!") if all_branches.include?(name)
  base ||= most_master
  base_pieces = base.split('/')
  hub('fetch', base_pieces[0]) if base_pieces.length > 1
  hub('checkout', '-b', name, 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



215
216
217
218
# File 'lib/sugarjar/commands.rb', line 215

def forcepush(remote = nil, branch = nil)
  assert_in_repo
  _smartpush(remote, branch, true)
end

#lintObject



198
199
200
201
# File 'lib/sugarjar/commands.rb', line 198

def lint
  assert_in_repo
  exit(1) unless run_check('lint')
end

#qamend(*args) ⇒ Object Also known as: amendq



132
133
134
135
# File 'lib/sugarjar/commands.rb', line 132

def qamend(*args)
  assert_in_repo
  SugarJar::Log.info(hub('commit', '--amend', '--no-edit', *args).stdout)
end

#smartclone(repo, dir = nil, *args) ⇒ Object Also known as: sclone



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sugarjar/commands.rb', line 160

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
  SugarJar::Log.info("Cloning #{reponame}...")
  hub('clone', canonicalize_repo(repo), dir, *args)

  Dir.chdir dir do
    # Now that we have a repo, if we have a hub host set it.
    set_hub_host if @ghhost

    org = extract_org(repo)
    SugarJar::Log.debug("Comparing org #{org} to ghuser #{@ghuser}")
    if org == @ghuser
      puts 'Cloned forked or self-owned repo. Not creating "upstream".'
      return
    end

    s = hub_nofail('fork', '--remote-name=origin')
    if s.error?
      # if the fork command failed, we already have one, so we have
      # to swap the remote names ourselves
      # newer 'hub's don't fail and do the right thing...
      SugarJar::Log.info("Fork (#{@ghuser}/#{reponame}) detected.")
      hub('remote', 'rename', 'origin', 'upstream')
      hub('remote', 'add', 'origin', forked_repo(repo, @ghuser))
    else
      SugarJar::Log.info("Forked #{reponame} to #{@ghuser}")
    end
    SugarJar::Log.info('Remotes "origin" and "upstream" configured.')
  end
end

#smartlogObject Also known as: sl

binfo for all branches



104
105
106
107
108
109
110
# File 'lib/sugarjar/commands.rb', line 104

def smartlog
  assert_in_repo
  SugarJar::Log.info(hub(
    'log', '--graph', '--oneline', '--decorate', '--boundary',
    '--branches', "#{most_master}.."
  ).stdout.chomp)
end

#smartpullrequestObject Also known as: spr, smartpr



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/sugarjar/commands.rb', line 227

def smartpullrequest
  assert_in_repo
  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
  system(which('hub'), 'pull-request')
end

#smartpush(remote = nil, branch = nil) ⇒ Object Also known as: spush



208
209
210
211
# File 'lib/sugarjar/commands.rb', line 208

def smartpush(remote = nil, branch = nil)
  assert_in_repo
  _smartpush(remote, branch, false)
end

#unitObject



203
204
205
206
# File 'lib/sugarjar/commands.rb', line 203

def unit
  assert_in_repo
  exit(1) unless run_check('unit')
end

#upObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sugarjar/commands.rb', line 114

def up
  assert_in_repo
  result = gitup
  if result
    SugarJar::Log.info(
      "#{color(current_branch, :green)} rebased on #{result}",
    )
  else
    die("#{color(current_branch, :red)}: Failed to rebase")
  end
end

#upallObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sugarjar/commands.rb', line 139

def upall
  assert_in_repo
  all_branches.each do |branch|
    next if branch == 'master'

    hub('checkout', branch)
    result = gitup
    if result
      SugarJar::Log.info(
        "#{color(branch, :green)} rebased on #{color(result, :green)}",
      )
    else
      SugarJar::Log.error(
        "#{color(branch, :red)} failed rebase. Reverting attempt and " +
        'moving to next branch',
      )
      hub('rebase', '--abort')
    end
  end
end

#versionObject



222
223
224
225
# File 'lib/sugarjar/commands.rb', line 222

def version
  puts "sugarjar version #{SugarJar::VERSION}"
  puts hub('version').stdout
end