Module: Actions
- Includes:
- DiffCalc
- Defined in:
- lib/actions.rb
Instance Attribute Summary
Attributes included from DiffCalc
Instance Method Summary collapse
- #branch ⇒ Object
- #checkout ⇒ Object
- #commit ⇒ Object
- #diff ⇒ Object
- #log ⇒ Object
- #p_initialize ⇒ Object
- #reset ⇒ Object
- #stage ⇒ Object
Methods included from DiffCalc
#build_sequences, #compute_diff, #differencing, #initialize, #print_diff, #print_line
Instance Method Details
#branch ⇒ Object
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 |
# File 'lib/actions.rb', line 281 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 |
#checkout ⇒ Object
239 240 241 242 243 244 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 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/actions.rb', line 239 def checkout = { commit: false } # Getting commit name & description OptionParser.new do |opts| opts. = 'Usage: stolen-git checkout [options]' opts.on('-c', '--commit', 'Add a commit id instead') do [:commit] = true end end.parse! inp = ARGV.last if !inp puts 'Please Enter the name of a branch or commit_id' elsif [: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 |
#commit ⇒ Object
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 109 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 |
# File 'lib/actions.rb', line 82 def commit = { name: '', description: '' } # Getting commit name & description OptionParser.new do |opts| opts. = 'Usage: stolen-git commit [options]' opts.on('-n', '--name NAME', 'Add a commit name') do |name| [:name] = name end opts.on('-d', '--description DESCRIPTION', 'Add a commit description') do |description| [:description] = description end end.parse! [:name] = ask('Add a commit name: ') if [: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 'stolen-git 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: [:name], description: [: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: [: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 |
#diff ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/actions.rb', line 308 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 |
#log ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/actions.rb', line 327 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_initialize ⇒ Object
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 |
#reset ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/actions.rb', line 217 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'] parent_commit_hash = if !commit_id || commit_id.empty? read_json(".stolen-git/commits/#{current_commit_hash}.json")['parent_commit'] else commit_history['commits'].find { |x| x['id'] == commit_id }['hash'] end if parent_commit_hash.empty? puts "The current commit is the earliest in the project. Can't reset behind that." return end revert_to_commit(parent_commit_hash) branch_content['commit_pointer'] = parent_commit_hash File.write(".stolen-git/branches/#{branch_id}.json", JSON.pretty_generate(branch_content)) end |
#stage ⇒ Object
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 |
# File 'lib/actions.rb', line 56 def stage files = ARGV if files.empty? puts 'Usage: stolen-git stage <files..>' return end index = JSON.parse(File.read('.stolen-git/index.json')) files.each 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 index = index.sort.to_h File.write('.stolen-git/index.json', JSON.pretty_generate(index)) end |