Sha256: 5d4260de77538ce492a1e9cc195444abd67d47ba26852919616298d6ec4c7a95

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

require 'digest/md5'
require 'git'

module Reflection
  class Repository
    
    attr_accessor :url
    attr_accessor :path
    
    def self.new_from_path(path)
      unless self.exists?(path)
        raise "'#{path}' is not a valid Git repository"
      end
      
      repo = Git.open(path)
      self.new(repo.remote.url, path)
    end
    
    def self.exists?(path)
      begin
        Git.open(path)
      rescue ArgumentError
        return false
      end      
    end
    
    def initialize(new_url, path = nil)
      self.url = new_url
      self.path = path
    end
    
    def identifier
      Digest::MD5.hexdigest(self.url)
    end
    
    def same_in_path?(path)
      git_repo = Git.open(path)
      (git_repo.remote && git_repo.remote.url == self.url) || false
    end
    
    def reset!
      Reflection.log.debug "Resetting target to HEAD"
      repo = Git.open(self.path)
      repo.reset_hard
    end
    
    def clone(path)
      Git.clone(self.url, path)
    end
    
    def commit_all_new_files
      repo = Git.open(self.path)
      Reflection.log.debug "Committing all changes.."
      Reflection.log.debug(repo.add)
      Reflection.log.debug(repo.commit_all("Updated stash.")) rescue true
    end
    
    def push
      repo = Git.open(self.path)
      Reflection.log.debug "Pushing commit.."
      Reflection.log.debug(repo.push)
    end
    
    def pull
      repo = Git.open(self.path)
      Reflection.log.debug "Pulling.."
      Reflection.log.debug(repo.pull)
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reflection-0.4.4 lib/reflection/repository.rb
reflection-0.4.3 lib/reflection/repository.rb
reflection-0.4.1 lib/reflection/repository.rb
reflection-0.4.0 lib/reflection/repository.rb
reflection-0.3.1 lib/reflection/repository.rb
reflection-0.0.2 lib/reflection/repository.rb