Sha256: 31a1cad463c3ffa75b86102d1753795c4d9fba28e6fcb3f0c75938dd6bd63a72

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'fileutils'

module Chicanery
  module Git
    class Repo
      include FileUtils

      attr_reader :name, :path, :branches, :remotes

      def initialize name, path, params={}
        @name, @path = name, path
        @branches = params[:branches] || []
        @remotes = params[:remotes] || {}
      end

      def in_repo
        Dir.chdir(path) { yield }
      end

      def prepare
        return if File.exists? path
        mkdir_p path
        in_repo { git 'init' }
      end

      def state
        prepare
        response = {}
        in_repo do
          remotes.each do |name, remote|
            git "remote add #{name} #{remote[:url]}" unless git("remote | grep #{name}") == name.to_s
            git "fetch -q #{name}"
            (remote[:branches] || ['master']).each do |branch|
              response["#{name}/#{branch}"] = head "#{name}/#{branch}"
            end
          end
          branches.each do |branch|
            response[branch] = head branch
          end
        end
        response
      end

      def head branch
        match = /^([^ ]*) /.match git "log -n 1 #{branch} --pretty=oneline"
        match[1] if match
      end

      def git command
        `git #{command}`.chomp
      end
    end

    def git_repo *args
      repo Repo.new *args
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chicanery-0.0.5 lib/chicanery/git.rb