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_rack_env, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #puts, #stdin, #system!, #theme

Instance Method Details

#descriptionObject



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

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

#optsObject



246
247
248
249
250
# File 'lib/brut/cli/apps/deploy.rb', line 246

def opts = [
  [ "--[no-]check-branch", "If true, requires that you are on 'main' (default true)" ],
  [ "--[no-]check-changes", "If true, requires that you have committed all local changes (default true)" ],
  [ "--[no-]check-push", "If true, requires that you are in sync with origin/main (default true)" ],
]

#runObject



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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
# File 'lib/brut/cli/apps/deploy.rb', line 252

def run
    puts theme.header.render("Checking Git repo to see if changes have all been pushed to main")
    puts

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

    checks = []

    branch = ""
    system!("git branch --show-current") do |output|
      branch << output
    end
    branch = branch.strip.chomp
    checks << [
      "Deploy from main",
    ]
    if branch != "main"
      checks.last << "Currently on #{theme.code.render(branch)}"
      checks.last << options.check_branch?
    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
    checks << [
      "No un-committed changes",
    ]
    if local_changes.strip != ""
      items = local_changes.split(/\n/)
      list = Lipgloss::List.new.items(items).item_style(theme.error)
      checks.last << "Files not committed:\n#{list.render.strip}\n"
      checks.last << options.check_changes?
    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)
    checks << [
      "Pulled from origin",
    ]
    if remote_ahead != 0
      if remote_ahead == 1
        checks.last << "There is 1 commit in origin you don't have"
      else
        checks.last << "There are #{remote_ahead} commits in origin you don't have"
      end
      checks.last << options.check_push?
    end
    checks << [
      "Pushed to origin",
    ]

    if local_ahead != 0
      if local_ahead == 1
        checks.last << "There is 1 commit not pushed to origin"
      else
        checks.last << "There are #{local_ahead} commits not pushed to origin"
      end
      checks.last << options.check_push?
    end

    rows = []
    checks.each do |(check,status,error)|
      row = [ check ]
      if status
        if error
          row << theme.error.render("FAILED")
        else
          row << theme.warning.render("Ignored")
        end
        row << theme.error.render(status)
      else
        row << theme.success.render("OK")
        row << ""
      end
      rows << row
    end
    table = Lipgloss::Table.new.
      headers(["Check", "Result", "Details"]).
      rows(rows).
      style_func(rows: rows.length, columns: 3) { |row,column|
      if row == Lipgloss::Table::HEADER_ROW
        Lipgloss::Style.new.inherit(theme.header).padding_left(1).padding_right(1)
      elsif column == 0
        Lipgloss::Style.new.inherit(theme.subheader).padding_left(1).padding_right(1).padding_bottom(1)
      else
        Lipgloss::Style.new.inherit(theme.none).padding_left(1).padding_right(1).padding_bottom(1)
      end
    }
  puts table.render
  checks_failed = checks.count { |(_,status,_)| status }
  checks_failed_not_ignored = checks.count { |(_,status,error)| status && error }
  if checks_failed > 0
    if checks_failed_not_ignored > 0
      puts theme.error.render("#{checks_failed} checks failed - aborting")
      return 1
    else
      puts theme.warning.render("#{checks_failed} checks failed but ignored")
    end
  else
    puts theme.success.render("All checks passed")
  end

  0
end