Class: Zold::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/diff.rb

Overview

DIFF pulling command

Instance Method Summary collapse

Constructor Details

#initialize(wallet:, copies:, log: Log::Quiet.new) ⇒ Diff

Returns a new instance of Diff.



34
35
36
37
38
# File 'lib/zold/commands/diff.rb', line 34

def initialize(wallet:, copies:, log: Log::Quiet.new)
  @wallet = wallet
  @copies = copies
  @log = log
end

Instance Method Details

#run(_ = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zold/commands/diff.rb', line 40

def run(_ = [])
  raise 'There are no remote copies, try FETCH first' if @copies.all.empty?
  cps = @copies.all.sort_by { |c| c[:score] }.reverse
  patch = Patch.new
  patch.start(Wallet.new(cps[0][:path]))
  cps[1..-1].each do |c|
    patch.join(Wallet.new(c[:path]))
  end
  before = File.read(@wallet.path)
  after = ''
  Tempfile.open do |f|
    patch.save(f, overwrite: true)
    after = File.read(f)
  end
  diff = Diffy::Diff.new(before, after, context: 0).to_s(:color)
  @log.info(diff)
  diff
end