Class: Orefs::Diff

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Diff) initialize(local, remote, sub_diffs = {})

A new instance of Diff



47
48
49
50
51
# File 'lib/orefs/diff.rb', line 47

def initialize(local, remote, sub_diffs={})
  @local = local
  @remote = remote
  @sub_diffs = sub_diffs
end

Instance Attribute Details

- (Object) local

local Info Object



5
6
7
# File 'lib/orefs/diff.rb', line 5

def local
  @local
end

- (Object) remote

remote Info Object



7
8
9
# File 'lib/orefs/diff.rb', line 7

def remote
  @remote
end

- (Object) sub_diffs

Returns the value of attribute sub_diffs



9
10
11
# File 'lib/orefs/diff.rb', line 9

def sub_diffs
  @sub_diffs
end

Class Method Details

+ (Object) digest_diff(ld, rd)



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/orefs/diff.rb', line 34

def self.digest_diff(ld, rd)
  diffs = {}
  lh = ld.entries_hash()
  rh = rd.entries_hash()
  keys = lh.keys | rh.keys
  keys.each do |k|
    le = lh[k]
    re = rh[k]
    diffs[k] = self.new(le, re) unless le == re
  end
  diffs
end

+ (Object) dir_diff(l, r, recursive = false)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/orefs/diff.rb', line 11

def self.dir_diff(l, r, recursive=false)
  ld = Digest.create(l, nil)
  rd = Digest.create(r, nil)
  diffs = digest_diff(ld, rd)
  if recursive
    lh = ld.entries_hash()
    rh = rd.entries_hash()
    keys = lh.keys | rh.keys
    keys.each do |k|
      next unless lh[k].is_a?(::Orefs::Dir) || rh[k].is_a?(::Orefs::Dir)
      unless (sd = dir_diff(::File.join(l, k), ::File.join(r, k))).nil? || sd.empty?
        if (d = diffs[k]).nil?
          d = self.new(lh[k], rh[k], sd)
        else
          d.sub_diffs = sd
        end
        diffs[k] = d
      end
    end
  end
  diffs
end

Instance Method Details

- (Object) fill_hash(prefix, h, sym = false)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/orefs/diff.rb', line 53

def fill_hash(prefix, h, sym=false)
  [:class, :name, :owner, :group, :mode, :mtime, :size, :target].each do |m|
    next unless @local.respond_to?(m) and @remote.respond_to?(m)
    lv = @local.send(m)
    rv = @remote.send(m)
    if sym
      k = m
    else
      k = @local.name + '.' + m.to_s
      k = prefix + '.' + k unless prefix.nil?
    end
    h[k] = [lv, rv] unless lv == rv unless (:mtime == m && !@remote.is_a?(::Orefs::File))
  end
  pre = prefix.nil? ? @local.name : (prefix + '.' + @local.name)
  @sub_diffs.each do |name,d|
    d.fill_hash(pre, h)
  end
end

- (Object) to_s



72
73
74
75
76
77
78
# File 'lib/orefs/diff.rb', line 72

def to_s()
  s = ''
  h = {}
  fill_hash(nil, h)
  h.each { |k,v| s << "#{k}: #{v[0]} vs #{v[1]}\n" }
  s
end