Class: Zold::Merge

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

Overview

MERGE pulling command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, copies:, log: Log::Quiet.new) ⇒ Merge

Returns a new instance of Merge.



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

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

Instance Method Details

#merge(wallet, cps, _) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zold/commands/merge.rb', line 63

def merge(wallet, cps, _)
  raise 'There are no remote copies, try FETCH first' if cps.all.empty?
  cps = cps.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
  modified = patch.save(wallet.path, overwrite: true)
  if modified
    @log.debug("Merged successfully into #{wallet.path}")
  else
    @log.debug("Nothing changed in #{wallet.path} after merge")
  end
  modified
end

#run(args = []) ⇒ Object

Returns the array of modified wallets (IDs)



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

def run(args = [])
  opts = Slop.parse(args, help: true) do |o|
    o.banner = "Usage: zold merge [ID...] [options]
Available options:"
    o.bool '--help', 'Print instructions'
  end
  if opts.help?
    @log.info(opts.to_s)
    return
  end
  raise 'At least one wallet ID is required' if opts.arguments.empty?
  modified = []
  opts.arguments.each do |id|
    wallet = @wallets.find(Id.new(id))
    next unless merge(wallet, Copies.new(File.join(@copies, id)), opts)
    modified << Id.new(id)
    require_relative 'propagate'
    modified += Propagate.new(wallets: @wallets, log: @log).run(args)
  end
  modified
end