lib/eternity/patch.rb in eternity-0.0.2 vs lib/eternity/patch.rb in eternity-0.0.3

- old
+ new

@@ -24,26 +24,10 @@ def delta @delta ||= TransparentProxy.new { calculate_delta } end - private - - def current_delta - @current_delta ||= base_delta_of current_commit, base_commit - end - - def target_delta - @target_delta ||= base_delta_of target_commit, base_commit - end - - def base_delta_of(commit, base) - return {} if commit == base - history = [commit] + commit.base_history_at(base)[0..-2] - Delta.merge history.reverse.map(&:base_delta) - end - end class Merge @@ -60,10 +44,18 @@ current_commit.fast_forward?(target_commit) end private + def current_delta + @current_delta ||= Delta.between target_commit, current_commit + end + + def target_delta + @target_delta ||= Delta.between current_commit, target_commit + end + def calculate_delta return {} if merged? base_commit.with_index do |base_index| target_delta.each_with_object({}) do |(collection, elements), hash| @@ -104,26 +96,51 @@ def current_action_for(collection, id) current_delta[collection][id]['action'] if has_current_changes_for? collection, id end log :calculate_delta - end class Diff extend Log include Common private + def current_delta + @current_delta ||= Delta.between base_commit, current_commit + end + + def target_delta + @target_delta ||= Delta.between base_commit, target_commit + end + + def diff_delta + @diff_delta ||= Delta.merge [Delta.revert(current_delta, base_commit), target_delta] + end + def calculate_delta - Delta.merge [Delta.revert(current_delta, base_commit), target_delta] + target_commit.with_index do |target_index| + diff_delta.each_with_object({}) do |(collection, elements), hash| + hash[collection] = {} + + elements.each do |id, change| + if change['data'] + sha1 = Blob.digest(Blob.serialize(change['data'])) + change['data'] = target_index[collection][id].data if target_index[collection].include?(id) && sha1 != target_index[collection][id].sha1 + end + + hash[collection][id] = change if change + end + + hash.delete collection if hash[collection].empty? + end + end end log :calculate_delta - end end end