Module: Actions

Includes:
DiffCalc
Included in:
Stg::CLI
Defined in:
lib/actions.rb

Instance Attribute Summary

Attributes included from DiffCalc

#cnt, #mem, #new_s, #old_s

Instance Method Summary collapse

Methods included from DiffCalc

#build_sequences, #compute_diff, #differencing, #initialize, #print_diff, #print_line

Instance Method Details

#branchObject



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/actions.rb', line 313

def branch
  name = ARGV.last
  pointer = read_json('.stolen-git/pointer.json')
  pointed_branch = pointer['type'] == 'branch' ? pointer['point_to'] : ''
  if name
    first_commit = pointer['type'] == 'branch' ? read_json(".stolen-git/branches/#{pointed_branch}.json")['commit_pointer'] : pointer['point_to']
    id = SecureRandom.uuid
    File.write(".stolen-git/branches/#{id}.json", JSON.pretty_generate({
                                                                         name: name,
                                                                         created_at: Time.now,
                                                                         commit_pointer: first_commit
                                                                       }))
    return
    puts "branch #{name} created"
  end
  Dir.children('.stolen-git/branches').each do |entry|
    branch_content = read_json(".stolen-git/branches/#{entry}")
    branch_id = File.basename(entry, '.*')
    if pointed_branch == branch_id
      print '* '
      puts "#{branch_content['name']}".green
    else
      puts "#{branch_content['name']}"
    end
  end
end

#checkoutObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/actions.rb', line 271

def checkout
  options = { commit: false }

  # Getting commit name & description
  OptionParser.new do |opts|
    opts.banner = 'Usage: stg checkout [options]'

    opts.on('-c', '--commit', 'Add a commit id instead') do
      options[:commit] = true
    end
  end.parse!
  inp = ARGV.last
  if !inp
    puts 'Please Enter the name of a branch or commit_id'
  elsif options[:commit]
    commit_history = read_json('.stolen-git/commits.json')
    current_commit_hash = commit_history['commits'].find { |x| x['id'] == inp }['hash']
    revert_to_commit(current_commit_hash)
  else
    # TODO: Handle if user enters branch_id instead
    branch_content = {}
    branch_id = ''
    Dir.children('.stolen-git/branches').each do |entry|
      branch_content = read_json(".stolen-git/branches/#{entry}")
      if branch_content['name'] == inp
        branch_id = File.basename(entry, '.*')
        break
      end
    end
    if branch_content.empty?
      puts 'There is no branch with that name.'
      nil
    end
    commit_hash = branch_content['commit_pointer']
    revert_to_commit(commit_hash)
    pointer = read_json('.stolen-git/pointer.json')
    pointer['point_to'] = branch_id
    pointer['type'] = 'branch'
    File.write('.stolen-git/pointer.json', JSON.pretty_generate(pointer))
  end
end

#commitObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/actions.rb', line 110

