Sha256: 61ab90d1421ca6bdd62ef82c98da9b71f7135137d4d1b48b65eb564e9a73f679

Contents?: true

Size: 1.58 KB

Versions: 12

Compression:

Stored size: 1.58 KB

Contents

require 'grit'

module Dandelion
  module Git
    class DiffError < StandardError; end
    class RevisionError < StandardError; end
  
    class Repo < Grit::Repo
      def initialize(dir)
        super(dir)
      end
    end
  
    class Diff
      attr_reader :from_revision, :to_revision
    
      @files = nil
  
      def initialize(repo, from_revision, to_revision)
        @repo = repo
        @from_revision = from_revision
        @to_revision = to_revision
        begin
          @files = parse(diff)
        rescue Grit::Git::CommandFailed
          raise DiffError
        end
      end

      def changed
        @files.to_a.select { |f| ['A', 'C', 'M'].include?(f.last) }.map { |f| f.first }
      end

      def deleted
        @files.to_a.select { |f| 'D' == f.last }.map { |f| f.first }
      end

      private
      
      def diff
        @repo.git.native(:diff, {:name_status => true, :raise => true}, from_revision, to_revision)
      end
    
      def parse(diff)
        files = {}
        diff.split("\n").each do |line|
          status, file = line.split("\t")
          files[file] = status
        end
        files
      end
    end

    class Tree
      def initialize(repo, revision)
        @repo = repo
        @commit = @repo.commit(revision)
        raise RevisionError if @commit.nil?
        @tree = @commit.tree
      end
    
      def files
        @repo.git.native(:ls_tree, {:name_only => true, :r => true}, revision).split("\n")
      end

      def show(file)
        (@tree / file).data
      end
  
      def revision
        @commit.sha
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dandelion-0.3.11 lib/dandelion/git.rb
dandelion-0.3.10 lib/dandelion/git.rb
dandelion-0.3.9 lib/dandelion/git.rb
dandelion-0.3.8 lib/dandelion/git.rb
dandelion-0.3.7 lib/dandelion/git.rb
dandelion-0.3.6 lib/dandelion/git.rb
dandelion-0.3.5 lib/dandelion/git.rb
dandelion-0.3.4 lib/dandelion/git.rb
dandelion-0.3.3 lib/dandelion/git.rb
dandelion-0.3.2 lib/dandelion/git.rb
dandelion-0.3.1 lib/dandelion/git.rb
dandelion-0.3.0 lib/dandelion/git.rb