Sha256: f3fdab7a7f8ed7d5e35f44198344e815e02cf93ad2e857b8cf29c9e628ae85ba

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require_relative "branch"
require_relative "command"
require_relative "reporter"

module GitFonky
  class RepoDir
    attr_reader :branch, :command, :dirname, :reporter

    def initialize(dirname, command = Command, branch = Branch, reporter = Reporter)
      @dirname = dirname
      @command = command.new
      @branch = branch.new(@command.current_branch)
      @command.branch_name = @branch.name
      @reporter = reporter.new(@dirname, @branch.name)
    end

    def self.sync(dirname)
      Dir.chdir(dirname) do
        new(dirname).sync
      end
    end

    def sync
      catch(:skip_repo) do
        announce_sync_attempt
        fetch
        pull
        push
        announce_sync_success
      end
    end

    private

    def announce_sync_attempt
      branch.valid? ? reporter.announce_sync_attempt : throw(:skip_repo, reporter.invalid_branch_msg)
    end

    def fetch
      command.fetch_upstream
      process_successful? ? reporter.announce("fetched") : throw(:skip_repo, reporter.failed_fetch_msg)
    end

    def pull
      command.pull_upstream
      process_successful? ? reporter.announce("pulled") : throw(:skip_repo, reporter.failed_pull_msg)
    end

    def push
      command.push_to_origin
      process_successful? ? reporter.announce("pushed", "to", "origin") : throw(:skip_repo, reporter.failed_push_msg)
    end

    def announce_sync_success
      reporter.announce_sync_success
    end

    def process_successful?
      $?.success?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_fonky-0.6.0 lib/git_fonky/repo_dir.rb