def commit
  options = { name: '', description: '' }

  # Getting commit name & description
  OptionParser.new do |opts|
    opts.banner = 'Usage: stg commit [options]'

    opts.on('-n', '--name NAME', 'Add a commit name') do |name|
      options[:name] = name
    end

    opts.on('-d', '--description DESCRIPTION', 'Add a commit description') do |description|
      options[:description] = description
    end
  end.parse!

  options[:name] = ask('Add a commit name: ') if options[:name].empty?

  # Get the index
  index = JSON.parse(File.read('.stolen-git/index.json'))
  tree_content = {
    entries: []
  }

  commit_history = JSON.parse(File.read('.stolen-git/commits.json'))

  pointer = read_json('.stolen-git/pointer.json')
  branch_id = pointer['point_to']
  branch_content = read_json(".stolen-git/branches/#{branch_id}.json")
  parent_commit = branch_content['commit_pointer']

  # Getting differences to last commit

  no_insertions = 0
  no_deletions = 0
  no_file_changed = 0
  commit_diff = {}

  getting_diff = lambda do |entries|
    parent_index = 0

    index.each do |key, value|
      if entries && entries[parent_index] && key == entries[parent_index]['path']
        new_hash = value['hash']
        old_hash = entries[parent_index]['hash']
        parent_index += 1
        next if value['hash'] == new_hash

        entry_content = File.read(".stolen-git/storage/blobs/#{old_hash}").lines.to_a
        new_entry_content = File.read(".stolen-git/storage/blobs/#{new_hash}").lines.to_a
        compute_diff(entry_content, new_entry_content)
        diff = build_sequences
        no_insertions += diff[:insertions]
        no_deletions += diff[:deletions]
        no_file_changed += 1
        commit_diff[key] = diff
      else
        new_entry_content = File.read(".stolen-git/storage/blobs/#{value['hash']}").lines.to_a
        compute_diff('', new_entry_content)
        diff = build_sequences

        no_insertions += diff[:insertions]
        no_deletions += diff[:deletions]
        no_file_changed += 1
        commit_diff[key] = diff
      end
    end
  end

  if parent_commit.empty?
    getting_diff.call(nil)
  else
    parent_commit_content = read_json(".stolen-git/commits/#{parent_commit}.json")
    parent_tree_hash = parent_commit_content['tree_hash']
    parent_tree_content = JSON.parse(File.read(".stolen-git/storage/trees/#{parent_tree_hash}.json"))
    getting_diff.call(parent_tree_content['entries'])
  end

  if no_file_changed <= 0
    puts 'Everything up to date'
    puts "If you have changed please 'stg stage' them first "
    return
  end

  # Making the tree
  index.each do |key, value|
    tree_content[:entries].push({
                                  path: key,
                                  type: 'blob',
                                  hash: value['hash'],
                                  diff: commit_diff[key] || {}
                                })
  end
  tree_content[:entries] = tree_content[:entries].sort { |a, b| a['path'] <=> b['path'] }
  tree_content = JSON.pretty_generate(tree_content)
  tree_hash = get_string_hash(tree_content)
  File.write(".stolen-git/storage/trees/#{tree_hash}.json", tree_content)

  # Making the commit
  # TODO: Change to actual values when personal profiles are created
  commit_id = SecureRandom.uuid
  commit_content = {
    tree_hash: tree_hash,
    created_at: Time.now,
    id: commit_id,
    parent_commit: parent_commit,
    branch_id: branch_id,
    author_profile: {
      name: 'Amr',
      email: 'amrbassem218@gmail.com',
      username: 'amrbassem218'
    },
    name: options[:name],
    description: options[:description],

    no_insertions: no_insertions,
    no_deletions: no_deletions,
    no_files_changed: no_file_changed
  }
  commit_content = JSON.pretty_generate(commit_content)
  commit_hash = get_string_hash(commit_content)
  File.write(".stolen-git/commits/#{commit_hash}.json", commit_content)

  # Adding commit to history
  commit_history['commits'].push({ id: commit_id, hash: commit_hash, name: options[:name] })
  File.write('.stolen-git/commits.json', JSON.pretty_generate(commit_history))

  # Adding to branch
  branch_content['commit_pointer'] = commit_hash
  File.write(".stolen-git/branches/#{branch_id}.json", JSON.pretty_generate(branch_content))

  # Print
  puts "#{no_file_changed} files changed, #{no_insertions} insertions(+), #{no_deletions} deletions(-)"
end

#diffObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/actions.rb', line 340

def diff
  index = read_json('.stolen-git/index.json')
  index.each do |key, value|
    new_file_content = File.exist?(key) ? File.read(key) : nil
    file_name = File.basename(key)
    if new_file_content.nil?
      print "@@#{file_name}".cyan
      print " [DELETED]]\n".red
    else
      new_hash = get_file_hash(key)
      next if new_hash == value['hash']

      print "@@#{file_name}".cyan
      blob_content = File.read(".stolen-git/storage/blobs/#{value['hash']}")
      print_diff(blob_content, new_file_content)
    end
  end
end

#logObject



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/actions.rb', line 359

def log
  limit = ARGV.last
  limit = limit && !limit.empty? ? limit.to_i : 0
  commit_history_content = read_json('.stolen-git/commits.json')

  commit_history_content['commits'].each_with_index do |commit, i|
    break if limit > 0 && i >= limit

    if limit == 0 && i >= 5
      q = ask(':')
      break if q == 'q'
    end
    commit_content = read_json(".stolen-git/commits/#{commit['hash']}.json")
    puts
    puts "commit #{commit_content['id']}".green
    puts "Author #{commit_content['author_profile']['username']} <#{commit_content['author_profile']['email']}>"
    puts "Date: #{commit_content['created_at']}"
    puts
    puts "    #{commit_content['name']}"
    puts
  end
