Sha256: fd0a9684189f7e1818c20baafc42751f770c8ccee85a3dc7b0d19648ea2a338d

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'pathname'

module Spaarti
  ##
  # Repo object, handles individual repo syncing and state
  class Repo
    def initialize(data, client, params = {})
      @data = data
      @client = client
      @options = params
      @path = @options[:format] % @data
    end

    def sync!
      clone
      Dir.chdir(@path) do
        config
        add_upstream
        update_submodules
      end
    end

    def parent_of(repo)
      repo.relative_path_from(Pathname.new(@path)).each_filename.first != '..'
    end

    private

    def log(msg)
      puts msg unless @options[:quiet]
    end

    def err(msg)
      STDERR.puts msg
    end

    def run(cmd, error_msg)
      res = system "#{cmd} &>/dev/null"
      err(error_msg) unless res
    end

    def clone
      return log("#{@data[:full_name]} already cloned") if Dir.exist? @path
      log "Cloning #{@data[:ssh_url]} to #{@path}"
      run(
        "git clone '#{@data[:ssh_url]}' '#{@path}' &>/dev/null",
        "Failed to clone #{@data[:ssh_url]}"
      )
    end

    def config
      @options[:git_config].each do |k, v|
        run("git config '#{k}' '#{v}'", "Failed to set config for #{@path}")
      end
    end

    def add_upstream
      return unless @data[:fork]
      return if `git remote`.split.include? 'upstream'
      upstream = @client.repo(@data[:id]).source.git_url
      run(
        "git remote add upstream '#{upstream}'",
        "Failed to add upstrema for #{@path}"
      )
    end

    def update_submodules
      run(
        'git submodule update --init',
        "Failed to update submodules in #{@path}"
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spaarti-0.0.6 lib/spaarti/repo.rb
spaarti-0.0.5 lib/spaarti/repo.rb