module Avmtrf1::Git::PushLarge::LfsCommit::Push

def after_push_banner

def after_push_banner
  infov message('Commit SHA1'), commit.sha1
  raise 'Cache of files size not found' if files_size_cache.read.blank?
  infov message('Files size'), PushLarge.bytes_size(files_size_cache.read)
end

def check_push_result(result)

def check_push_result(result)
  if result.fetch(:exit_code).zero?
    on_push_result_ok
  elsif result.fetch(:stderr).include?(PACK_ERROR_MESSAGE)
    on_push_result_pack_error
  else
    on_push_result_arbitrary_error(result)
  end
end

def fresh_push

def fresh_push
  fresh_push_banner
  check_push_result(
    git.execute(
      'push', push_large.remote_name, '--force',
      "#{commit.sha1}:#{push_reference}"
    )
  )
end

def fresh_push_banner

def fresh_push_banner
  infom message('Pushing...')
  fresh_push_commit_banner
  fresh_push_remote_banner
end

def fresh_push_commit_banner

def fresh_push_commit_banner
  infov message('SHA1', 1), commit.sha1
  infov message('Files size', 1), PushLarge.bytes_size(commit.files_size)
end

def fresh_push_remote_banner

def fresh_push_remote_banner
  infov message('Remote', 1), push_large.remote_name
  infov message('Reference', 1), push_reference
end

def on_push_result_arbitrary_error(result)

def on_push_result_arbitrary_error(result)
  raise result.pretty_inspect.to_s
end

def on_push_result_ok

def on_push_result_ok
  push_pack_error_cache.write(PACK_ERROR_FALSE)
  success(push_message('successful'))
end

def on_push_result_pack_error

def on_push_result_pack_error
  push_pack_error_cache.write(PACK_ERROR_TRUE)
  warn(push_message("failed with message \"#{PACK_ERROR_MESSAGE}\""))
end

def push_message(suffix)

def push_message(suffix)
  message("Push of #{commit.sha1} (Size: #{commit.files_size}, " \
          "Files: #{commit.files.count}): #{suffix}.")
end

def push_ok_uncached

def push_ok_uncached
  commit
  fresh_push if push_pack_error_cache.read.blank?
  after_push_banner
  push_pack_error_cache.read != PACK_ERROR_TRUE
end

def push_reference

def push_reference
  "refs/heads/#{push_large.target_ref}"
end