module Eternity::Patch::Common

Attributes

current_commit[R]
target_commit[R]

Public Class Methods

new(current_commit, target_commit) click to toggle source
# File lib/eternity/patch.rb, line 16
def initialize(current_commit, target_commit)
  @current_commit = current_commit
  @target_commit = target_commit
end

Public Instance Methods

base_commit() click to toggle source
# File lib/eternity/patch.rb, line 21
def base_commit
  @base_commit ||= Commit.base_of current_commit, target_commit
end
base_history() click to toggle source
# File lib/eternity/patch.rb, line 29
def base_history
  @base_history ||= [base_commit] + base_commit.history
end
current_history() click to toggle source
# File lib/eternity/patch.rb, line 33
def current_history
  @current_history ||= [current_commit] + current_commit.history - base_history
end
delta() click to toggle source
# File lib/eternity/patch.rb, line 25
def delta
  @delta ||= TransparentProxy.new { calculate_delta }
end
remaining_history() click to toggle source
# File lib/eternity/patch.rb, line 41
def remaining_history
  @remaining_history ||= current_history - target_history
end
target_history() click to toggle source
# File lib/eternity/patch.rb, line 37
def target_history
  @target_history ||= [target_commit] + target_commit.history - base_history
end

Private Instance Methods

calculate_delta() click to toggle source
# File lib/eternity/patch.rb, line 47
def calculate_delta
  if current_commit.nil?
    target_commit.with_index do |target_index| 
      target_index.each_with_object({}) do |(collection, collection_index), hash|
        hash[collection] = collection_index.ids.each_with_object({}) do |id, h|
          h[id] = {
            'action' => INSERT,
            'data' => collection_index[id].data
          }
        end
      end
    end
  else
    base_commit.with_index do |base_index|
      current_commit.with_index do |current_index|
        current_delta = Delta.merge current_history.reverse.map(&:delta)
        target_delta = Delta.merge target_history.reverse.map(&:delta)
        revert_delta = Delta.revert current_delta, base_index

        merged_delta = merge_deltas target_delta, revert_delta, base_index

        merged_delta.each_with_object({}) do |(collection, elements), hash|
          hash[collection] = {}

          elements.each do |id, change|
            if change['action'] == UPDATE && current_index[collection][id].sha1 == Blob.digest(Blob.serialize(change['data']))
              change = nil 
            elsif change['action'] == DELETE && !current_index[collection].include?(id)
              change = nil
            end
            hash[collection][id] = change if change
          end

          hash.delete collection if hash[collection].empty?
        end
      end
    end
  end
end