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.



13
14
15
16
17
18
19
# File 'lib/sugarjar/commands.rb', line 13

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

Instance Method Details

#amend(*args) ⇒ Object



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

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



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

def bclean(name = nil)
  assert_in_repo
  name ||= current_branch
  # rubocop:disable Style/GuardClause
  unless clean_branch(name)
    die("Cannot clean #{name} - there are unmerged commits")
  end
  # rubocop:enable Style/GuardClause
end

#bcleanallObject



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

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

    # rubocop:disable Style/Next
    unless clean_branch(branch)
      SugarJar::Log.info(
        "Skipping branch #{branch} - there are unmerged commits",
      )
    end
    # rubocop:enable Style/Next
  end
end

#binfoObject



67
68
69
70
71
72
73
# File 'lib/sugarjar/commands.rb', line 67

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

#brObject



62
63
64
65
# File 'lib/sugarjar/commands.rb', line 62

def br
  assert_in_repo
  puts hub('branch', '-v').stdout
end

#co(*args) ⇒ Object



57
58
59
60
# File 'lib/sugarjar/commands.rb', line 57

def co(*args)
  assert_in_repo
  hub('checkout', *args)
end

#feature(name, base = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/sugarjar/commands.rb', line 21

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 #{name} based on #{base}")
end

#forcepush(remote = nil, branch = nil) ⇒ Object Also known as: fpush



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sugarjar/commands.rb', line 176

def forcepush(remote = nil, branch = nil)
  unless remote && branch
    remote ||= 'origin'
    branch ||= current_branch
  end
  if run_prepush
    puts hub('push', '--force-with-lease', remote, branch).stderr
  else
    SugarJar::Log.error('Pre-push checks failed. Not pushing.')
  end
end

#lintObject



153
154
155
# File 'lib/sugarjar/commands.rb', line 153

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

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



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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sugarjar/commands.rb', line 117

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', 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 = File.basename(File.dirname(repo))
    if org == @ghuser
      put '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
      SugarJar::Log.info("Fork (#{@ghuser}/#{reponame}) detected.")
      hub('remote', 'rename', 'origin', 'upstream')
      hub('remote', 'add', 'origin', repo.gsub("#{org}/", "#{@ghuser}/"))
    else
      SugarJar::Log.info("Forked #{reponame} to #{@ghuser}")
    end
    SugarJar::Log.info('Remotes "origin" and "upstream" configured.')
  end
end

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



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sugarjar/commands.rb', line 161

def smartpush(remote = nil, branch = nil)
  unless remote && branch
    remote ||= 'origin'
    branch ||= current_branch
  end

  if run_prepush
    puts hub('push', remote, branch).stderr
  else
    SugarJar::Log.error('Pre-push checks failed. Not pushing.')
  end
end

#unitObject



157
158
159
# File 'lib/sugarjar/commands.rb', line 157

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

#upObject



75
76
77
78
79
80
81
82
83
# File 'lib/sugarjar/commands.rb', line 75

def up
  assert_in_repo
  result = gitup
  if result
    SugarJar::Log.info("Rebased branch on #{result}")
  else
    die('Failed to rebase current branch')
  end
end

#upallObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sugarjar/commands.rb', line 98

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

    hub('checkout', branch)
    result = gitup
    if result
      SugarJar::Log.info("Rebased #{branch} on #{result}")
    else
      SugarJar::Log.error(
        "Failed to rebase #{branch}, aborting that and moving to next " +
        'branch',
      )
      hub('rebase', '--abort')
    end
  end
end

#versionObject



190
191
192
193
# File 'lib/sugarjar/commands.rb', line 190

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