end

#p_initializeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/actions.rb', line 11

def p_initialize
  # Check if already initialized
  if File.exist?('.stolen-git')
    if confirm?('An instance of stolen-git is already up here do you want to replace it')
      if confirm?('THIS WILL DELETE ALL COMMITS AND INSTANCES OF stolen-git. ARE YOU SURE')
        FileUtils.rm_rf('.stolen-git')

        if File.exist?('.stolen-git')
          puts 'An error occured during the deletion process of the old directory of stolen-git'
        else
          p_initialize
        end
      else
        puts 'ok'
      end
    else
      puts 'ok'
    end

  else
    # main dot directory
    FileUtils.mkdir_p('.stolen-git')

    # Sub Directories
    FileUtils.mkdir_p('.stolen-git/commits')
    FileUtils.mkdir_p('.stolen-git/branches')

    FileUtils.mkdir_p('.stolen-git/storage')
    FileUtils.mkdir_p('.stolen-git/storage/blobs')
    FileUtils.mkdir_p('.stolen-git/storage/trees')

    # main files
    File.write('.stolen-git/project_info.json', {})
    File.write('.stolen-git/commits.json', JSON.pretty_generate({ commits: [] }))
    File.write('.stolen-git/index.json', {})

    main_branch_id = SecureRandom.uuid
    File.write(".stolen-git/branches/#{main_branch_id}.json",
               JSON.pretty_generate({ name: 'main', created_at: Time.now, commit_pointer: '' }))

    File.write('.stolen-git/pointer.json', JSON.pretty_generate({ point_to: main_branch_id, type: 'branch' }))
    puts 'Stolen-git initialized Sucessfully :D'
  end
end

#resetObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/actions.rb', line 245

def reset
  commit_id = ARGV.first
  commit_history = read_json('.stolen-git/commits.json')
  pointer = read_json('.stolen-git/pointer.json')
  branch_id = pointer['point_to']
  branch_content = read_json(".stolen-git/branches/#{branch_id}.json")
  current_commit_hash = branch_content['commit_pointer']

  new_commit = if !commit_id || commit_id.empty?
                 current_commit_hash
               else
                 commit_history['commits'].find do |x|
                   x['id'] == commit_id
                 end['hash']
               end

  if new_commit.empty?
    puts "This commit doesn't exit"
    return
  end

  revert_to_commit(new_commit)
  branch_content['commit_pointer'] = new_commit
  File.write(".stolen-git/branches/#{branch_id}.json", JSON.pretty_generate(branch_content))
end

#stageObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/actions.rb', line 56

def stage
  inp = ARGV

  if inp.empty?
    puts 'Usage: stg stage <directory/> <file.xy>'
    return
  end

  index = JSON.parse(File.read('.stolen-git/index.json'))

  stage_file = lambda do |file_path|
    file_hash = get_file_hash(file_path)
    file_content = File.read(file_path)

    next if index[file_path] && index[file_path]['hash'] == file_hash

    # Create blob
    File.write(".stolen-git/storage/blobs/#{file_hash}", file_content)

    # Assign index_obj
    default_index_obj = {}
    index[file_path] ||= default_index_obj
    index[file_path]['hash'] = file_hash
  end

  stage_directory = lambda do |dir_path|
    Dir.children(dir_path).each do |entry|
      path = File.join(dir_path, entry)
      if File.file?(path)
        stage_file.call(path)
      else
        stage_directory.call(path)
      end
    end
  end

  inp.each do |inp_path|
    unless File.exist? inp_path
      puts "#{inp_path} doesn't exist"
      next
    end

    if File.file?(inp_path)
      stage_file.call(inp_path)
    elsif File.directory? inp_path
      stage_directory.call(inp_path)
    else
      puts "Error logging #{inp_path}. It's neither a file or a directory"
    end
  end
  index = index.sort.to_h
  File.write('.stolen-git/index.json', JSON.pretty_generate(index))
end