Module: Copernicium::RevLog

Included in:
Repos
Defined in:
lib/RevLog.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_file(file_name, content) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/RevLog.rb', line 67

def RevLog.add_file(file_name, content)
  hash = hash_file(file_name, content)
  File.open(File.join(@@rev_path, hash), 'w') { |f| f.write(content) }
  @@logmap[file_name] = @@logmap[file_name] << {:time => Time.now,
                                                :hash => hash}
  @@hashmap[hash] = @@hashmap[hash] << {:time => Time.now,
                                        :filename => file_name}
  updatelog
  return hash
end

.delete_file(file_id) ⇒ Object

return 1 if succeed, otherwise 0



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/RevLog.rb', line 79

def RevLog.delete_file(file_id)
  begin
    file_name = @@hashmap[file_id][0][:filename]
    @@hashmap[file_id].delete_if { |e| e[:filename] == file_name }
    @@logmap[file_name].delete_if { |e| e[:hash] == file_id }
    updatelog
    File.delete(File.join(@@rev_path, file_id))
    return 1
  rescue Exception
    return 0
  end
end

.diff_files(file_id1, file_id2) ⇒ Object



103
104
105
# File 'lib/RevLog.rb', line 103

def RevLog.diff_files(file_id1, file_id2)
  Diffy::Diff.new(get_file(file_id1), get_file(file_id2)).to_s()
end

.get_file(id) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/RevLog.rb', line 93

def RevLog.get_file(id)
  file_path = File.join(@@rev_path, id.to_s)
  if File.exist? file_path
    File.open(file_path, 'r') { |f| return f.read }
  else
    raise Exception, 'RevLog: invalid file revision id!'.red
  end
end

.hash_file(file_name, content) ⇒ Object



107
108
109
# File 'lib/RevLog.rb', line 107

def RevLog.hash_file(file_name, content)
  Digest::SHA256.hexdigest(file_name + content.to_s)
end

.history(file_name) ⇒ Object



118
119
120
121
122
# File 'lib/RevLog.rb', line 118

def RevLog.history(file_name)
  hashs = []
  @@logmap[file_name].each { |m| hashs << m[:hash] }
  hashs
end

.merge(id1, id2) ⇒ Object



111
112
113
114
115
116
# File 'lib/RevLog.rb', line 111

def RevLog.merge(id1, id2)
  diff_a = Diffy::Diff.new(get_file(id1), get_file(id2)).each_chunk.to_a
  return get_file(id2) if diff_a.all? { |d| d[0]!='-'}
  # return get_file(id1) if diff_a.all? { |d| d[0]!='+'}
  diff_a
end

.setup(root = Dir.pwd) ⇒ Object

called when including RevLog dont make any new folders



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/RevLog.rb', line 31

def RevLog.setup(root = Dir.pwd)
  @@cop_path = File.join(root, '.cn')
  @@rev_path = File.join(@@cop_path, 'revs')
  @@log_path = File.join(@@cop_path, 'logmap')
  @@hash_path = File.join(@@cop_path, 'hashmap')
  if File.exist?(@@log_path) && File.exist?(@@hash_path)
    @@logmap = hash_array.merge(YAML.load_file(@@log_path))
    @@hashmap = hash_array.merge(YAML.load_file(@@hash_path))
  else
    @@logmap = hash_array
    @@hashmap = hash_array
  end
end

.setup_tester(root = Dir.pwd) ⇒ Object

called when running the unit tests create a new folder for testing



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/RevLog.rb', line 47

def RevLog.setup_tester(root = Dir.pwd)
  @@cop_path = File.join(root, '.cn')
  @@rev_path = File.join(@@cop_path, 'revs')
  @@log_path = File.join(@@cop_path, 'logmap')
  @@hash_path = File.join(@@cop_path, 'hashmap')
  Dir.mkdir(@@cop_path) unless Dir.exist?(@@cop_path)
  Dir.mkdir(@@rev_path) unless Dir.exist?(@@rev_path)
  if File.exist?(@@log_path) && File.exist?(@@hash_path)
    @@logmap = hash_array.merge(YAML.load_file(@@log_path))
    @@hashmap = hash_array.merge(YAML.load_file(@@hash_path))
  else
    @@logmap = hash_array
    @@hashmap = hash_array
  end
end

.updatelogObject



124
125
126
127
# File 'lib/RevLog.rb', line 124

def RevLog.updatelog
  File.open(@@log_path, 'w') { |f| f.write(@@logmap.to_yaml) }
  File.open(@@hash_path, 'w') { |f| f.write(@@hashmap.to_yaml) }
end

Instance Method Details

#hash_arrayObject



63
64
65
# File 'lib/RevLog.rb', line 63

def hash_array
  Hash.new {[]}
end