Sha256: 8e35f67a15e3960f0d43de0b6d24026b42346c9482462f7c427c91eae0101ea5

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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.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

2 entries across 2 versions & 1 rubygems

Version Path
dandelion-0.6.0 lib/dandelion/changeset.rb
dandelion-0.5.4 lib/dandelion/changeset.rb