require 'eac_launcher/publish/base' module EacLauncher module Git class PublishBase < ::EacLauncher::Publish::Base include ::Eac::SimpleCache include ::EacRubyUtils::Console::Speaker CHECKERS = %w[publish_remote_no_exist remote_equal remote_following local_following].freeze def initialize(instance) @instance = instance end protected def internal_check CHECKERS.each { |checker| return send("#{checker}_check_result") if send("#{checker}?") } divergent_result_check_result end private def publish_remote_no_exist? !sgit.remote_exist?(::EacLauncher::Git::WarpBase::TARGET_REMOTE) end def publish_remote_no_exist_check_result ::EacLauncher::Publish::CheckResult.blocked('Remote does not exist') end def remote_equal? remote_sha.present? && remote_sha == local_sha end def remote_equal_check_result ::EacLauncher::Publish::CheckResult.updated('Remote equal') end def remote_following? remote_sha.present? && sgit.descendant?(remote_sha, local_sha) end def remote_following_check_result ::EacLauncher::Publish::CheckResult.outdated('Remote following') end def divergent_result_check_result ::EacLauncher::Publish::CheckResult.blocked( "Divergent (L: #{local_sha}, R: #{remote_sha})" ) end def local_following? return true if remote_sha.blank? sgit.descendant?(local_sha, remote_sha) end def local_following_check_result ::EacLauncher::Publish::CheckResult.pending('Local following') end def sgit_uncached ::EacLauncher::Git::Base.new(@instance.warped) end def publish info 'Pushing...' sgit.push(::EacLauncher::Git::WarpBase::TARGET_REMOTE, 'HEAD:master', dryrun: !::EacLauncher::Context.current.publish_options[:confirm]) info 'Pushed!' end def local_sha sgit.git.object('HEAD').sha end def remote_sha_uncached sgit.fetch(::EacLauncher::Git::WarpBase::TARGET_REMOTE) b = sgit.git.branches["#{::EacLauncher::Git::WarpBase::TARGET_REMOTE}/master"] b ? b.gcommit.sha : nil end end end end