Sha256: dba7225e1ccd7df6bd9b3c47738ec7be8a93ee89601b562c667a0717ef4a318e

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

module Dandelion
  class Changeset
    include Enumerable

    def initialize(tree, commit, options = {})
      @tree = tree
      @commit = commit
      @options = options
    end

    def diff
      Diff.new(@commit, @tree.commit)
    end

    def empty?
      diff.empty?
    end

    def each
      diff.each do |change|
        if applicable?(change.path)
          path = transform_path(change.path)

          if change.type == :delete
            yield Change.new(path, change.type)
          else
            read = -> { @tree.data(change.path) }
            
            if @tree.is_symlink?(change.path)
              yield Change.new(path, :symlink, read)
            else
              yield Change.new(path, change.type, read)
            end
          end
        end
      end
    end

  private

    def expand_path(path)
      File.expand_path(path).to_s
    end

    def local_path
      expand_path(@options[:local_path] || '')
    end

    def applicable?(path)
      expand_path(path).start_with?(expand_path(local_path))
    end

    def transform_path(path)
      trimmed = expand_path(path)[expand_path(local_path).length..-1]
      trimmed = trimmed[1..-1] if trimmed[0] == File::SEPARATOR
      trimmed
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dandelion-0.5.3 lib/dandelion/changeset.rb
dandelion-0.5.2 lib/dandelion/changeset.rb
dandelion-0.5.1 lib/dandelion/changeset.rb
dandelion-0.5.0 lib/dandelion/changeset.rb