module Copernicium::RevLog
Public Class Methods
add_file(file_name, content)
click to toggle source
# 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)
click to toggle source
return 1 if succeed, otherwise 0
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
called when including RevLog dont make any new folders
# 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)
click to toggle source
called when running the unit tests create a new folder for testing
# 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
updatelog()
click to toggle source
# 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
Public Instance Methods
hash_array()
click to toggle source
# File lib/RevLog.rb, line 63 def hash_array Hash.new {[]} end