Class: Brut::CLI::Apps::Deploy::Check::Git

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/deploy.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#descriptionObject



203
# File 'lib/brut/cli/apps/deploy.rb', line 203

def description = "Perform the check assuming Git is the version-control system"

#optsObject



204
# File 'lib/brut/cli/apps/deploy.rb', line 204

def opts = self.parent_command.opts

#runObject



206
207
208
209
210
211
212
213
214
215
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
286
287
# File 'lib/brut/cli/apps/deploy.rb', line 206

def run

  checks_ignored = 0

  options.set_default(:check_branch,  true)
  options.set_default(:check_changes, true)
  options.set_default(:check_push,    true)

  branch = ""
  system!("git branch --show-current") do |output|
    branch << output
  end
  branch = branch.strip.chomp
  if branch != "main"
    stderr.puts "You are not on the 'main' branch, but on '#{branch}'"
    if options.check_branch?
      stderr.puts "You may only deploy from main"
      return 1
    else
      checks_ignored += 1
      stderr.puts "Ignoring..."
    end
  end

  system!("git status") do |*| # reset local caches to account for Docker/host wierdness
    # ignore
  end
  local_changes = ""
  system!("git diff-index --name-only HEAD --") do |output|
    local_changes << output
  end
  if local_changes.strip != ""
    stderr.puts "You have un-committed changes:"
    stderr.puts
    local_changes.split(/\n/).each do |change|
      checks_ignored += 1
      stderr.puts "  #{change}"
    end
    stderr.puts
    if options.check_changes?
      stderr.puts "Commit or revert these, then push to origin"
      return 1
    else
      checks_ignored += 1
      stderr.puts "Ignoring..."
    end
  end

  rev_list = ""
  system!("git rev-list --left-right --count origin/main...main") do |output|
    rev_list << output
  end
  remote_ahead, local_ahead = rev_list.strip.chomp.split(/\t/,2).map(&:to_i)
  if remote_ahead != 0
    stderr.puts "There are commits in origin you don't have."
    if options.check_push?
      stderr.puts "Pull those in, re-run bin/ci, THEN deploy"
      return 1
    else
      checks_ignored += 1
      stderr.puts "Ignoring..."
    end
  end

  if local_ahead != 0
    stderr.puts "You have not pushed to origin."
    if options.check_push?
      stderr.puts "Push to origin before deploying"
      return 1
    else
      checks_ignored += 1
      stderr.puts "Ignoring..."
    end
  end
  if checks_ignored == 0
    stdout.puts "All checks passed"
  else
    stdout.puts "#{checks_ignored} checks failed, but ignored"
  end

  0
end