Module: Utils

Defined in:
lib/utils.rb

Instance Method Summary collapse

Instance Method Details

#ask(prompt) ⇒ Object



20
21
22
23
# File 'lib/utils.rb', line 20

def ask(prompt)
  print prompt
  gets.chomp
end

#confirm?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/utils.rb', line 5

def confirm?(prompt)
  loop do
    print "#{prompt} (y/n): "
    input = gets.chomp.downcase
    case input
    when 'y'
      return true
    when 'n'
      return false
    else
      puts 'wrong input only enter y/n'
    end
  end
end

#get_file_from_hash(hash, dir_path) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/utils.rb', line 37

def get_file_from_hash(hash, dir_path)
  Dir.glob("#{dir_path}/**/*").each do |file_path|
    next unless File.file?(file_path)

    file_hash = get_file_hash(file_path)
    return file_path if file_hash == hash
  end
  nil
end

#get_file_hash(path) ⇒ Object



33
34
35
# File 'lib/utils.rb', line 33

def get_file_hash(path)
  Digest::SHA256.file(path).hexdigest
end

#get_string_hash(str) ⇒ Object



25
26
27
# File 'lib/utils.rb', line 25

def get_string_hash(str)
  Digest::SHA256.hexdigest(str)
end

#read_json(path) ⇒ Object



29
30
31
# File 'lib/utils.rb', line 29

def read_json(path)
  JSON.parse(File.read(path))
end

#revert_to_commit(commit_hash) ⇒ Object



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

def revert_to_commit(commit_hash)
  commit_content = read_json(".stolen-git/commits/#{commit_hash}.json")
  commit_tree = read_json(".stolen-git/storage/trees/#{commit_content['tree_hash']}.json")
  index = {}
  commit_tree['entries'].each do |entry|
    blob = File.read(".stolen-git/storage/blobs/#{entry['hash']}")
    # TODO: Figure out what to do when path changes
    File.write(entry['path'], blob)
    index[entry['path']] ||= {}
    index[entry['path']]['hash'] = entry['hash']
  end

  File.write('.stolen-git/index.json', JSON.pretty_generate(index))
end