Module: Utils

Defined in:
lib/stg/utils.rb

Instance Method Summary collapse

Instance Method Details

#ask(prompt) ⇒ Object



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

def ask(prompt)
  print prompt
  gets.chomp
end

#check_program_existsObject



99
100
101
102
103
104
# File 'lib/stg/utils.rb', line 99

def check_program_exists
  return true if File.exist? '.stolen-git'

  puts "There is no instance of stolen-git found. Please run 'stg init' first."
  false
end

#clean_path(path) ⇒ Object



82
83
84
# File 'lib/stg/utils.rb', line 82

def clean_path(path)
  Pathname.new(path).cleanpath.to_s
end

#confirm?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


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

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



40
41
42
43
44
45
46
47
48
# File 'lib/stg/utils.rb', line 40

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



36
37
38
# File 'lib/stg/utils.rb', line 36

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

#get_string_hash(str) ⇒ Object



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

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

#ignored_path?(path, ignore_patterns) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/stg/utils.rb', line 86

def ignored_path?(path, ignore_patterns)
  path = clean_path(path)
  return false if path == '.'

  path_as_dir = path.end_with?('/') ? path : "#{path}/"

  ignore_patterns&.any? do |pattern|
    File.fnmatch(pattern, path) ||
      File.fnmatch(pattern, path_as_dir) ||
      (pattern.end_with?('/') && File.fnmatch("#{pattern}**", path))
  end
end

#read_json(path) ⇒ Object



30
31
32
33
34
# File 'lib/stg/utils.rb', line 30

def read_json(path)
  JSON.parse(File.read(path))
rescue StandardError
  nil
end

#revert_to_commit(commit_hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stg/utils.rb', line 50

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")
  current_index = read_json('.stolen-git/index.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
    dir = File.dirname(entry['path'])
    FileUtils.mkdir_p(dir) unless dir == '.'
    File.write(entry['path'], blob)
    index[entry['path']] ||= {}
    index[entry['path']]['hash'] = entry['hash']
  end

  (current_index.keys - index.keys).each do |path|
    FileUtils.rm_f(path)
  end

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

#revert_to_indexObject



73
74
75
76
77
78
79
80
# File 'lib/stg/utils.rb', line 73

def revert_to_index
  index = read_json('.stolen-git/index.json') || {}

  index.each do |path, entry|
    blob = File.read(".stolen-git/storage/blobs/#{entry['hash']}")
    File.write(path, blob)
  end
end